JSON is a popular format for diagnostic logs because it’s so easy to manipulate and analyze. Every now and then, I hear from someone who’d like to pipe a JSON log file back through Serilog so that they can send the events to one of the many storage options that Serilog can target.

This has never been as easy as it sounds - the Serilog event model isn’t really designed for it.

It’s not impossible either, though, and this post is a quick heads-up to point you to Serilog.Formatting.Compact.Reader, a tiny library that reads compact JSON format log files back into Serilog events.

Given a log file from Serilog.Formatting.Compact.CompactJsonFormatter:

{"@t":"2016-10-12T04:46:58.0554314Z","@mt":"Hello, {@User}","User":{"Name":"nblumhardt","Id":101}}
{"@t":"2016-10-12T04:46:58.0684369Z","@mt":"Number {N:x8}","@r":["0000002a"],"N":42}
{"@t":"2016-10-12T04:46:58.0724384Z","@mt":"Tags are {Tags}","@l":"Warning","Tags":["test","orange"]}
{"@t":"2016-10-12T04:46:58.0904378Z","@mt":"Something failed","@l":"Error", "@x":"Systemp.DivideBy…"}

A LogEventReader pulls events from the file one-by-one:

var log = new LoggerConfiguration()

var reader = new LogEventReader(File.OpenText("log.json"));

LogEvent evt;
while (reader.TryRead(out evt))

Piping them back into Serilog, as in the example, produces just what you’d see if the events went directly into the logging pipeline. You can pipe the events to a log collector, database, a flat file in a different format, or even Rx.


For now, I’ve put this up on NuGet/GitHub under my personal account just to get the code out there. Depending on the level of interest we might end up trying to support this “officially” alongside the formatters in the Serilog project. If you find it useful, make sure to drop us a line and let us know.

Update: the package is now hosted and supported under the Serilog GitHub organization.