Skip to main content

Serilog Logging Framework

Serilog is an open-source logging framework for .NET applications that provides a flexible and powerful way to capture and store log events.

It is designed to be easy to use and highly extensible, making it a popular choice for developers who want more control over their logging infrastructure.

In this tutorial, we will explore the history, features, and examples of the Serilog framework. We will discuss its key features and demonstrate how to use them with code snippets and explanations of the output. By the end of this tutorial, you will have a good understanding of what Serilog can do and how to integrate it into your .NET applications.

History of Serilog

Serilog was first released in 2013 by Nicholas Blumhardt as an alternative to traditional logging frameworks like log4net and NLog. It was created with the goal of providing a more structured and flexible logging experience, with a focus on semantic logging and easy integration with other tools and services.

Since its initial release, Serilog has gained popularity among .NET developers due to its simplicity, extensibility, and powerful features. It has a vibrant community and is actively maintained and updated to meet the evolving needs of developers.

Key Features of Serilog

1. Structured Logging

One of the main features of Serilog is its support for structured logging. Instead of simply logging text messages, Serilog allows you to log structured data in a format like JSON or key-value pairs. This makes it easier to search, filter, and analyze log events, as well as integrate with other tools and services.

Here's an example of how to log a structured event using Serilog:

logger.Information("Order {OrderId} placed by {Username}", orderId, username);

In this example, the log event includes two properties: OrderId and Username. These properties can be used for filtering and querying log events later.

2. Flexible Output Sinks

Serilog supports a wide range of output sinks, allowing you to choose where your log events should be stored. Some of the popular output sinks include:

  • Console sink: Writes log events to the console output.
  • File sink: Writes log events to a text file.
  • Seq sink: Sends log events to the Seq log server for centralized logging and analysis.
  • Elasticsearch sink: Indexes log events in Elasticsearch for easy searching and analysis.

You can configure multiple output sinks and filter log events based on their properties to send them to different destinations. This flexibility allows you to tailor your logging infrastructure to your specific needs.

Here's an example of how to configure Serilog to write log events to a file:

Log.Logger = new LoggerConfiguration()
.WriteTo.File("log.txt")
.CreateLogger();

3. Enrichers

Serilog provides enrichers that allow you to add additional properties to log events automatically. This can be useful for capturing contextual information, such as the current user, request ID, or environment.

For example, you can use the WithProperty enricher to add a property to all log events:

Log.Logger = new LoggerConfiguration()
.Enrich.WithProperty("ApplicationName", "MyApp")
.CreateLogger();

In this example, every log event will have an additional property called "ApplicationName" with the value "MyApp".

4. Filtering

Serilog allows you to filter log events based on their properties, enabling you to control which events are stored and where they are sent. You can use simple comparison operators or complex expressions to define filtering rules.

Here's an example of how to filter log events based on their log level:

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.CreateLogger();

logger.Information("This will be logged");
logger.Debug("This will not be logged");

In this example, only log events with a log level of "Information" or higher will be written to the console.

Examples of Serilog Usage

Example 1: Logging to Console

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();

logger.Information("Hello, Serilog!");

Output:

Hello, Serilog!

Example 2: Logging to File

Log.Logger = new LoggerConfiguration()
.WriteTo.File("log.txt")
.CreateLogger();

logger.Information("Logged to file");

Output (log.txt):

Logged to file

Example 3: Structured Logging

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();

logger.Information("Order {OrderId} placed by {Username}", orderId, username);

Output:

Order 123 placed by john.doe

Conclusion

In this tutorial, we explored the Serilog framework for logging in .NET applications. We discussed its history, key features, and provided several examples demonstrating its usage. Serilog's structured logging, flexible output sinks, enrichers, and filtering capabilities make it a powerful tool for capturing and analyzing log events in your applications.

To learn more about Serilog and its advanced features, refer to the official Serilog documentation.