title: How to save Serilog event logs before a database is created by the app?
body: <p>I created a logger successfully using Serilog but I can't determine how to tell if the SQL Server database I'm logging to exists before I create the logger.  The goal is to create the logger before this line in my Main function so I get all of the startup logs:</p>
  
  <p><code>var host = CreateHostBuilder(args).Build();</code></p>
  
  <p>However, if I do that, and the database doesn't exist yet, the logging table won't get created the first time I start the application. It will get created the second time I start the application because I'm creating and seeding the database after that line of code.</p>
  
  <p>I want to create the logger before CreateHostBuilder runs if the database exists or create the logger after CreateHostBuilder and SeedDatabase run if the database doesn't exist yet. I can't run CreateLogger() twice either or I'll get an exception.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public static void Main(string[] args)
  {
      try
      {
          // If I create the logger here I'll see all the startup logs
          // if the database exists. If the database doesn't exist yet the 
          // logging table won't get created until the second time I startup the app.
          CreateLogger();
  
          Log.Information("Starting Up");
  
          var host = CreateHostBuilder(args).Build();
  
          SeedDatabase(host);
  
          // If I create the logger here I don't get all of the startup logs but
          // it ensures that if the database hasn't been created and seeded yet the 
          // logging table is still created.
          CreateLogger();
  
          host.Run();
      }
      catch (Exception exception)
      {
          Log.Fatal(exception, "Application start-up failed");
      }
      finally
      {
          Log.CloseAndFlush();
      }
  }
  
  public static void CreateLogger()
  {
      Log.Logger = new LoggerConfiguration()
          // Add/Update configuration settings in appsettings.json.
          // Don't add them here.
          .ReadFrom.Configuration(Configuration)
          .Enrich.FromLogContext()
          .CreateLogger();
  }
  
  </code></pre>
  
  <p><strong>UPDATED</strong> appsettings.json after adding file logging:</p>
  
  <pre><code>
  "Serilog": {
      "MinimumLevel": "Information",
      "WriteTo": [
        {
          "Name": "MSSqlServer",
          "Args": {
            "connectionString": "DbContext",
            "tableName": "EventLog",
            "autoCreateSqlTable": true
          }
        }
      ],
      "WriteTo:Async": {
        "Name": "Async",
        "Args": {
          "configure": [
            {
              "Name": "File",
              "Args": {
                "path": "../../EventLogs/",
                "rollingInterval": "Day"
              }
            }
          ]
        }
      }
    }
  </code></pre>