ASP.NET Core 2.0 Structured Logging


How to work with structured logging in ASP.NET Core and Serilog.


Starting from the previous post on logging, add NuGet packages:

  • Serilog.AspNetCore
  • Serilog.Sinks.Literate
  • Serilog.Sinks.Seq

In Program.cs configure Serilog using its LoggerConfiguration class and storing an instance of ILogger (returned by CreateLogger) in Serilog’s static Log class:

Using the ILogger is same as our previous post, however, with Serilog we can do structured logging:

Running the application will show messages in Console window:

Serilog - Output


Structured logging is a technique to include semantic information as part of the messages being logged. This helps ‘machine readability’ of these messages and tools can be written to analyse raw log messages and produce interesting information.

Serilog uses message template, similar to string.Format() in .NET. Few interesting aspects of template syntax are:

  • Use {} to enclose property names e.g. {developer} in above solution. These will be stored as metadata and can be queried using structured data storage (e.g. Seq, Azure).
  • Use @ to preserve object structure e.g. in solution above the anonymous object is serialised into JSON representation.


In Serilog enrichers are used to attach information with every log event that can then be used by structured data storage (e.g. Seq, Azure) for viewing and filtering. A simple way to do this is by using .Enrich.WithProperty() when configuring Serilog:


As we saw in the previous post, a category can be attached to the logged messages, which normally is the fully qualified name of the class. This information could be used by structured data storage (e.g. Seq, Azure). Serilog provides this mechanism by attaching Context via ForContext() method:


Sinks in Serilog refer to destination of log messages e.g. file, database or console (in our example). There are several sinks available (refer to link below). I’ll use Seq as an example sink to show how all the metadata we’ve added is available in a structured storage:

Serilog - Output2

Notice how data we added via enricher, context and custom object appears as key/value pairs. This can now be used for filtering data and creating dashboards within Seq.

Note: Refer to Seq website (link below) for installation instructions (it’s very simple!).

Source Code


Useful Links

Serilog Sinks:

Leave a Reply