title: Logging, Aspect Oriented Programming, and Dependency Injection - Trying to make sense of it all
body: <p>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, <a href="https://stackoverflow.com/questions/940831/how-to-use-log4net-with-dependency-injection/940872#940872">some consider logging wrappers an anti-pattern</a>. 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. </p>
  
  <p>I am aware of the <a href="http://netcommon.sourceforge.net/index.html" rel="noreferrer">Common.Logging</a> 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.</p>
  
  <p>That said, <strong>if logging is achieved via AOP, is it even necessary to use DI for the logging dependency</strong> (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? </p>
  
  <p>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 <em>test drive</em> the AOP side of things? </p>
  
  <p>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. <strong>Since AOP is relatively new, and logging is the most common example, what is the recommended approach?</strong></p>