title: .NET Core Worker Serilog logging inside Program.cs
body: <p>In .Net Core 3.1 worker I am trying to log using serilog inside my <code>Program.cs</code> file.
  Serilog config is inside <code>appsettings.json</code>, and <code>CreateHostBuilder</code> is like</p>
  <pre><code>public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
      Host.CreateDefaultBuilder(args)
          .ConfigureHostConfiguration(builder =&gt;
          {
              builder.AddJsonFile(&quot;appsettings.json&quot;, optional: false, reloadOnChange: true);
              builder.AddJsonFile(&quot;appsettings.Development.json&quot;, optional: true);
          })
          .UseSerilog((hostContext, loggerConfig) =&gt; {
              loggerConfig.ReadFrom.Configuration(hostContext.Configuration);
          })
          .ConfigureServices((hostContext, services) =&gt;
          {
              // configuring some services here, and I want to log message to serilog
  
          })
          .UseWindowsService();
  </code></pre>
  <p>}</p>
  <p>I have managed to use Serilog inside Main class using</p>
  <pre><code>public static void Main(string[] args)
  {
      // CreateHostBuilder(args).Build().Run(); // inital code
      var host = CreateHostBuilder(args).Build();
      // get logger instance
      var logger = host.Services.GetService&lt;ILogger&lt;Program&gt;&gt;();
  
      try
      {
          logger.LogInformation(&quot;Worker started succesfully.&quot;);
          host.Run();
      }
      catch (Exception ex)
      {
          logger.LogCritical(ex, &quot;Starting worker failed!&quot;);
          Environment.Exit(160);
      }            
  }
  </code></pre>
  <p>I would like to know how is it possible to make Serilog logs inside <code>ConfigureServices</code>.
  I have used in <code>ConfigureServices</code></p>
  <pre><code>            var provider = services.BuildServiceProvider();
              var logger = provider.GetService&lt;ILogger&lt;IHostBuilder&gt;&gt;();
  </code></pre>
  <p>but after that, I am getting multiple files - two files per debugging session with F5, and I want to log to one (per day, that is configured):</p>
  <p><a href="https://i.stack.imgur.com/iwa9m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iwa9m.png" alt="enter image description here" /></a></p>