Dimitris Paxinos

.net and other programming stories

Centralized & Structured .NET Logging with Serilog & Seq Part 2

Contextual Logging

After Part1 and the introduction to the basics of Serilog and Seq, we are now going to examine how we can add some context to our logging events.

When we say Context, we mean information that is implicitly added to the log event without including it in the actual log event submission.

Let’s see how this works…

Create Contextual Logger

In order to enable our Logger to ‘carry’ this context, we have to include the .Enrich.FromLogContext() part when we create it:

Add a property

Let’s add a property to the context using the static PushProperty method of the LogContext class that returns an IDisposable. As you can see, the context is only added to the call which is done within the using statement:

Add a structured object

One of the biggest advantages of Serilog is also available for our context. Structured objects can also be included. We will use the Person class (like in Part 1) in order to create an object to be added to our context:

Add a dynamic object

Dynamic objects can also be included. Imagine packing a number of properties into a dynamic object and adding this to your log event context:

Include the class name

I think this is straightforward:

We will get the same event in both cases with the class name being assigned to the SourceContext property:

Add Context using Custom Enrichers

There is one more way to add context to your logs by using concrete classes in order to encapsulate the logic you would like to include. All you need to do is implement the ILogEventEnricher interface like in the following example:

You can then add your ILogEventEnricher to your context:



Comments

Centralized & Structured .NET Logging with Serilog & Seq Part 1

TL;DR

Serilog and Seq are two open-source tools that can help you introduce a centralized and structured logging mechanism in your .NET application really quickly. Has a quick learning curve and can substantially improve your logging, monitoring and error reporting workflow.

The Problem

The traditional approach of plain-text file based logging has some very important drawbacks. Log files have to be physically retrieved first and their contents have to subsequently be parsed into a structured form so that we can draw some useful conclusions.

Retrieving the log file when dealing with a web application is not that hard you might say. What happens if you have a client-server application? How are you going to get the client log files?

In the enterprise world, where LOB applications run in the intranet, the helpdesk could remotely connect to the computer where the client is running and retrieve the log file. But still… this could be better…

Serilog & Seq for the win

In this blog post, we are going to start examining a different logging approach which offers:

  • Structured meaningful logging objects
  • A central repository for collecting these logs
  • A neat web interface with a queryable log list

using two open-source tools, Serilog and Seq. The goal is to get from reading to trying it out very fast.

What is Serilog?

Serilog is a logging .NET library like Log4Net which:

  • Logs structured event data. (complete JSON objects)
  • Has a very clean and straightforward syntax.
  • Can write the logs to many different places (the so-called Sinks) like files, the console, DBs etc.

What is Seq?

Seq is a complete solution for storing, displaying and querying structured event data which:

  • Can be installed in a matter of minutes
  • Works out of the box with Serilog
  • Has an API for the logs to be submitted
  • Has a web interface with a queryable log list
  • Has some other cool features like retention and event triggers.

Let’s try it out!

Installing Seq

  1. Get Seq from here and do a default install
  2. You can now access the web interface here: http://localhost:5341/#/events

Serilog Basics

Fire up Visual Studio, create a console application and…

NuGet packages

Add the following two NuGet packages:

  • Serilog
  • Serilog.Sinks.Seq
Create Logger

It is amazingly easy to create a logger that would write to our Seq server:

Publish a log event - Log Event Levels

Publishing a log is as easy as using one line of code. You can use any of the following logging levels:

And this is what you would see on the Seq web interface:

Attaching Parameters

Let’s now attach some parameters to our log event and make it a bit more meaningful:

The two parameters will be included in the logging event object as two additional properties with property names as declared in the log event string, {FirstName} and {LastName}:

Attaching Structured Objects

As said earlier, one of the strengths of Serilog is its ability to serialize and attach complete structured objects in its log event:

As you can see below, you have to prefix your property name with ‘@’ in order to be automatically serialized by Serilog:

Attaching an Exception

Since we can attach stuctured objects, exceptions can also be attached:

and also highlighted in red within Seq:

Summing up Part 1

In this first part, we went through the basics of using Serilog and Seq. The goal was to get you quickly up and running so that you can try it out yourself. In Part 2, we will go through the contextual logging capabilities of Serilog, always in conjunction with Seq.

Continue with Part2.



Comments