title: Logging as a decorator vs. Dependency Injection - what if I need to log inside the class?
body: <p><em>(I originally asked this question in <a href="https://stackoverflow.com/questions/7905110/logging-aspect-oriented-programming-and-dependency-injection-trying-to-make?lq=1#comment56990956_7906547">this comment</a>, but Mark Seemann asked me to create a new question instead.)</em></p>
  
  <p>I'm starting a new app (.NET Core, if that matters), and right now I'm trying to decide how exactly to do logging.</p>
  
  <p>The general consensus seems to be that logging is a cross-cutting concern, so the logger shouldn't be injected directly into the class that is supposed to log.</p>
  
  <p>Often, there's an example like the following class how <strong>not</strong> to do it:</p>
  
  <pre><code>public class BadExample : IExample
  {
      private readonly ILogger logger;
  
      public BadExample(ILogger logger)
      {
          this.logger = logger;
      }
  
      public void DoStuff()
      {
          try
          {
              // do the important stuff here
          }
          catch (Exception e)
          {
              this.logger.Error(e.ToString());
          }
      }
  }
  </code></pre>
  
  <p>Instead, the class with the business logic shouldn't know about the logger (<a href="https://en.wikipedia.org/wiki/Single_responsibility_principle" rel="noreferrer">SRP</a>) and there should be a separate class which does the logging:</p>
  
  <pre><code>public class BetterExample : IExample
  {
      public void DoStuff()
      {
          // do the important stuff here
      }
  }
  
  public class LoggingBetterExample : IExample
  {
      private readonly IExample betterExample;
      private readonly ILogger logger;
  
      public LoggingBetterExample(IExample betterExample, ILogger logger)
      {
          this.betterExample = betterExample;
          this.logger = logger;
      }
  
      public void DoStuff()
      {
          try
          {
              this.betterExample.DoStuff();
          }
          catch (Exception e)
          {
              this.logger.Error(e.ToString());
          }
      }
  }
  </code></pre>
  
  <p>Whenever an <code>IExample</code> is needed, the DI container returns an instance of <code>LoggingBetterExample</code>, which uses <code>BetterExample</code> (which contains the actual business logic) under the hood.</p>
  
  <h3>Some sources for this approach:</h3>
  
  <p>Blog posts by <a href="https://stackoverflow.com/users/126014/mark-seemann">Mark Seemann</a>:</p>
  
  <ul>
  <li><a href="http://blog.ploeh.dk/2010/09/20/InstrumentationwithDecoratorsandInterceptors/" rel="noreferrer">Instrumentation with Decorators and Interceptors</a></li>
  <li><a href="http://blog.ploeh.dk/2010/04/07/DependencyInjectionisLooseCoupling/" rel="noreferrer">Dependency Injection is Loose Coupling</a></li>
  </ul>
  
  <p>Blog post and SO answer by <a href="https://stackoverflow.com/users/264697/steven">Steven</a>:</p>
  
  <ul>
  <li><a href="https://cuttingedge.it/blogs/steven/pivot/entry.php?id=91" rel="noreferrer">Meanwhile... on the command side of my architecture</a></li>
  <li><a href="https://stackoverflow.com/a/9915056/6884">Windsor - pulling Transient objects from the container</a></li>
  </ul>
  
  <hr>
  
  <h1>My question:</h1>
  
  <p>Obviously, the <code>LoggingBetterExample</code> approach only works as long as the logging can be done outside the actual class.<br>
  (like in the example above: catch any exceptions thrown by <code>BetterExample</code> from outside)</p>
  
  <p>My problem is that I'd like to log other things inside the actual class.<br>
  Mark Seemann <a href="https://stackoverflow.com/questions/7905110/logging-aspect-oriented-programming-and-dependency-injection-trying-to-make?lq=1#comment42283040_7906547">suspected here</a> that if someone needs to do this, maybe the method in question is doing too much.</p>
  
  <p>As I said before, I'm in the planning phase for a new application, so I don't have much code to show, but the use case I'm thinking right now is something like this:</p>
  
  <p>My app will have a config file with some optional values.<br>
  The user may decide to omit the optional values, but it's an important decision to do this.<br>
  So I'd like to log a warning when some of the optional values are missing, just in case it happened by error.<br>
  <em>(omitting the values is perfectly fine though, so I can't just throw an exception and stop)</em></p>
  
  <p>This means that I will have a class which reads config values and needs to do something like this (pseudocode):</p>
  
  <pre><code>var config = ReadConfigValues("path/to/config.file");
  
  if (config.OptionalValue == null)
  {
      logger.Warn("Optional value not set!");
  }
  </code></pre>
  
  <p>No matter if <code>ReadConfigValues</code> is in this class or a different one, I don't think this class would violate the SRP.</p>
  
  <h3>When I'm not able to log outside the actual class by using a decorator, is there a better solution than to inject the logger?</h3>
  
  <p>I know I could read the config file in the inner class, but check the values (and log the warning) in the decorator. But IMO checking the value is business logic and not infrastructure, so to me it belongs in the same class where the config file is read.</p>