title: How to use dependency injection in Serilog Sink?
body: <p>I follow this article. <a href="https://github.com/serilog/serilog/wiki/Developing-a-sink" rel="nofollow noreferrer">Developing a sink</a></p>
  <p>But I need to injection IHttpClientFactory.</p>
  <pre><code>public class MySink : ILogEventSink
  {
      //...
  
      public MySink(IFormatProvider formatProvider, IHttpClientFactory httpClientFactory)
      {
          //...
      }
  }
  
  public static class MySinkExtensions
  {
      public static LoggerConfiguration MySink(
          this LoggerSinkConfiguration loggerConfiguration, 
          IHttpClientFactory httpClientFactory, 
          IFormatProvider formatProvider = null,
          LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose)
      {
          return loggerConfiguration.Sink(new MySink(formatProvider, httpClientFactory), restrictedToMinimumLevel);
      }
  }
  </code></pre>
  <p>Program Main:</p>
  <pre><code>class Program
  {
      static async Task Main(string[] args)
      {
          var serviceCollection = new ServiceCollection();
          serviceCollection.AddHttpClient();
  
          var logger = new LoggerConfiguration()
              .ReadFrom.Configuration(configuration)
              .WriteTo.MySink(null) //&lt;--How to get IHttpClientFactory?
              .CreateLogger();
  
          services.AddLogging(configure =&gt; configure.ClearProviders().AddSerilog(logger));
  
          var serviceProvider = services.BuildServiceProvider();
      }
  }
  </code></pre>
  <p>appsettings:</p>
  <pre><code>{
  &quot;Serilog&quot;: {
      &quot;Using&quot;: [ &quot;Serilog.Sinks.Console&quot;, &quot;MyTestProject&quot; ],
      &quot;WriteTo&quot;: [
          {
              &quot;Name&quot;: &quot;Console&quot;,
              &quot;Args&quot;: {
                  &quot;restrictedToMinimumLevel&quot;: &quot;Debug&quot;
              }
          },
          {
              &quot;Name&quot;: &quot;MySink&quot;,
              &quot;Args&quot;: {
                  &quot;restrictedToMinimumLevel&quot;: &quot;Warning&quot;
              }
          }
      ]
  }
  </code></pre>
  <p>Can I use the Args to injection IHttpClientFactory?</p>
  <p>Or handle this in Main method?</p>
  <p>Note: I am using net core console application.</p>
  <p><strong>Update:</strong> According to the <a href="https://stackoverflow.com/a/57421439/11657865">asnwer</a></p>
  <p>Now, the Program Main is:</p>
  <pre><code>class Program
      {
          static async Task Main(string[] args)
          {
              var serviceCollection = new ServiceCollection();
              serviceCollection.AddHttpClient();
  
              serviceCollection.AddSingleton&lt;Serilog.ILogger&gt;(sp =&gt;
              {
                  var httpClientFactory = sp.GetRequiredService&lt;IHttpClientFactory&gt;();
  
                  return new LoggerConfiguration()
                      .MinimumLevel.Debug()
                      .WriteTo.MySink(httpClientFactory)
                      .CreateLogger();
              });
  
              serviceCollection.AddLogging(configure =&gt; configure.ClearProviders().AddSerilog());
  
              var serviceProvider = serviceCollection.BuildServiceProvider();
  
              Log.Logger = serviceProvider.GetRequiredService&lt;Serilog.ILogger&gt;();
          }
      }
  </code></pre>
  <p>And I can injection ILogger in other class</p>
  <pre><code>class Test
  {
      private readonly ILogger&lt;Test&gt; _logger;
  
      Test(ILogger&lt;Test&gt; logger)
      {
          _logger = logger;
      }
  }
  </code></pre>
  <p><strong>Update:</strong> (2020-07-01)</p>
  <p>Now, in asp net core, this <a href="https://github.com/serilog/serilog-extensions-hosting/pull/20" rel="nofollow noreferrer">package</a> supply dependency injection.</p>
  <pre><code>.UseSerilog((hostingContext, services, loggerConfiguration) =&gt; loggerConfiguration
  .ReadFrom.Configuration(hostingContext.Configuration)
  .Enrich.FromLogContext()
  .WriteTo.SomeSink(services.GetRequiredService&lt;ISomeDependency&gt;()));
  </code></pre>