Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 655
Thomas Mueller Avatar asked Sep 13 '25 19:09

Thomas Mueller


1 Answers

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.

like image 70
squillman Avatar answered Sep 16 '25 10:09

squillman