DEV Community

Deependra kushwah
Deependra kushwah

Posted on

Exploring Diffrent Types of Sinks in Serilog

In the world of logging frameworks, Serilog stands out as a powerful and flexible option for .NET applications.

One of the key features that makes Serilog so versatile is its support for a wide range of sinks.

Exploring Diffrent Types of Sinks in Serilog

Sinks are responsible for delivering log events to various destinations, such as the console, files, databases, or third-party services.

In this article, we will get some brief idea about the built-in sinks provided by Serilog, including the Console sink, File sink, Seq sink, and more.

If you are using .Net Core application, each sink can be configured using appsettings.json file.

Before we move into the diffrent sinks configurations. Let's setup the serilog in .Net Core app.

  • Create .Net Core app
  • Install the nuget packages -- Serilog -- Serilog.Extensions.Configuration

Add this piece of code into the startup file or program.cs file.

Setup Serilog in .Net Core

We are reading configuration from the appsetting.json file. So we can keep updating the sinks and other configuration from config file itself.

Console Sink

The Console sink is a basic and widely used sink that writes log events to the console or command-line interface. It is useful for debugging and development purposes.

Nuget Package for Console Sink

-Serilog.Sinks.Console

Configuration for Console sink

Configuration for Console sink

File Sink

The File sink allows you to write log events to one or more log files on the local file system. We'll cover various configuration options such as file path, rolling file behavior, and log file size limits.

Nuget Package required

  • Serilog.Sinks.File

Changes in configuration. Let's Add logging to file as well along with the console log.

Log to File using Serilog.Sinks.File

Rolling Interval is used to defin the time when new file will be generated. e.g: Minut,Hour,Day,Month and Year.

Seq Sink

Seq is a popular log server and analysis tool that integrates seamlessly with Serilog. The Seq sink enables you to send log events to a Seq server for centralized log storage and analysis.

Nuget Package

  • Serilog.Sinks.Seq

Writing to seq server require two things. Server url and API key which will be configured in appsettings.json.

Log to Seq Sink

Email Sink:

The Email sink allows you to send log events via email notifications. This can be useful for critical error alerts or for monitoring specific log events.

Nuget Package

  • Serilog.Sinks.Email

Serilog.Sinks.Email

Writing to email sink is necessary, with server, recipient, and log message subject.

If we don't pay attention to filtering the logs, you could get a lot of unwanted logs that aren't even required.

Database Sinks

Serilog provides several database sinks, including sinks for SQL Server, MySQL, PostgreSQL, and more. These sinks enable you to store log events in a structured manner for easy querying and analysis.

Nuget Package

  • Serilog.Sinks.MSSqlServer

Log to MsSql With Serilog.Sinks.MSSqlServer
A few parameters must be configured in order to log data in the database. such the table name, connection string, and flag that creates the table if it doesn't already exist.

External Service Sinks

Serilog offers sinks for various external services, such as Elasticsearch, Azure Application Insights, and Splunk. These sinks allow you to send log events to these services for advanced log analysis and monitoring.

Conclusion

The built-in sinks provided by Serilog offer a wide array of options for logging in .NET applications.

Whether you need simple console output, file-based logs, centralized log storage and analysis, or integration with external services, Serilog has got you covered.

By exploring and understanding the features and configuration options of these built-in sinks, you can make informed decisions about the best logging strategy for your specific needs.

Reference Article
A Comprehensive Guide to Logging with Serilog for .NET Developers

Top comments (0)