title: How to pass dependencies to a custom .NET Core ILoggerProvider
body: <p>I am creating a custom .NET Core <code>ILoggerProvider</code> that requires some dependencies to be passed into its constructor.</p>
  
  <p>I believe I am using a fairly common pattern to initialize my logging implementation; it looks something like this:</p>
  
  <pre><code>var services = new ServiceCollection();
  
  // Register some services here
  
  services.AddLogging(builder =&gt;
  {
      builder.AddProvider(new DebugLoggerProvider());
  });
  
  var provider = services.BuildServiceProvider();
  </code></pre>
  
  <p>I want to add my new provider within the <code>AddLogging</code> block, in the same way that the <code>DebugLoggerProvider</code> is currently added.</p>
  
  <p>My custom provider requires some other services to be passed into its constructor and since these are already registered with the <code>ServiceCollection</code>, I assume that I should be able to reference them. However, unlike methods such as <code>AddSingleton</code>, which have an overload that exposes the <code>IServiceProvider</code>, <code>AddLogging</code> doesn't seem to offer an equivalent.</p>
  
  <p>Is there a simple way to achieve this, or am I attempting to do something that contradicts the way .NET Core logging was designed to be deployed?</p>
  
  <p>UPDATE:</p>
  
  <p>After experimenting with the suggestions proposed by @Nkosi, I can confirm that it is possible to get this to work by bypassing <code>AddLogging</code> and directly implementing what it does internally, as follows:</p>
  
  <pre><code>var services = new ServiceCollection();
  
  // Register some services
  services.AddSingleton&lt;IMyService, MyService&gt;();
  
  // Initialize logging
  services.AddOptions();
  services.AddSingleton&lt;ILoggerFactory, LoggerFactory&gt;();
  services.AddSingleton(typeof(ILogger&lt;&gt;), typeof(Logger&lt;&gt;));
  services.AddSingleton&lt;ILoggerProvider&gt;(p =&gt; new DebugLoggerProvider());
  services.AddSingleton&lt;ILoggerProvider&gt;(p =&gt; new MyLoggerProvider("Constant value", p.GetService&lt;IMyService&gt;()));
  
  var provider = services.BuildServiceProvider();
  </code></pre>