Exploring Serilog Sinks in .NET 8

Chaitanya (Chey) Penmetsa
CodeNx
Published in
4 min readApr 8, 2024

In this blog we will explore what are Serilog sinks and see how we can leverage Serilog sinks in .NET Web API. In the context of Serilog, “sinks” are the destinations where log events are sent for storage or further processing. Serilog sinks are essential because they allow you to direct log events to various output targets, such as files, databases, consoles, cloud storage, email, and more. For understanding why, we need Serilog and importance of structured logging please my previous blog using below link.

Image from Serilog website

Why do we need Serilog sinks?

  • Flexibility and Versatility — Serilog sinks provide a wide range of options for storing and viewing log data. This flexibility allows you to tailor your logging strategy to the specific needs of your application and environment. You can choose from a variety of sinks based on your requirements, such as File, Console, Database, Email, Seq, Elasticsearch, and many others.
  • Centralized Logging — Sinks enable you to consolidate log events from multiple sources into a centralized location. This is particularly useful in distributed systems where logs need to be aggregated for monitoring and analysis. Centralized logging helps in troubleshooting, debugging, and monitoring the health of your application across different environments.
  • Integration with External Services — Serilog sinks allow seamless integration with external services and platforms. For example, you can send logs to cloud-based services like Azure Application Insights, AWS CloudWatch, Google Cloud Logging, etc. Integration with these services provides additional features such as advanced search, monitoring dashboards, alerting, and long-term log retention.
  • Performance Optimization — Serilog sinks offer options to optimize performance based on your application’s needs. For example, you can choose asynchronous sinks to improve logging performance by offloading log writing to background threads.
  • Scalability — By using sinks that support distributed systems and cloud environments, you can easily scale your logging infrastructure as your application grows.
  • Ease of Maintenance and Troubleshooting — Serilog sinks simplify maintenance and troubleshooting by providing clear separation between logging configuration and implementation code.

You can find lot of packages built for different destinations as Serilog sinks from below link:

Now let us look at how we can configure multiple sinks like write to Console and File. Also, we will configure them using configuration. For that create a simple Web API and install below packages for demonstrating how we can configure Serilog Sinks:

dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Sinks.MSSqlServer

Even though we want to write to Console, File and Sql Server we have only installed Sql Server sink separately since Serilog.AspNetCore by default installs Console and File sinks. Now let us see how we can configure the Serilog using appsettings.json as shown below:

{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": "Debug",
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": { "path": "Logs/log.txt" }
},
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=Server;Database=SerilogSample;Trusted_Connection=True;TrustServerCertificate=True;",
"tableName": "Logs",
"autoCreateSqlTable": true
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithExceptionDetails" ]
},
"AllowedHosts": "*"
}

Below is how we can setup application to use serilog.

builder.Services.AddSerilog(options =>
{
//we can configure serilog from configuration
options.ReadFrom.Configuration(configuration);
});

Now run the application and you will see logs getting written to three places simultaneously as show below.

With this we conclude this blog in which we have see how to configure Serilog sinks to write to different sources. In future blog let us see how we can configure Serilog to write to ELK as well as Seq also we can see how we can enrich our log messages using Serilog Enrichers.

🙏Thanks for taking the time to read the article. If you found it helpful and would like to show support, please consider:

  1. 👏👏👏👏👏👏Clap for the story and bookmark for future reference
  2. Follow me on Chaitanya (Chey) Penmetsa for more content
  3. Stay connected on LinkedIn.

Wishing you a happy learning journey 📈, and I look forward to sharing new articles with you soon.

--

--

Chaitanya (Chey) Penmetsa
CodeNx
Editor for

👨🏽‍💻Experienced and passionate software enterprise architect helping solve real-life business problems with innovative, futuristic, and economical solutions.