title: Net Core 2 Serilog inject from Console App to Webhost
body: <p>I would like to know how to manage and best way to bring Serilog instance from a console app to a StartUp class that configures a webhost.</p>
  
  <p>Scenario:</p>
  
  <p>I ve got a Main class from a console app where I configure a logger (serilog). 
  Namespace 1</p>
  
  <pre><code> static void Main(string[] args)
      {
          DateTime Date = new DateTime.Now;
             Log.Logger = = new LoggerConfiguration()
           .MinimumLevel.Debug()
           .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
           .Enrich.FromLogContext()
           .WriteTo.Console()
           .WriteTo.File($"Log-Startup-{Date}.txt", rollingInterval: 
  RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff 
  zzz}")
           .CreateLogger();
  
  [...]
  
  `StaticLauncherClass.Launch(args, config, Log.Logger)`;
  </code></pre>
  
  <p>Notice after this I'm just doing a call to static class method and I pass the Logger.</p>
  
  <p>Static class is supposed to configure a Webhost as typical webhost app in net core 2. Launch Method does something like.  Namespace2</p>
  
  <pre><code>      public static IConfiguration Config { get; private set; }
  
  
  public static void Launch(string[] args, IConfigurationSection config, 
  ILogger logger)
          {
              Log.Logger = logger;
              Config = config;
              CreateWebHostBuilder(args).Build().Run();
          }
  
  
        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
          {
              Log.Information("Starting web host");
    Serilog.ILogger _logger = Log.Logger;
              try
              {
                  return CreateWebhostBuilder(args)
                          // Use Serilog
                          // Evitar inicio de hospedaje
                          .UseSerilog()
                          .UseSetting(WebHostDefaults.PreventHostingStartupKey, 
  "true")
                      .ConfigureServices(svc =&gt;
  {
      svc.AddSingleton(_logger);
  })
                          .UseStartup&lt;Startup&gt;();
              }
  [...]
  }
  </code></pre>
  
  <p>In same class, finally CreateWebhostBuilder(args) configure webhost </p>
  
  <pre><code> var builder = new WebHostBuilder()
             .UseKestrel()
              .UseContentRoot(Directory.GetCurrentDirectory())
              .ConfigureAppConfiguration((hostingContext, config) =&gt;
              {
                  var env = hostingContext.HostingEnvironment;
  
                  if (env.IsDevelopment())
                  {
                      var appAssembly = Assembly.Load(new 
  AssemblyName(env.ApplicationName));
                      if (appAssembly != null)
                      {
                          config.AddUserSecrets(appAssembly, optional: true);
                      }
                  }
  
                  if (args != null)
                  {
                      config.AddCommandLine(args);
                  }
                  config.SetBasePath(Directory.GetCurrentDirectory());
                  // Add settings to bootstrap EF config.
              })
  
              .UseIISIntegration()
              .UseDefaultServiceProvider((context, options) =&gt;
              {
                  options.ValidateScopes = 
  context.HostingEnvironment.IsDevelopment();
              });
  
  
      return builder;
  </code></pre>
  
  <p>And then my question si how with this scenario I can <strong>use my Logger in Startup used class. My intention is to globalize logger the best and easier way posible. What solutions you purpose?</strong></p>
  
  <pre><code> internal class Startup {
  ???
  ???
   public Startup(IConfiguration config, IHostingEnvironment environment)
      {       
          ???
      }
  
     public void ConfigureServices(IServiceCollection services)
      {
          Console.WriteLine("Startup");
          Log.Information("Startup: Configure services");
      }
  }
  </code></pre>
  
  <p>Startup class is writing Console.WriteLine content but does not write Log.Information content.
  Edit: Actual status, bring service to Startup but does not apply configuration provided to log in file. I think it acts just by default.</p>