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

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

问题描述

我知道日志记录是 AOP 的主要用例.此外,当您想要使用 DI 以便类不与特定的日志实现耦合时,日志包装器也被举例说明.但是,有些人认为日志包装器是一种反模式.主要是因为在大多数情况下,包装器趋于简单,并且删除了许多特定于日志记录框架的功能.如果您实现了这些特定功能,为什么不直接使用该框架.

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 有某种依赖性.不是关于接口等的代码/单元测试方式,但如果项目结束(自上次发布以来已经过去一年多了)或者您想稍后切换到不受支持的记录器,这可能会导致问题.

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)?是的,代码的 AOP 部分将是紧密耦合的,但是想要进行单元测试的类的逻辑没有日志依赖关系(至少在编织发生之前).正是在这一点上,我有点迷茫(我还没有尝试过 AOP).编织之后,AOP 代码没有使用 DI 会导致对被测方法进行单元测试的问题吗?或者可以在不编写 AOP 代码的情况下进行单元测试吗?

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 代码中的日志依赖项吗?或者只是不试驾 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?

推荐答案

日志记录不是一种服务,而是一种 交叉关注.因此,最好使用装饰器来实现.然而,添加大量装饰器只是为了启用各种不同服务的日志记录往往会违反 DRY,在这种情况下,您可以进一步将这些装饰器演变为单个拦截器.

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 weaving 来实现 AOP,但更好的选择是使用支持动态拦截的 DI Container,因为它是一种更轻量级的解决方案.

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,你可以在线查看我在 2010 年 GOTO 哥本哈根会议上的演讲.

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天全站免登陆