Logging, Aspect Oriented Programming, and Dependency Injection – Trying to make sense of it all

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.

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).

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

Leave a Comment