title: .Net Core 3 dependency injection of a Serilog
body: <p>I am doing a Windows Service where I use Serilog</p>
  
  <p>this is fine in the worker:</p>
  
  <pre><code>public class Worker: BackgroundService
  {
      private readonly ILogger&lt;Worker&gt; _logger;
  
      private readonly IConfiguration _configuration;
      private readonly string _connectionString;
      private bool _serviceIsStarting = true;
  
      public Worker(ILogger&lt;Worker&gt; logger, IConfiguration iConfig)
      {
          _logger = logger;
          _configuration = iConfig;
          _connectionString = _configuration.GetConnectionString("DestinationDatabase");
      }
  
      //....
  
  }
  </code></pre>
  
  <p>My Program.cs contains this:</p>
  
  <pre><code>public static IHostBuilder CreateHostBuilder(string[] args)
  {
      return Host.CreateDefaultBuilder(args)
          .UseWindowsService()
          .UseSerilog((hostingContext, loggerConfiguration) =&gt; loggerConfiguration
              .ReadFrom.Configuration(hostingContext.Configuration))
          .ConfigureServices((hostContext, services) =&gt;
          {
              services.AddHostedService&lt;Worker&gt;();
          });
  }
  </code></pre>
  
  <p>Everything is fine but then I want to inject a logger to my custom class that the Worker instance uses:</p>
  
  <pre><code>public class DataAccess
  {
      private readonly string _connectionString;
  
      private readonly ILogger&lt;DataAccess&gt; _logger;
  
      public DataAccess(ILogger&lt;DataAccess&gt; logger, string connectionString)
      {
          _connectionString = connectionString;
          _logger = logger;
      }
  
      //.....
  }
  </code></pre>
  
  <p>My problem is that I do not know how to instantiate my class DataAccess in the worker or to the dependency injection here.</p>
  
  <p>Here is a [simplified] verison of my class:</p>
  
  <pre><code>using Microsoft.Extensions.Logging;
  using Dapper;
  
  public class DataAccess
  {
      private readonly string _connectionString;
      private readonly ILogger&lt;DataAccess&gt; _logger;
  
      public DataAccess(ILogger&lt;DataAccess&gt; logger, string connectionString)
      {
          _logger = logger;
          _connectionString = connectionString;
      }
      public void SaveInspection(InspectionElm inspection)
      {
          using IDbConnection connection = new System.Data.SqlClient.SqlConnection(_connectionString);
          connection.Open();
  
          var p = new DynamicParameters();
          p.Add("@RodNumber", inspection.RodNumber);
              p.Add("@InspectionDate", Convert.ToDateTime(inspection.InspectionDate));
              p.Add("@MeasurementEnd", Convert.ToDateTime(inspection.MeasurementEnd));
              p.Add("@InspectionResultPass", Convert.ToBoolean(inspection.InspectionResultPass));;
  
          connection.Execute("InsertTindasjaMasterRecord", p, commandType: CommandType.StoredProcedure);
  
          _logger.LogInformation("Log something...");
      }
  
      public void InitMesureResultTypes()
      {
          using IDbConnection connection = new System.Data.SqlClient.SqlConnection(_connectionString);
          connection.Open();
          connection.Execute("InitMeasureResultType", null, commandType: CommandType.StoredProcedure);
  
          _logger.LogInformation("Log something...");
      }
  }
  </code></pre>