title: How to perform logging in ConfigureServices method of Startup.cs in ASP.NET Core 5.0
body: <p>Constructor injection of a logger into <code>Startup</code> works in earlier versions of ASP.NET Core because a separate DI container is created for the Web Host. As of now only one container is created for Generic Host, see the <a href="https://github.com/aspnet/Announcements/issues/353" rel="noreferrer">breaking change announcement</a>.
  <br><br>
  Startup.cs</p>
  <pre><code>public class Startup
  {
      /// &lt;summary&gt; The configuration. &lt;/summary&gt;
      public IConfiguration Configuration { get; }
      
      /// &lt;summary&gt; The web host environment. &lt;/summary&gt;
      public IWebHostEnvironment WebHostEnvironment { get; }
  
      public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
      {
          Configuration = configuration;
          WebHostEnvironment = webHostEnvironment;
      }
  
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddServices(Configuration);    // This is a custom method, that adds multiple services to the container.
      }
  
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger&lt;Startup&gt; logger) =&gt;
      app.UseComponents(env, Configuration, logger);
  }
  </code></pre>
  <p>Now as per the <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0#configure-a-service-that-depends-on-ilogger" rel="noreferrer">MSDN</a>, I have modified my Startup.cs as follows:</p>
  <pre><code>public class Startup
  {
      /// &lt;summary&gt; The configuration. &lt;/summary&gt;
      public IConfiguration Configuration { get; }
      
      public ILogger&lt;Startup&gt; Logger { get; set; }
  
      /// &lt;summary&gt; The web host environment. &lt;/summary&gt;
      public IWebHostEnvironment WebHostEnvironment { get; }
  
      public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
      {
          Configuration = configuration;
          WebHostEnvironment = webHostEnvironment;
      }
  
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddSingleton((container) =&gt;
          {
              var logger = container.GetRequiredService&lt;ILogger&lt;MyService&gt;&gt;();
              return new Startup(Configuration, WebHostEnvironment) { Logger = logger };
          });    // I know this is incorrect, but that my question- how do I correctly access logger in here?
          services.AddServices(Configuration, Logger);    // Logger =&gt; null
      }
  
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger&lt;Startup&gt; logger) =&gt;
      app.UseComponents(env, Configuration, logger);
  }
  </code></pre>
  <p>StartupExtension.ConfigureServices.cs</p>
  <pre><code>public static partial class StartupExtension
  {
      #region Internal Method
      /// &lt;summary&gt; Adds all the services required by the DemoUsageApp. &lt;/summary&gt;
      /// &lt;param name=&quot;services&quot;&gt;The services, &lt;see cref=&quot;IServiceCollection&quot;/&gt;.&lt;/param&gt;
      /// &lt;param name=&quot;configuration&quot;&gt;The configuration, &lt;see cref=&quot;IConfiguration&quot;/&gt;.&lt;/param&gt;
      /// &lt;param name=&quot;logger&quot;&gt;The logger, &lt;see cref=&quot;ILogger{Startup}&quot;/&gt;.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      internal static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration, ILogger logger)
      {
          services.AddDotNetCoreServices(configuration, logger);
          services.AddCrossCuttingServices(configuration, logger);
  
          return services;
      }
      #endregion Internal Method
  
      #region Private Methods
      /// &lt;summary&gt; Adds .NET Core services. &lt;/summary&gt;
      /// &lt;param name=&quot;services&quot;&gt;The services, &lt;see cref=&quot;IServiceCollection&quot;/&gt;.&lt;/param&gt;
      /// &lt;param name=&quot;configuration&quot;&gt;The configuration, &lt;see cref=&quot;IConfiguration&quot;/&gt;.&lt;/param&gt;
      /// &lt;param name=&quot;logger&quot;&gt;The logger, &lt;see cref=&quot;ILogger{Startup}&quot;/&gt;.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      private static IServiceCollection AddDotNetCoreServices(this IServiceCollection services, IConfiguration configuration, ILogger logger)
      {
          logger.LogInformation(&quot;----  Adding .NET Core components  ----&quot;);
  
          services.AddMvc();
          services.AddControllers();
  
          logger.LogInformation(&quot;----  Successfully added .NET Core components  ----&quot;);
  
          return services;
      }
  
      /// &lt;summary&gt; Adds Cross-Cutting services. &lt;/summary&gt;
      /// &lt;param name=&quot;services&quot;&gt;The services, &lt;see cref=&quot;IServiceCollection&quot;/&gt;.&lt;/param&gt;
      /// &lt;param name=&quot;configuration&quot;&gt;The configuration, &lt;see cref=&quot;IConfiguration&quot;/&gt;.&lt;/param&gt;
      /// &lt;param name=&quot;logger&quot;&gt;The logger, &lt;see cref=&quot;ILogger{Startup}&quot;/&gt;.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      private static IServiceCollection AddCrossCuttingServices(this IServiceCollection services, IConfiguration configuration, ILogger logger)
      {
          logger.LogInformation(&quot;----  Adding Cross-Cutting components  ----&quot;);
  
          services.AddSql(configuration, logger);    //This is a extension methods, that adds Sql services (which are also completely custom) to the application.
  
          logger.LogInformation(&quot;----  Successfully added Cross-Cutting components  ----&quot;);
  
          return services;
      }
      #endregion Private Methods
  }
  </code></pre>
  <p><strong>Question-</strong> How do I access Logger in ConfigureServices method of Startup.cs so that I can pass it to other custom extension methods that add custom services to the container?</p>
  <p><strong>NOTE:</strong> Solution requested for Microsoft and Serilog Logger (optional).</p>
  <p><strong>Update 1:</strong> As @MathewBeck pointed out in his 2nd comment, I already have the following structure of <code>Program.cs</code>:</p>
  <pre><code>public class Program
  {
      #region Public Methods
      /// &lt;summary&gt; Main method, the entry-point of the application. &lt;/summary&gt;
      /// &lt;param name=&quot;args&quot;&gt;The args &lt;see cref=&quot;string[]&quot;/&gt;&lt;/param&gt;
      public static void Main(string[] args)
      {
          var host = CreateHostBuilder(args).Build();
          var logger = host.Services.GetRequiredService&lt;ILogger&lt;Program&gt;&gt;();
          logger.LogInformation(&quot;Host created.&quot;);
  
          host.Run();
      }
  
      /// &lt;summary&gt; Creates the host builder. &lt;/summary&gt;
      /// &lt;param name=&quot;args&quot;&gt;The args &lt;see cref=&quot;string[]&quot;/&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
          Host.CreateDefaultBuilder(args)
              //.ConfigureLogging(loggingBuilder =&gt;
              //{
              //    loggingBuilder.ClearProviders();
              //    loggingBuilder.AddConsole();
              //    loggingBuilder.AddDebug();
              //    loggingBuilder.AddEventLog();
              //    loggingBuilder.AddEventSourceLogger();
              //})
              .ConfigureWebHostDefaults(webBuilder =&gt;
              {
                  webBuilder.UseStartup&lt;Startup&gt;();
                  webBuilder.UseConfiguration(GetConfiguration());
              });
      #endregion Public Methods
  
      #region Private Methods
      /// &lt;summary&gt; Gets the configuration. &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      private static IConfiguration GetConfiguration()
      {
          var env = Environment.GetEnvironmentVariable(&quot;ASPNETCORE_ENVIRONMENT&quot;) ?? &quot;Development&quot;;
          var builder = new ConfigurationBuilder()
              .AddJsonFile(&quot;appsettings.json&quot;, true, true)
              .AddJsonFile($&quot;appsettings.{env}.json&quot;, true, true);
  
          return builder.Build();
      }
      #endregion Private Methods
  } 
  </code></pre>