title: Microsoft Logging with open generic types and Dependency Injection
body: <p>I'm really trying to wrap my head around all of this.</p>
  
  <p>I want to use the ILogger in my "AddTextCalculator1" Example class using open generic types.</p>
  
  <p><a href="https://stackify.com/net-core-dependency-injection/" rel="nofollow noreferrer">As noted here</a></p>
  
  <p>First I register the Services</p>
  
  <pre><code>var serviceProvider = new ServiceCollection()
      .AddLogging()
      .BuildServiceProvider();
  
  var logger = serviceProvider.GetService&lt;ILoggerFactory&gt;()
      .CreateLogger&lt;Program&gt;();
  
  var services = new ServiceCollection();
  services.AddInternalServices();
  services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger&lt;&gt;), typeof(Logger&lt;&gt;)));
  services.AddTextCalculator1();
  
  var testCalculatorOneService = provider.GetService&lt;ISomeCalculationOne&gt;();
  </code></pre>
  
  <p>Then the IServiceCollectionExtension class</p>
  
  <pre><code>public static class IServiceCollectionExtension
  {
      public static IServiceCollection AddTextCalculator1(this IServiceCollection services )
      {
          //services.AddTransient&lt;ISomeCalculationOne&gt;(s =&gt; new SomeCalculationOne(serviceProvider));
          services.AddTransient&lt;ISomeCalculationOne, SomeCalculationOne&gt;();
          return services;
      }
  }
  </code></pre>
  
  <p>My Interface</p>
  
  <pre><code>public interface ISomeCalculationOne 
  {
      void DoSomeCalcAndOutputToScreen();
  }
  </code></pre>
  
  <p>And then my class</p>
  
  <pre><code>public class SomeCalculationOne : ISomeCalculationOne
  {
      public ILogger _logger { get; private set; }
  
      public SomeCalculationOne(ILogger&lt;SomeCalculationOne&gt; logger)
      {
          _logger = logger;
          _logger.LogInformation("Constructed - HOT DAIMN!!!!");
      }
  
      public void DoSomeCalcAndOutputToScreen()
      {
          int a = 1 + 1;
          _logger.LogDebug("This is a debug logging test");
          Console.WriteLine(a.ToString());
      }
  }
  </code></pre>
  
  <p>It's currently throwing an error because I am not passing in logger of type ILogger into my constructor (Notice the commented out code in my  IServiceCollectionExtension class)</p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'Unable to resolve service for type
    'Microsoft.Extensions.Logging.ILoggerFactory' while attempting to
    activate 'Microsoft.Extensions.Logging.Logger`</p>
  </blockquote>
  
  <p>But that would mean I would have to pass in logger of type ILogger when defining my services</p>
  
  <pre><code>services.AddTextCalculator1(ILogger&lt;SomeCalculationOne&gt; bla);
  </code></pre>
  
  <p>But that just completely defies the point of adding logger to my services in the first place, or I'm I not understanding it correctly?</p>