Serilog: Installation & Basics
Why logging matters
Imagine you are maintaining a critical e-commerce application. On a Monday morning, your team is urgently summoned: customers cannot complete purchases, and sales are plummeting.
Cloud monitoring tools give you a head start - you see that 22% of API requests to the purchase endpoint failed with 500 Internal Server Error in the last 24 hours. But monitoring only tells you that something is wrong, not why. Is it the database? A third-party service? A code bug?
This is where logging becomes essential.
A quick search through your logs reveals a pattern: the failures occur specifically during communication with a payment gateway, returning timeout errors. Further investigation confirms the gateway provider is operational, pointing to a connection issue on your end. A quick service restart resolves the bottleneck, and traffic returns to normal.
Effective logging turned a potential disaster into a manageable incident, saving the company revenue and earning you recognition. It provides the "why" behind the "what," enabling you to diagnose and resolve issues rapidly.
How default .NET logging works
The .NET ecosystem includes a built-in logging abstraction called Microsoft.Extensions.Logging. It typically uses Dependency Injection to provide an ILogger<T> instance to any class that needs it.
Here is a standard example in a Controller:
By default, these logs are output to the console. While sufficient for local development, production environments require more robust capabilities—such as writing to files, databases, or centralized logging services. This is where third-party libraries come in.
Why Serilog?
Serilog is one of the most popular logging libraries for .NET, and for good reason.
While traditional logging relies on message formatting (strings), Serilog supports structured logging. This means it captures log events as structured data (objects), allowing you to query and filter logs based on properties rather than parsing text.
Additionally, Serilog offers a vast ecosystem of sinks—plugins that write logs to various destinations like files, SQL databases, Elasticsearch, or cloud providers. It strikes an excellent balance between powerful customization and ease of use.
Let's set it up in a .NET Web API.
Prerequisites
To follow this tutorial, ensure you have:
- Git
- .NET 8 SDK or later
- An IDE (e.g., VS Code, Rider, or Visual Studio)
I have prepared a starter repository. Clone SerilogDemo and checkout the 001-installation-and-basics branch to follow along.
Default .NET logging provider
Before installing Serilog, let's look at the default behavior in Program.cs:
Running this application and hitting the /weatherforecast endpoint produces simple text output:
Step 1 - Open the Project
Clone the repository and open it in your IDE.
Step 2 - Install Serilog packages
We need to install the core Serilog integration for ASP.NET Core and a sink for the console. Run the following commands in your terminal:
This installs:
Serilog.AspNetCore: Integrates Serilog with the ASP.NET Core pipeline.Serilog.Sinks.Console: Writes log events to the standard output.
Step 3 - Basic Serilog Setup
Open Program.cs. First, add the namespace and configure the host to use Serilog:
Next, wrap the application startup logic in a try-catch-finally block to ensure logs are flushed correctly if the app crashes during startup:
Run the application again. You will notice the log format has changed, indicating Serilog is now active:
Bonus for advanced engineers
While researching this post, I found an insightful video by Theo discussing logging best practices, based on Boris Tane's excellent resource loggingsucks.com.
Both the video and the article are highly recommended for a deeper dive into logging philosophy.
Recap & What's Next
You have successfully replaced the default .NET logger with Serilog. The underlying ILogger abstraction allows your application code to remain unchanged while the logging engine is swapped out for a more powerful one.
In the next part of this series, we will:
- Configure logging to files.
- Move configuration into
appsettings.json. - Explore log levels and enrichment for better context.
Stay tuned! 🚀
Recommended for you

Serilog: Structured Logging Explained

Serilog: Enrichers for Better Context
