title: Serilog Logging from Class Library method called from .NET 6 Minimal API
body: <p>I have a .Net 6 Core Minimal API set up to use SeriLog to log to MS SQL Server. In my class library I have managed to get logging working with SeriLog ONLY IF I modify the constructors in my class libraries. I am trying to avoid modifying the constructors of my class library classes or methods.</p>
  <p>In my experience with console apps, if I set up SeriLog in my main Program.cs, then I can use logging in any class in my class library without passing the logger to the constructors. So, I can just use Log.Information(&quot;my message&quot;) anywhere in the class library and it works. I am trying to achieve the same with my Program.cs in a .Net 6 minimal API project.</p>
  <p>I sense it should be possible to do from looking at other questions on this topic. In particular <a href="https://stackoverflow.com/questions/41323580/using-dependency-injection-with-net-core-class-library-net-standard">this one</a> in which the answer stated that:</p>
  <blockquote>
  <p>You don't have to do anything in your class library. Only the main application has a composition root (earliest point in an application lifecycle you can set up your object graph).</p>
  </blockquote>
  <p>So following from the above, in my API Program.CS, I have this code (I have indicated what works and what does not in the comments):</p>
  <pre class="lang-cs prettyprint-override"><code>    //Configure SeriLog
      builder.Logging.ClearProviders();
      var appSettings = new ConfigurationBuilder()
         .SetBasePath(Directory.GetCurrentDirectory())
         .AddJsonFile(&quot;appsettings.json&quot;)
         .Build();
      
      var logDB = 
      builder.Configuration.GetSection(&quot;ConnectionStrings:Default&quot;).Value;
      var sinkOpts = new MSSqlServerSinkOptions { TableName = &quot;Logs&quot; };
      var columnOptions = new ColumnOptions();
      
      var logger = new LoggerConfiguration()
         .MinimumLevel.Override(&quot;Microsoft&quot;, 
      Serilog.Events.LogEventLevel.Information)
        .WriteTo.MSSqlServer(
            connectionString: logDB,        
            sinkOptions: sinkOpts,
            columnOptions: columnOptions,
            appConfiguration: appSettings
          ).CreateLogger();
      
      builder.Logging.AddSerilog(logger);
  
      //Depency injection for Class Library method 
      //LoggerTestClass is a class in my Class Library project
      builder.Services.AddScoped&lt;ILoggerTestClass, LoggerTestClass&gt;();
  
      var app = builder.Build();
      app.ConfigureTestAPI();
  </code></pre>
  <p>The Method &quot;ConfigureTestAPI()&quot; is in a Extension Class that is shown below:</p>
  <pre class="lang-cs prettyprint-override"><code>    public static class API_Test
      {
  
      public static void ConfigureTestAPI(this WebApplication app) 
      //Extension method for app
      {               
          app.MapGet(&quot;/test/&quot;, GetTest);
          
      }
  
      private static async Task&lt;IResult&gt; GetTest(int id, 
      ILogger&lt;LoggerTestClass&gt; logger, ILoggerTestClass testClass)
      {
          try
          {
              try
              {
                  //This works
                  logger.LogInformation(&quot;Starting test now&quot;);  
  
                  //This does NOT work
                  Log.Information(&quot;Using Log. directly&quot;); 
  
                  
                  testClass.Test();  //Call to class library method
  
                  logger.LogInformation(&quot;Test finished&quot;);  //Also works
                  return Results.Ok(&quot;OK&quot;);
              }
              catch (Exception ex)
              {
                  return Results.Problem(ex.Message);
              }
          }
          catch (Exception ex)
          {
              return Results.Problem(ex.Message);
          }
       }
      }
  </code></pre>
  <p>And finally here is the class with the test method in my class library:</p>
  <pre class="lang-cs prettyprint-override"><code>        namespace TestingLib.Testing;
      public class LoggerTestClass : ILoggerTestClass
      {
      
          private Microsoft.Extensions.Logging.ILogger _logger;
      
          public LoggerTestClass(ILogger&lt;LoggerTestClass&gt; logger)
          {
              _logger = logger;
          }
      
          public void Test()
          {
              try
              {
      
                 //Does not work
                 Log.Information(&quot;Test logging from class library using Log.&quot;);
      
                 //Does not work
                 Log.Logger.Information(&quot;In Test Class in DLL. Trying loging with [Log.Logger.Information]&quot;);
      
                 //This works
                 _logger.LogInformation(&quot;In Test Class in DLL. Trying loging with [_logger.LogInformation]&quot;);
              }
              catch (Exception ex)
              {
                  Log.Error(&quot;An error in class library&quot;);
              }
          }
      
      }
  
  </code></pre>