title: .Net Core 3.1 Using Serilog with Microsoft.Extensions.Logger
body: <p>In this application I've pulled in and setup Serilog, which works for exceptions, and I wanted to use the Microsoft's Logger with Serilog, but I can't seem to figure out how to DI it into services without this error during <code>dotnet build</code>.</p>
  <p>I'm not sure what I'm missing to have ILogger be using an instance of Serilog?</p>
  <p><strong>Error on <code>dotnet build</code>:</strong></p>
  <pre><code>[13:56:21 FTL] Host terminated unexpectedly
  System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'App.Services.MyService'.) 
  </code></pre>
  <p><strong>Dependency Injection into Service:</strong></p>
  <pre class="lang-cs prettyprint-override"><code>using Microsoft.Extensions.Logging;
  
  public class MyService : BaseService
  {
      private readonly ILogger _logger;
  
      public MyService(
          ILogger logger)
          : base(context, httpContext)
      {
          _logger = logger;
      }
      
      // ... Removed for brevity
  }
  </code></pre>
  <p>Implementation</p>
  <pre class="lang-cs prettyprint-override"><code>public static int Main(string[] args)
  {
      CreateLogger();
  
      try
      {
          Log.Information(&quot;Starting web host&quot;);
          CreateHostBuilder(args)
              .Build()
              .Run();
          return 0;
      }
      catch (Exception ex)
      {
          Log.Fatal(ex, &quot;Host terminated unexpectedly&quot;);
          return 1;
      }
      finally
      {
          // Ensure buffered logs are written to their target sink
          Log.CloseAndFlush();
      }
  }
  
  public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
      Host.CreateDefaultBuilder(args)
          .ConfigureWebHostDefaults(webBuilder =&gt; webBuilder.UseStartup&lt;Startup&gt;())
          .UseSerilog();
  
  private static void CreateLogger()
  {
      string path = PrimeConstants.LOG_FILE_PATH;
  
      try
      {
          if (isDevelopment())
          {
              Directory.CreateDirectory(path);
          }
      }
      catch (Exception e)
      {
          Console.WriteLine(&quot;Creating the logging directory failed: {0}&quot;, e.ToString());
      }
  
      var name = Assembly.GetExecutingAssembly().GetName();
      var outputTemplate = &quot;[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}&quot;;
  
      Log.Logger = new LoggerConfiguration()
          .MinimumLevel.Information()
          .MinimumLevel.Override(&quot;Microsoft&quot;, LogEventLevel.Warning)
          .MinimumLevel.Override(&quot;Microsoft.Hosting.Lifetime&quot;, LogEventLevel.Information)
          .MinimumLevel.Override(&quot;System&quot;, LogEventLevel.Warning)
          .Enrich.FromLogContext()
          .Enrich.WithMachineName()
          .Enrich.WithProperty(&quot;Assembly&quot;, $&quot;{name.Name}&quot;)
          .Enrich.WithProperty(&quot;Version&quot;, $&quot;{name.Version}&quot;)
          .WriteTo.Console(
              outputTemplate: outputTemplate,
              theme: AnsiConsoleTheme.Code)
          .WriteTo.Async(a =&gt; a.File(
              $@&quot;{path}/prime.log&quot;,
              outputTemplate: outputTemplate,
              rollingInterval: RollingInterval.Day,
              shared: true))
          .WriteTo.Async(a =&gt; a.File(
              new JsonFormatter(),
              $@&quot;{path}/prime.json&quot;,
              rollingInterval: RollingInterval.Day))
          .CreateLogger();
  }
  </code></pre>