title: Setting up Serilog to write logs to SQL Server db table
body: <p>I'm trying to set up Serilog to write logs to my db table. </p>
  
  <p>Here's the breakdown of what I did to try and set up logging to SQL Server. </p>
  
  <p>1) Install the Nuget Packages, Serilog and Serilog.Sinks.MSSqlServer</p>
  
  <p>2) I set up a class called Logger.cs that houses all the configuration for Serilog: </p>
  
  <pre><code>public static class Logger
  {
      private static readonly ILogger _Logger;
  
      static Logger()
      {
  
          var connStr = "data source=DESKTOP-BLAH;Initial Catalog=DBNAME;Trusted_Connection=True;";
  
          _Logger = new LoggerConfiguration()
              .WriteTo.MSSqlServer(connStr, "ApplicationLogs", 
              columnOptions: GetSqlColumnOptions(), restrictedToMinimumLevel: LogEventLevel.Debug, batchPostingLimit: 1)
              .CreateLogger();
  
          Serilog.Debugging.SelfLog.Enable(msg =&gt; Debug.WriteLine(msg));
  
      }
  
      public static ColumnOptions GetSqlColumnOptions()
      {
          var colOptions = new ColumnOptions();
  
          colOptions.Store.Remove(StandardColumn.Properties);
          colOptions.Store.Remove(StandardColumn.MessageTemplate);
          colOptions.Store.Remove(StandardColumn.Message);
          colOptions.Store.Remove(StandardColumn.Exception);
          colOptions.Store.Remove(StandardColumn.TimeStamp);
          colOptions.Store.Remove(StandardColumn.Level);
  
          colOptions.AdditionalDataColumns = new Collection&lt;DataColumn&gt;
          {
              new DataColumn{DataType = typeof(DateTime), ColumnName = "LogTimeStamp"},
              new DataColumn{DataType = typeof(Int32), ColumnName = "RecordNum"},
              new DataColumn{DataType = typeof(string), ColumnName = "ComputerName"},
              new DataColumn{DataType = typeof(DateTime), ColumnName = "ProcessTimeStamp"},
              new DataColumn{DataType = typeof(string), ColumnName = "LogGroup"},
              new DataColumn{DataType = typeof(string), ColumnName = "Type"},
              new DataColumn{DataType = typeof(Int32), ColumnName = "EventId"},
              new DataColumn{DataType = typeof(string), ColumnName = "UserId"},
              new DataColumn{DataType = typeof(Int32), ColumnName = "Line"},
              new DataColumn{DataType = typeof(string), ColumnName = "Description"},
              new DataColumn{DataType = typeof(string), ColumnName = "Source"},
              new DataColumn{DataType = typeof(string), ColumnName = "Data"},
              new DataColumn{DataType = typeof(DateTime), ColumnName = "AddTimestamp"},
              new DataColumn{DataType = typeof(string), ColumnName = "DeviceID"}
  
          };
          return colOptions;
      }
  
  
      public static void WriteError(ApplicationLog infoToLog)
      {
          try
          {        
              _Logger.Error(LogEventLevel.Error,
                      "{LogTimestamp}{RecordNum}{ComputerName}{ProcessTimeStamp}{LogGroup}" +
                      "{Type}{EventId}{UserId}{Line}" +
                      "{Description}{Source}{Data}{AddTimestamp}{DeviceID}",
                      infoToLog.LogTimestamp, infoToLog.RecordNum,
                      infoToLog.ComputerName, infoToLog.ProcessTimestamp,
                      infoToLog.LogGroup, infoToLog.Type,
                      infoToLog.EventId, infoToLog.UserId,
                      infoToLog.Line, infoToLog.Description,
                      infoToLog.Source, infoToLog.Data,
                      infoToLog.AddTimestamp, infoToLog.DeviceId);
          }
          catch(Exception ex)
          {
  
          }
      }
  }
  </code></pre>
  
  <p>3) Then I believe I should be good to go and I set it up like this: </p>
  
  <pre><code>ApplicationLog appLog = new ApplicationLog()
  {
      LogTimestamp = DateTime.Now,
      RecordNum = 1,
      ComputerName = Environment.MachineName,
      ProcessTimestamp = DateTime.Now,
      LogGroup = "10",
      Type = "T",
      EventId = 23,
      UserId = Environment.UserName,
      Line = 0,
      Description = "Duplicate Failed to Upload",
      Source = "FooController.cs",
      Data = "DBNAME",
      AddTimestamp = DateTime.Now,
      DeviceId = "15"
  
  };
  
  Logger.WriteError(appLog);
  </code></pre>
  
  <p>4) Created table in my DB already and just waiting for the data from logging to populate. </p>
  
  <p>However, it seems like it still doesn't get saved in my SQL Server db table. </p>
  
  <p>I'm wondering if I missed any steps or what I did wrong for the setup here? </p>