When it comes to logging in .NET applications, Serilog is a favorite among developers for its structured logging capabilities and seamless integration with the .NET ecosystem. In this blog post, we’ll enhance a sample console application (which we created here) to include robust logging using Serilog.
Why Use Serilog?#
Logging is critical in modern applications for debugging, monitoring, and understanding system behavior. While .NET’s built-in logging framework is excellent, combining it with Serilog takes your logs to the next level by offering:
- Structured Logs: Logs are emitted in a format like JSON, enabling easier querying and analysis.
- Rich Ecosystem: Choose from dozens of sinks to send logs to files, databases, cloud platforms (there is no better and easier way to log to Azure Application Insights), or even specialized tools like Seq or Elasticsearch.
- Dynamic Contextual Data: Attach custom properties to your logs, improving traceability.
Setting Up the Application#
Let’s start with a sample console application using .NET 9. First, create a new console application:
| |
Add the necessary NuGet packages:
| |
Configuring Serilog#
In your Program.cs, configure Serilog as early as possible to capture all logs, even during application startup:
| |
This setup ensures Serilog is the primary logging provider, and logs are flushed before the application exits.
Adding Context to Your Logs#
One of Serilog’s most powerful features is log enrichment. You can dynamically add properties to logs, making them more informative:
| |
This is particularly useful in scenarios like distributed systems, where you might want to include properties such as RequestId or UserId.
Configuring Serilog with appsettings.json#
To make the logging configuration environment-specific, move the Serilog configuration to appsettings.json:
- Create a appsettings.json file in the project root:
| |
Install the Serilog.Settings.Configuration NuGet Package and update the Serilog configuration in Program.cs:
| |
Now you can have different configurations for Debug and Release environments by creating appsettings.Development.json and appsettings.Production.json.
Testing the Setup#
Add a sample hosted service to test logging:
| |
Run the application, and you’ll see Serilog’s structured logs in your console.

Key Takeaways#
- Exception Handling: Ensure exceptions are logged properly using Log.Fatal().
- Contextual Logging: Use LogContext to enrich your logs with dynamic properties.
- Environment-Specific Configurations: Leverage appsettings.json for flexible setups.
- Integration: Serilog integrates seamlessly with .NET’s logging framework.
Logging is the backbone of any reliable application. By combining .NET’s built-in logging framework with the power of Serilog, you can build a logging system that’s maintainable, scalable, and insightful. Effective logging not only helps you pinpoint issues quickly but also provides valuable insights into your application’s behavior over time. Think of it as leaving breadcrumbs for your future self — making troubleshooting smoother and resolutions faster. Trust me, future you will thank you!
Ready to take your logging to the next level? Try it out in your next project, and remember to log your progress along the way!
P.S. I’ve also leveraged this logging mechanism in my Blazor blog posts. Curious to see it in action? Get started here:
An Adventure in Modern Web Hosting
