How to Implement Serilog in .NET Core Web Api?


Logging is an important part of any application as long as everything runs smoothly. You don’t need it, but if you face any issue with your application, you better have logs that help you find the problem. In this blog, we will learn about serilog and how to integrate it into an ASP.NET core web api application running on .net.  


  • What is Serilog?
  • Benefits of using Serilog 
  • How to configure Serilog

What is Serilog?

Serilog is an easily configurable logging library for .NET. with a clearly defined api. It is a simple .NET logging with fully-structured events. This logging helps us find the primary cause during code debugging, and if we get the error in production, a quick solution is provided for the end user. It helps the developers to log events or messages into various applications like consoles, web, databases, and many more apps.

Serilog provides a fluent API for configuring the logger, which allows developers to chain together configuration methods to set up sinks, enrichers, and other options. Here are some of the most commonly used methods in the Serilog API:

  • WriteTo: This specifies one or more sinks to write log events to, such as the console, a file, or a database.
  • Enrich: This specifies one or more enrichers to add additional context to log events, such as the current thread ID or user information.
  • MinimumLevel: This sets the minimum logging level for events to be written to the sink(s), such as Information, Warning, or Error.
  • Filter: This specifies a filter to include or exclude log events based on some condition, such as the logger name or log level.
  • With: This sums up the additional properties to a log event, such as a correlation ID or request ID.
  • CreateLogger: This creates the logger instance based on the configured sinks and options.

There are many other methods available in the Serilog API for configuring the logger, such as,

  • ReadFrom for reading configuration from external sources, 
  • Destructure for controlling how objects are logged, and 
  • AuditTo for writing audit events to a separate sink. 

The API is designed to be extensible and flexible, allowing developers to customize the logger to their specific needs.

Benefits of using Serilog

Serilog is a logging framework for .NET applications that offers several benefits for developers. Here are some of the key benefits of using Serilog in ASP.NET C#:

  1. Flexible logging: Serilog offers a flexible logging experience that allows developers to log data in a variety of formats, including JSON, XML, and plain text. This makes it easier to integrate with different logging systems and services.
  2. Easy to configure: Serilog is easy to configure and use, and it comes with several built-in sinks that allow you to send log data to different targets, such as the console, files, and databases. Additionally, it supports structured logging, which makes it easier to search and analyze log data.
  3. Fast and scalable: Serilog is designed to be fast and scalable, which makes it suitable for use in high-traffic web applications. It uses asynchronous logging and batching to reduce the impact of logging on application performance.
  4. Extensible: Serilog is extensible and can extend with custom sinks, enrichers, and other components to meet the specific needs of your application.
  5. Open source: Serilog is open source which means that you can use it for free, and if you wish, then you can contribute to its development. Additionally, it has a large and active community that provides support and shares the best practices for using the framework.

Overall, Serilog is a powerful logging framework that offers several benefits for .NET developers, including flexibility, ease of use, scalability, extensibility, and an active community.

How to Configure Serilog?


In Solution Explorer > your project name > Manage NuGet Packages. It opens a Nuget Package Manager Tab and Installs packages shown in the below image.

Serilog ASP.NET

For Package Manager Console

Before starting coding,  you need to install some dependencies for Serilog. You can click on Tools > NuGet Package Manager > Package Manager Console. 

Now Go to the console and run the command one by one.

Install-Package Serilog.AspNetCore

Configure Serilog in Program.cs

The UseSerilog function can be called in the HostBuilder instance for configuring Serilog using a lambda expression. 


Open appsettings.json and change the code in the following file, which is given below.


Now, open WeatherForecastController.cs and Replace the following code.


Now run your application and call the weather forecast controller to get the method. 

Then you go to the file path ( this path we define in the appsettings.json -> write to -> file Path section), where we log it.





Serilog is easy to use and configure and implements structured logging in your .net application. If you are looking for a good logging framework, developing and maintaining your .NET applications will be much easier!

Similar Articles