title: Using Asp.Net Core 2 Injection for Serilog with Multiple Projects
body: <p>I have Serilog configured for Asp.Net Core 2.0 and it works great via .Net Core dependency injection in my startup web project (if I use it through Microsoft.Extensions.Logging), but I can't access it from any other project.</p>
  
  <p>Here's what I have:</p>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>using System;
  using System.IO;
  using Microsoft.AspNetCore;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.Extensions.Configuration;
  using Serilog;
  
  namespace ObApp.Web.Mvc
  {
      public class Program
      {
          public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
              .SetBasePath(Directory.GetCurrentDirectory())
              .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
              .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
              .Build();
  
          public static void Main(string[] args)
          {
              Log.Logger = new LoggerConfiguration()
                  .ReadFrom.Configuration(Configuration)
                  .CreateLogger();
  
              try
              {
                  Log.Warning("Starting BuildWebHost");
  
                  BuildWebHost(args).Run();
              }
              catch (Exception ex)
              {
                  Log.Fatal(ex, "Host terminated unexpectedly");
              }
              finally
              {
                  Log.CloseAndFlush();
              }
          }
  
          public static IWebHost BuildWebHost(string[] args) =&gt;
              WebHost.CreateDefaultBuilder(args)
                  .UseStartup&lt;Startup&gt;()
                  .UseSerilog()
                  .Build();
      }
  }
  </code></pre>
  
  <p><strong>Startup.cs</strong></p>
  
  <p>Startup .cs is pure default. I haven't altered it in any way from the original template for the new Asp.Net Core MVC project.</p>
  
  <p>I have thought that I might need to add Serilog in the IServiceCollection services, but the article <em>Leaner, meaner ASP.NET Core 2 logging</em> at <a href="https://nblumhardt.com/2017/08/use-serilog/" rel="noreferrer">https://nblumhardt.com/2017/08/use-serilog/</a> tells me it's unnecessary.</p>
  
  <blockquote>
    <p>You can then go ahead and delete any other logger configuration that’s
    hanging around: there’s no need for a "Logging" section in
    appsettings.json, no AddLogging() anywhere, and no configuration
    through ILoggerFactory in Startup.cs.</p>
    
    <p>UseSerilog() replaces the built-in ILoggerFactory so that every logger
    in your application, whether it’s Serilog’s Log class, Serilog’s
    ILogger, or Microsoft.Extensions.Logging.ILogger, will be backed
    with the same Serilog implementation, and controlled through the same
    configuration.</p>
  </blockquote>
  
  <p><strong>What I Expected</strong></p>
  
  <p>The ability to simply inject a logger via a constructor in any class in any project in the solution. For example:</p>
  
  <pre><code>using Serilog;
  public MyTestClass(ILogger logger) { ... }
  </code></pre>
  
  <p><strong>What I Got - Web (Startup) Project</strong></p>
  
  <p>Injection works in the HomeController if I use the Microsoft.Extensions.Logging wrapper:</p>
  
  <pre><code>using Microsoft.Extensions.Logging;
  
  public class HomeController : Controller
  {
      ILogger&lt;HomeController&gt; _logger;
  
      public HomeController(ILogger&lt;HomeController&gt; logger)
      {
          _logger = logger;
          _logger.LogDebug("Controller instantiated.");
      }
  }
  </code></pre>
  
  <p>Injection fails in any project/class if I try to inject Serilog.ILogger</p>
  
  <pre><code>using Serilog;
  using Serilog.Extensions.Logging; // Not sure if this would help.
  
  public class HomeController : Controller
  {
      ILogger _logger;
  
      public HomeController(ILogger logger)
      {
          _logger = logger;
          _logger.Debug("Controller instantiated.");
      }
  }
  </code></pre>
  
  <blockquote>
    <p>InvalidOperationException: Unable to resolve service
    for type 'Serilog.ILogger' while attempting to activate 
    'ObApp2.Web.Mvc.Controllers.HomeController'.</p>
  </blockquote>
  
  <p><strong>My Bigger Issue - OtherProjects</strong></p>
  
  <p>My bigger issue is that I can't get a logger via DI through any method I've tried if I'm working in another project in the solution.</p>
  
  <p>When I try to inject either using Microsoft.Extensions.Logging or using Serilog I get a missing parameter exception at build time.</p>
  
  <pre><code>using Microsoft.Extensions.Logging;
  namespace ObApp.Domain
  {
      public class MyTestClass(ILogger&lt;MyTestClass&gt; logger) { ... }
  
  - or -
  
  using Serilog;
  namespace ObApp.Domain
  {
      public class MyTestClass(ILogger logger) { ... }
  </code></pre>
  
  <p>Both generate build error similar to:</p>
  
  <blockquote>
    <p>There is no argument given that corresponds to the required formal
    parameter 'logger' of 'MyTestClass.MyTestClass(ILogger)'</p>
  </blockquote>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li><p>When injecting with this type of Serilog configuration, is it recommended that I reference Microsoft.Extensions.Logging or Serilog in the class files where I'm doing the injection?</p></li>
  <li><p>How can I get DI to work through all projects?</p></li>
  </ol>