日志记录,面向方面编程,和依赖注入 - 试图让这一切的感觉 [英] Logging, Aspect Oriented Programming, and Dependency Injection - Trying to make sense of it all

查看:253
本文介绍了日志记录,面向方面编程,和依赖注入 - 试图让这一切的感觉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,伐木是一个最好的用例AOP。此外,当你想使用DI,这样的类不与一个特定的日志实现日志的包装也例举案例。但是,<一个href="http://stackoverflow.com/questions/940831/how-to-use-log4net-with-dependency-injection/940872#940872">some考虑记录包装的反模式。首先,这样的图是因为在大多数情况下,包装器趋向于简单化和删除许多功能的具体的日志框架的。如果要实现这些特定功能,为什么不直接使用的框架。

I know that logging is a prime use case for AOP. Additionally logging wrappers are also exemplified as cases when you want to use DI so that classes aren't coupled with a specific logging implementation. However, some consider logging wrappers an anti-pattern. Primarily, such a view is because in most cases the wrapper tends to be simplistic and removes many of the features specific of the logging framework. If you implement those specific features, why not just use the framework directly.

我知道 Common.Logging 门面,试图对log4net的,EntLib的功能抽象量好,NLOG为您服务。然而,即使在这里,我们仍然有各种各样的Common.Logging的依赖。不要在关于接口和code /单元测试等办法,但如果该项目肯定就死(自上次发布它已经超过一年),或者你想后切换到记录器不支持,可能会导致问题。

I am aware of the Common.Logging facade that attempts to abstract a good amount of the features of log4Net, EntLib, NLog for you. However, even here we still have a dependency of sorts on Common.Logging. Not in a code/unit testing way regarding interfaces and such, but if the project dies (it's been over a year since the last release) or you want to latter switch to a logger not supported, that can cause problems.

这是说,如果日志记录是通过AOP实现的,它甚至需要使用DI的记​​录依赖(即为什么不直接引用说NLOG)?是的code表示AOP部分将被紧密耦合,但一个人想单元测试类的逻辑是缺乏记录的依赖关系(编织发生至少前)。这是在这一点上,我有点失去了(我没有尝试过的AOP还没有)。在织造后,就已经不使用DI为AOP code引起的问题进行单元测试的测试方法?或者没有编织AOP code可一个单元测试?

That said, if logging is achieved via AOP, is it even necessary to use DI for the logging dependency (i.e. why not just directly reference say NLog)? Yes that AOP portion of code would be tightly coupled, but the logic of the classes that one wants to unit test is devoid of logging dependencies (at least before the weaving happens). It's at this point that I'm a bit lost (I haven't tried AOP yet). After weaving, would having not used DI for the AOP code cause problems for unit testing the method under test? Or can one unit test without weaving the AOP code?

除非记录是该软件的用户的需求,我不知道它是多么有用的测试记录已发生与嘲笑。我认为测试方法的业务逻辑是最有兴趣的测试。最后,如​​果一个人想使用TDD / BDD,也没有一所使用的DI在AOP code中的日志记录的依赖?或者,总会有只是没有的试驾的事情的AOP的一面呢?

Unless logging is a requirement of the user of the software, I'm not sure how useful it is to test that logging has occured with mocks. I would think that the business logic of the method under test is what most would be interested in testing. Lastly, if one wants to use TDD/BDD, wouldn't one have to use DI for the logging dependencies in the AOP code? Or would one just not test drive the AOP side of things?

正如你所看到的,我想感受一下最实用的方法是用于开发将同时使用AOP进行跨领域,关注和DI设计/测试的应用程序。 由于AOP是比较新的,和记录是最常见的例子,什么是推荐的方法?

As you can see, I'm trying to get a feel for what the most practical approach is for developing an application that would be using both AOP for cross-cutting-concerns and DI for design/testing. Since AOP is relatively new, and logging is the most common example, what is the recommended approach?

推荐答案

日志是不是一个服务,这是一个跨领域关注。因此,最好用装饰实施。然而,加入大量的装修只是为了使各种不同的服务日志往往违反了,在这种情况下,你可以然后进一步发展那些装饰成一个单一的拦截器。

Logging is not a Service, it's a cross-cutting concern. As such, it's best implemented with a Decorator. However, adding lots of Decorators just to enable logging of various different services tend to violate DRY, in which case you can then further evolve those Decorators into a single Interceptor.

在你的可以的使用IL织来实现AOP,更好的选择是使用支持动态截取一个DI容器,因为它是一个更为轻量级的解决方案。

While you can use IL weaving to implement AOP, a better option is to use a DI Container that supports dynamic interception, as it's a far more lightweight solution.

这使您可以完全记录脱钩的具体服务。在这种情况下的话,我会说,没有任何理由来包装任何特定的日志框架,因为如果你想改变日志框架,你可以改变这种单一的拦截器。

This enables you to completely decouple concrete services from logging. In that case then, I'd say that there's no reason to wrap any particular logging framework, because if you ever want to change the logging framework, you can just change that single Interceptor.

下面是一个例子谈到有关装饰和拦截器的仪器(非常类似于日志)

Here's an example that talks about Decorators and Interceptors for instrumentation (very similar to logging).

如果您想了解更多关于AOP和DI,您可以<一href="http://channel9.msdn.com/Events/GOTO/GOTO-2011-Copenhagen/CPH-Aspect-Oriented-Programming-with-Dependency-Injection">view上网本演讲中,我给在GOTO哥本哈根2010 。

If you want to learn more about AOP and DI, you can view online this talk I gave at GOTO Copenhagen 2010.

这篇关于日志记录,面向方面编程,和依赖注入 - 试图让这一切的感觉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆