Search code examples
sql-serverserilog

Outputting JSON with the MSSqlServer Serilog sink


How can I configure the MSSqlServer Serilog sink to output the properties as JSON instead of XML? I tried passing a RenderedCompactJsonFormatter to the formatProvider parameter of LoggerConfigurationMSSqlServerExtensions.MSSqlServer(), but that expects an IFormatProvider.


Solution

  • You could instead use the LogEvent column and pull the Properties object out of the JSON in that column value.

    var colOpts = new ColumnOptions();
    colOpts.Store.Add(StandardColumn.LogEvent);
    colOpts.Store.Remove(StandardColumn.Properties);
    var log = new LoggerConfiguration()
                  .WriteTo.MSSqlServer(connString, "Logs",columnOptions:colOpts)
                  .CreateLogger();
    

    Note, I also removed the Properties column so as not to duplicate data in the table.