title: How to switch between Serilog and another logger in .Net Core through configuration?
body: <p>I need to be able to switch between Serilog and another logger, MyLog. Currently I am switching by comment/uncomment code as the following:</p>
  <pre><code>    public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
          Host.CreateDefaultBuilder(args)
              .ConfigureWebHostDefaults(webBuilder =&gt;
              {
                  webBuilder.UseStartup&lt;Startup&gt;();
              })
              //// === to switch between Serilog and MyLogger comment/uncomment code below ===
              //.UseSerilog((hostContext, loggerConfig) =&gt;
              //    loggerConfig.ReadFrom.Configuration(hostContext.Configuration));
              .ConfigureLogging((hostBuilderContext, logging) =&gt;
               {
                   logging.AddMyLogger(options =&gt;
                   {
  hostBuilderContext.Configuration.GetSection(MyLoggerOptions.LogOptions).Bind(options);
                   });
               });
  }
  </code></pre>
  <p>This works, but I prefer to make it controlled by a appsettings.json setting, say, Logger. So here is my appsettings.json file:</p>
  <pre><code>{
    &quot;Logger&quot;: &quot;Serilog&quot;, // Use Serilog or MyLog
  
    &quot;Logging&quot;: {
      &quot;LogLevel&quot;: {
        &quot;Default&quot;: &quot;Debug&quot;,
        &quot;System&quot;: &quot;Information&quot;,
        &quot;Grpc&quot;: &quot;Information&quot;,
        &quot;Microsoft&quot;: &quot;Information&quot;,
        &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Information&quot;
      },
      &quot;MyLog&quot;: {
        &quot;Options&quot;: {
          &quot;FolderPath&quot;: &quot;logs&quot;,
          &quot;FilePath&quot;: &quot;Svr_{date}.log&quot;
        },
        &quot;LogLevel&quot;: {
          &quot;Default&quot;: &quot;Information&quot;,
          &quot;Microsoft&quot;: &quot;Error&quot;,
          &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Error&quot;
        }
      }
    },
  
    &quot;Serilog&quot;: {
      &quot;MinimumLevel&quot;: &quot;Verbose&quot;,
      &quot;Override&quot;: {
        &quot;Microsoft.AspNetCore&quot;: &quot;Warning&quot;
      },
      &quot;WriteTo&quot;: [
        {
          &quot;Name&quot;: &quot;Console &quot;
        },
        {
          &quot;Name&quot;: &quot;File&quot;,
          &quot;Args&quot;: {
            &quot;path&quot;: &quot;Logs\\Serilog_Server-.log&quot;,
            &quot;formatter&quot;: &quot;Serilog.Formatting.Json.JsonFormatter, Serilog&quot;,
            &quot;rollingInterval&quot;: &quot;Day&quot;
          }
        }
      ]
    }
  }
  </code></pre>
  <p>and here is the code where I try to set log depending on the Loger setting:</p>
  <pre><code>public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
      Host.CreateDefaultBuilder(args)
          .ConfigureWebHostDefaults(webBuilder =&gt;
          {
              webBuilder.UseStartup&lt;Startup&gt;();
          })
  
          .ConfigureLogging((hostBuilderContext, logging) =&gt;
          {
              string loger = hostBuilderContext.Configuration.GetValue&lt;String&gt;(&quot;Logger&quot;);
              if (loger == &quot;MyLog&quot;)
              {
                  logging.AddMyLogger(options =&gt;
                  {
                      hostBuilderContext.Configuration.GetSection(MyLoggerOptions.LogOptions).Bind(options);
                  });
              }
              else if (loger == &quot;Serilog&quot;)
              {
                  logging.AddSerilog(); // here is the problem. Serilog doesn't write.
              }
          });
  </code></pre>
  <p>The problem of this code is the Serilog (MyLog is fine) - it doesn't write at all. My guess is that, since I don't have a way to pass in the configuration object, it might not get configured properly. But how do I fix it? or is there a better way to do this?</p>
  <p><strong>UPDATE</strong></p>
  <p>Tried what Camilo suggested as the following with an error. How do I get the configuration object from here?</p>
  <p><a href="https://i.stack.imgur.com/ollBe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ollBe.png" alt="enter image description here" /></a></p>