Serilog: logging to console, Seq, ElasticSearch & File using dotnet6

Satyam Pushkar
6 min readDec 30, 2021

This article talks about how to use Serilog in dotnet applications for logging into Console, Seq, Elasticsearch and file(text & json).

What is Serilog

Serilog is one of the .NET libraries available for logging. It provides diagnostic logging to console, files and many other options. But what distinguishes it from others?

  • Serilog is built with structured event data in mind. It has a great support for logs as structured data, i.e. it supports a stream of first-class events that in turn provides observability to applications in distributed architecture.
  • It also supports many output destinations (sinks), and has ability to enrich, route, filter, and format structured log events. All of this makes Serilog very much necessary in modern real-world distributed applications.

Serilog has many output options(sinks) for logging. Following sections discuss about console, seq, elasticsearch & file sinks. The sample code for this app can be found at GitHub.

Set up Serilog in dotnet application

The very first step is to add required packages. If you are creating an asp.net application you can add the package Serilog.AspNetCore. Packages can be added using nuget package manager or through dotnet add package <package-name>.

Otherwise if you want the minimal setup, you can add the package Serilog alone itself. Later based on the required output option, add the required sink packages like Serilog.Sinks.File, Serilog.Sinks.Console, Serilog.Sinks.Seq or Serilog.Sinks.Elasticsearch.

Logs in Console using Serilog

For the sample app I have used Serilog.AspNetCore which already have added Serilog.Sinks.Console as it’s dependency. Add the following piece of code in Programm.cs(sample app is based on dotnet6).

builder.Host.UseSerilog((context, loggerConfiguration) =>{loggerConfiguration.ReadFrom.Configuration(context.Configuration).Enrich.FromLogContext().Enrich.WithMachineName().Enrich.WithEnvironmentName().WriteTo.Console()});

Once you run the application through Visual Studio 2022, You will see the output similar to shown in below image.

Logs in Seq using Serilog

Seq is an intelligent search, analysis, and alerting server built specifically for modern structured log data and Serilog is Seq’s most supported logging library.

For logging into seq, first add the package Serilog.Sinks.Seq. Then you can call a WriteTo.Seq() in Programm.cs with required configurations similar to console logging. But I have used a different approach here to read these values from appSettings/config. Either of this can be used for configuring the output/sink destination for logging.

You can find the script to run Seq locally under Infrastructure folder. You can run it locally in two ways:

  • Using docker container way: Run the PowerShell script start-seq.ps1 available under Infrastructure\Seq\docker-container-example. Prerequisite: Docker Desktop. Use the ServerUrl for docker-container from appsettings.json and run the dotnet app. You will get output similar to below diagram in Seq. The UI is exposed through localhost:8081 and injection is happening through port localhost:5341. Injection can happen through localhost:8081 also.
  • Using Helm chart way: Run the PowerShell script start-seq-with-helm.ps1 available under Infrastructure\Seq\helm-example. Prerequisite: Docker Desktop with Kubernetes enabled and Helm Chart installed. Use the ServerUrl for Helm-chart from appsettings.json and run the dotnet app. You will get output similar to below diagram in Seq. The UI is exposed through localhost/sequi/ and injection is happening through localhost/seqin/. Injection can happen through localhost/sequi/ also.

Seq has many features, dashboard is one of them. Here you can see the events/errors/exceptions in a beautiful graphical format. You can check the dashboard and event page’s snapshot in below diagram.

Logs in ELK using Serilog

Elasticsearch is a distributed, open-source search and analytics engine which allows to store, search, and analyze huge volumes of data quickly and in near real-time and give back answers in milliseconds. It is a widely popular among developers for logging.

For logging into Elasticsearch, add the package Serilog.Sinks.Elastisearch. Then you can call a WriteTo.Elastisearch() in Programm.cs with required configurations(check the below image) similar to console logging. It can also be done using appSettings/config as explained in Seq.

Kibana is very helpful for visualization. You can find the script to run EK(Elasticsearch & Kibana) locally under Infrastructure folder. You can run it in following ways:

  • Using docker composer way: Run the PowerShell script start-ek.ps1 available under Infrastructure\Elastic\docker-compose-example. Prerequisite: Docker Desktop. Run the dotnet app and you will get output similar to below diagram in Kibana. The Kibana UI is exposed on localhost:5601. You can also check the Elasticsearch on localhost:9200.

In Kibana,

  • To add the index, go to Management>> Stack Management>> Kibana >> Index Patterns and create index pattern by selecting index pattern name (in our case serilog-demo-logs*).
  • To check the logs as stream, go to Observability>>Logs>>Settings and append the log indices with the index which is created by logs injecting app(in our case serilog-demo-logs*).
  • To check the logs; select discover from menu and you will see the output similar to below diagram.

You can click on any of the entry to see more details as shown in below diagram.

Logs in file using Serilog

Logging into files is a very common and popular way among developers. For logging into Files, first add the package Serilog.Sinks.File. Then you can call a WriteTo.File() in Programm.cs with required configurations similar to console logging. It can also be done using appSettings/config as explained in Seq. For this example, I have used logging into file in 2 different ways.

  1. Logging into file in plain text format: Logs will be appended in the specified text file in plain text format. Configuration is added in Programm.cs >> .WriteTo.File(“.\\logs\\log.txt”)
  2. Logging into file in json format: Logs will be appended in the specified text file in json format. Configuration is added in appsettings.json.

Hope this article has helped you understand the basics of Serilog and how to use it with Console, Seq, Elasticsearch and Files.

To understand ELK stack in more detail, Please check my article here.

To understand Helm Chart, Please check my article here.

--

--

Satyam Pushkar

Software Engineer | Backend Specialist | System Architect | Cloud Native Apps | DOTNET, PYTHON | https://www.linkedin.com/in/satyampushkar/