Structured Logging with Serilog

As a .Net developer you can choose from a variety of logging frameworks. All are more or less built to mitigate the shortcomings of the built in tracing framework. While System.Diagnostics.Trace has some helpful concepts, it misses many basic features most other logging frameworks can offer out of the box.

Serilog is not just another logging framework. The simple API is a good reason to use it, but it starts shining when it comes to the structured logging capabilities. The whole framework is built around the idea that log messages should be more than a collection of strings. Writing JSON to a variety of output providers (called sink) will enable you to get meaningful objects (and not just strings) that can be used to analyse the state of your application in much more detail.

The documentation can be found on Serilog.net and is only a quick read. The examples are easy to follow and the API is straight forward when you ever used another logging framework. To install Serilog in your application you simply use NuGet to fetch the package:

Serilog_Nuget_t

This post is part of the Improve Your Log Messages series. You can find the other parts here:

 

Start simple

If you start with a new application you can use Serilog from the beginning. However, should you have an application that already writes log messages you need to be careful. Don’t just change the format of the log messages. Your co-workers or your operations team may have written special tools to parse the log messages. In this case let Serilog write the messages in the same format as your current logger. The new and improved output will go to a different file and when you have enough evidence that this approach is better, you simply stop writing the messages in the old format.

You can use a fluent interface to configure Serilog. All you have to do is to call the appropriate methods to configure the instance of your Logger and then call the static Log class to write your messages:

If you run this code you will get the following output in your console:

 

Add Structure

To add structure you will need an additional place to store the data. In this example we use a sink (a place to put data) that writes a JSON formatted output to the file “mylog.txt”. All it needs is a bit more configuration. The call to the logger stays the same:

The console output will still produce the exact same messages, but the big addition is in the log file. The messages there are formatted as JSON objects and offer more context than the console:

 

Next

While it may be nice to produce a JSON formatted message, it doesn’t make it easier to pinpoint the source of a problem. The next step is to store the data in a way you can easily search for a specific property. And since we already have JSON documents we are going to use RavenDB for this task.

1 thought on “Structured Logging with Serilog”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.