title: Passing in the type of the declaring class for NLog using Autofac
body: <p>Following on from <a href="https://stackoverflow.com/questions/3947136/problem-matching-specific-nlog-logger-name">this question</a> I would like autofac to inject the type of the declaring object into the constructor of my NLog service, so that it can correctly log which type is logging entries.</p>
  
  <p>My NLogService class looks like this...</p>
  
  <pre><code>public class NLogService : ILogService
  {
      private readonly Logger _logger;
  
      public NLogService(Type t)
      {
          var consumerType = t.DeclaringType.FullName;
          _logger = LogManager.GetLogger(consumerType);
      }
  </code></pre>
  
  <p>However it fails on app startup because it obviously cannot work out what to inject into the constructor of the NLogService with the following error...</p>
  
  <blockquote>
    <p>None of the constructors found with
    'Public binding flags' on type
    'MyProduct.Domain.Services.Logging.NLogService'
    can be invoked with the available
    services and parameters: Cannot
    resolve parameter 'System.Type t' of
    constructor 'Void .ctor(System.Type)'.</p>
  </blockquote>
  
  <p>So, my question is - how do i instruct autofac to inject the type of the calling class?</p>
  
  <p>I tried this...</p>
  
  <pre><code>public NLogService(Type t)
      {
          var method = MethodBase.GetCurrentMethod();
          Type consumingType = method.DeclaringType;
          var consumerType = consumingType.FullName;
          var consumerType = t.DeclaringType.FullName;
          _logger = LogManager.GetLogger(consumerType);
      }
  </code></pre>
  
  <p>But i just end up with <code>MyProduct.Domain.Services.Logging.NLogService</code></p>
  
  <p>What i want is the type of the class that is doing the actual logging.</p>
  
  <p>i have already <a href="https://stackoverflow.com/questions/4774286/is-it-possible-to-get-container-type-in-autofac">tried this suggestion</a> and it didnt work for me either.</p>