Contact Us

How to use advanced Serilog features in ASP.NET Core MVC | InfoWorld

Mobile App | July 13, 2022

One of the great features of ASP.NET Core is its built-in support for logging. This means that you can take advantage of your logging framework to get infrastructure logs from your logging infrastructure. While this can help you gain deeper insights into your application metrics, this does have its downsides as well. For example, you would often get too many logs, i.e., too many messages in your logs that might not be meaningful.

Serilog is a third-party, open-source library that integrates nicely with ASP.NET Core and allows developers to easily log-structured event data to the console, to files, and various kinds of log targets. I’ve discussed on the basic features of Serilog in a previous article here. This article talks about a few advanced capabilities in Serilog such as reducing log verbosity and log message summary and how you can implement them in ASP.NET Core MVC 5.

To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here.

Create an ASP.NET Core MVC project in Visual Studio 2019

First off, let’s create an ASP.NET Core project in Visual Studio 2019. Following these steps will create a new ASP.NET Core MVC 5 project in Visual Studio 2019.

A new ASP.NET Core MVC 5 project will be created together with the default HomeController class. We’ll use this project to work with Serilog in the subsequent sections of this article.

Install the Serilog NuGet packages

If you have successfully created an ASP.NET Core project, the next thing you should do is add the necessary NuGet packages to your project. To do this, select the project in the Solution Explorer window and right-click and select “Manage NuGet Packages…” In the NuGet Package Manager window, search for the following packages and install them.


Alternatively, you can install the two packages via the NuGet Package Manager console by entering the lines shown below.

Register Serilog in ASP.NET Core MVC

To start using Serilog in ASP.NET Core applications, you should register Serilog on the WebHostBuilder in the Program.cs file using the UseSerilog extension method as shown below.

Here is the complete source code of the Program class for your reference:

Configure Serilog in ASP.NET Core MVC

Serilog takes advantage of sinks to send logs to different log targets such as a text file or a database. You should write the following code in the Main method to configure the log target and specify the rolling interval and file size limit.

Understand Serilog event severity levels

Now that you’ve configured Serilog in your application, you can take advantage of the built-in interface ILogger to log data in action methods of your controller as shown below.

By default Serilog will log events of Information severity level and above. Two levels below Information level are also available — Debug and Verbose.

Create a Serilog logger instance in ASP.NET Core MVC

The following code snippet illustrates how the _logger instance has been created using dependency injection in the constructor of the HomeController class.

Here is the complete source code of the HomeController class for your reference:

If you run the application, the application will automatically navigate to the Home page by default. You’ll observe that a log file has been generated inside the C:Logs folder in your computer with the following content:

Figure 1: By default, Serilog logs events of Information severity level and above.

Note the highlighted text in Figure 1. Those are the lines that we’ve written to the log in the Index action method of the HomeController class. Note too that logging the events associated with Serilog’s default severity levels — Information, Warning, Error, and Fatal — can create very verbose logs. Fortunately we can reduce the verbosity of the log by turning off event levels we don’t care about.

Turn off unnecessary events in Serilog

Update the Serilog configuration you’ve specified in the Program class using filters as shown in the following code snippet.

Note that, while we’ve changed the minimum event severity level to Debug, we have restricted logging to Information level and higher for system (“Microsoft”) events and Warning level and higher for ASP.NET Core. When you run the application again, here’s how the log file would look like this time:

Figure 2: Filtering log messages in Serilog.

The log messages have been reduced and the log looks much cleaner now, doesn’t it?

Use Serilog’s request logging middleware in ASP.NET Core MVC

Available as part of the Serilog.AspNetCore package, Serilog’s request logging middleware (i.e., the RequestLoggingMiddleware class) is used to add one summary log message per request in your application. In other words, you can take advantage of the request logging middleware to get a summary of all of the log messages for each request in your application.

To add RequestLoggingMiddleware to the pipeline, you need to call the UseSerilogRequestLogging() method. This method condenses the vital information of each request into a clean and concise completion event. Then to use RequestLoggingMiddleware all you have to do is specify the following line in the Configure method of the Startup class:

Now, if you run our application again, you’ll see the same log messages but with an additional summary log message from Serilog that lets you know the time taken to execute the request, as shown in Figure 3 below.

Figure 3: Serilog’s RequestLoggingMiddleware in action!

As logging is integral to every application, simplicity and flexibility are essential attributes for a logging framework. Serilog offers a logging framework that is simple to set up and can be used to log structured event data to a variety of logging destinations. Serilog’s request logging middleware may be used to filter out the vast majority of the infrastructure logs that are produced by default, allowing you to save time and resources. I’ll discuss more of Serilog’s advanced features in future articles here.

This content was originally published here.