title: Injecting existing logger in the Hosted Service
body: <p>I am having the project in .net core 3.1. I have configured to use the SeriLog as logger.</p>
  <p>One of the service <code>MqttComunicationClient</code> is having logger injected to constructor which is using the serilog configuration. Here <code>ILogger</code> is <code>Microsoft.Extensions.Logging</code></p>
  <pre><code>public MqttCommunicationClient(
              IMqttCommunicationConfiguration mqttConfiguration,
              ILogger logger,
              )
          {
              this.mqttConfiguration = mqttConfiguration;
              this.logger = logger;            
          }
  </code></pre>
  <p>From the <code>Init</code> method of this class I am initializing the custom written MQTT API.</p>
  <pre><code>public void Initialize()
          {
              var mqttClientId = $&quot;{DefaultClientIdPattern}{Environment.MachineName}&quot;;
  
              serviceHost = Host.CreateDefaultBuilder()
                    .UseMQTTApi(opt =&gt;
                    {                          
                        opt.UserName = &quot;test&quot;;
                        opt.Password = &quot;test&quot;;
                    })
                    .ConfigureServices(services =&gt;
                    {
                        services.AddSingleton(logger);                           
                    })
                    .ConfigureHostConfiguration(c =&gt; { c.Build(); })
                    .Start();
  
              managedMqttClient = serviceHost.Services.GetRequiredService&lt;IManagedMqttClient&gt;();
          }
  </code></pre>
  <p>My MQTT Api is having the Handlers which will be triggered on the message received on specified topic.</p>
  <p>One of the handler is having following constructor.</p>
  <pre><code>public ConnectionStatusHandler(
           ILogger&lt;ConnectionStatus&gt; logger,
           IDeviceCommunications deviceCommunications)         
          {
              this.logger = logger;
              this.deviceCommunications = deviceCommunications;
          }
  </code></pre>
  <p>Now the issue is when I debug my <code>MqttComunicationClient</code> the logger is configured as the <code>SeriLog</code>. As per the requirment and configuration <code>logger.LogInformation()</code> used inside this class is logging to the log file.</p>
  <p><a href="https://i.stack.imgur.com/7zIaF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7zIaF.png" alt="enter image description here" /></a></p>
  <p>When the <code>ConnectionStatusHandler</code> is called the <code>logger</code> changes to the <code>Microsoft.Extensions.Logging.Logger</code> Logger.</p>
  <p><a href="https://i.stack.imgur.com/SBNod.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SBNod.png" alt="enter image description here" /></a></p>
  <p>The logger used in <code>ConnectionStatusHandler</code> class is only logging to the console and not in the file.</p>
  <p>How can I pass on the same serilog configured <code>logger</code> object from <code>MqttComunicationClient</code> to the <code>ConnectionStatusHandler</code> ?
  Do I need some additional settings? I am a new to this so any help is appreciated.</p>
  <p><strong>Edits</strong>
  Code which is configuring the <code>serilog</code> as logger</p>
  <pre><code>public IServiceProvider CreateServiceProvider(string basePath)
    {
       var configuration = GetConfiguration(basePath);
       
       var logger = CreateLogger(configuration);
       var services = new ServiceCollection();
       services.AddSingleton(logger);
       return services.BuildServiceProvider();
    }
  
  protected virtual ILogger CreateLogger(IConfiguration configuration)
    {
       return LogBuilder.CreateLogger(null, configuration);
    }
    
  // Serilog configured here
  public static ILogger CreateLogger(string categoryName, IConfiguration configuration, Action&lt;string&gt; selfLogAction = null)
    {
       var serilogLogger = new LoggerConfiguration()
                             .ReadFrom.Configuration(configuration)
                             .CreateLogger();
  
       using (var serilogLoggerProvider = new SerilogLoggerProvider(serilogLogger))
       {
          var logger = serilogLoggerProvider.CreateLogger(categoryName);
  
          if (selfLogAction != null)
          {
             Serilog.Debugging.SelfLog.Enable(selfLogAction);
          }
  
          return logger;
       }
    } 
  
  // This method reads the configuration from the config json file.
  protected virtual IConfiguration GetConfiguration(string basePath)
    {
       StringBuilder configLog = null;
       var mainConfiguration = new ConfigurationBuilder()
          .SetBasePath(basePath)
          .AddJsonFile(&quot;mytest.config.json&quot;)
          .Build();
       var builder = new ConfigurationBuilder();
       return BuildConfiguration(builder, mainConfiguration, basePath, configLog)
          .Build();
    }
    
  </code></pre>