An Overview of Structured Logging with Serilog

Traditional logging techniques (such as writing lines of text to files) results in the loss of structure and semantics.

Structured logging enables richer, more semantic, structured log data to be created. But more importantly queried.

Serilog is an open source logging library that facilitates this structured logging.

Serilog can be used in the traditional way, for example writing to files, but it is designed to allow capture of structured data.

There are multiple provided “sinks” that route log data to a store; from unstructured data stores such as text files (single and rolling) to structured stores such as RavenDB. Multiple sinks can be configured so the same log event is written to both stores.

Writing to multiple sinks with Serilog

To create structured data, the logging code looks slightly different from traditional (unstructured) logging code. Once the logger is configured, to log an information level event we could write something like the following:

Log.Information("Log in succeeded for {UserId}. Authentication service took {LogInMilliseconds}.", userId, responseTime);

Here, a log event is being written that contains two pieces of structured, sematic data: UserId and LogInMilliseconds. If this message is written to an unstructured source (such as text file) then a simple string will be written. If a structured sink is used then the individual data items will be stored as well.

This means that this structured information can be richly queried, for example queries such as: “show me all the logins for user 42 where the login time exceeded 200ms” or “show me all the logins that took more than 1000ms”.

Check out the Serilog site for more info or my Modern Structured Logging With Serilog and Seq Pluralsight course.

You can start watching with a Pluralsight free trial.


Comments (1) -

  • Thomas Rolseth

    3/23/2020 4:45:03 PM | Reply

    I watched your 'Modern Structured Logging with Serilog and Seq' course on Pluralsight which was excellent.  I have a couple of questions I was hoping you could help with:
    1)  I have a .NET web api using Serilog and Seq and it is working great.  When the client app makes calls to it it sends the user name and a session id in the request header.  How can I capture this as a property for every log entry?
    2) Additionally, I'd like to capture the controller and service name for each log entry
    3) Some of the Seq apps -- Serilog.Extras.AppSettings for one -- are not compatible with .netcore.  Any workarounds for this?  

    Thanks, Tom

Pingbacks and trackbacks (1)+

Add comment