Structured Logging with Serilog in ASP.NET Core

.NET 8 Zero to Hero Series has started! Here is how to Log like a PRO using Serilog in ASP.NET Core Applications.

Hi Everyone,

This is the first content in my all-new .NET 8 Zero to Hero series, that focuses specifically on backend development.

Logging is the most essential part of any application nowadays, as it helps to debug/analyze and diagnose any application issues or bugs.

Serilog is the most popular logging library for ASP.NET Core Applications. In this article, we will learn everything you need to know to master Structured Logging in your ASP.NET Core Application using Serilog. We will understand Serilog Configuration, Sinks, and all the best practices you need to follow. This will be the only guide you need to refer to master logging for your .NET Application.

That said, here are a few best practices and recommendations while working with Serilog in ASP.NET Core Applications.

  • Do not rely on File and Console logging in Production

  • Sparingly use console logging in Production, as this might be a blocking call at times. Do console logging only for Errors if needed. Otherwise always stick to external logging in Production.

  • Prefer appsettings.json over fluent api configuration, as appsettings is more robust and flexible to work with.

  • Prefer SEQ For Local Development as it’s easy to diagnose issues based on the incoming logs.

  • Use Asynchronous Logging to ensure the best performance of your ASP.NET Core application.

  • For larger systems, try to add a correlation ID to your structured logs so that you can identify the logs of a single request. This can be done by introducing a middleware and pushing a GUID as the correlation ID.

I hope you enjoyed this article! There is a ton of content that I have planned for this series/newsletter. Do Subscribe to be notified whenever I post new content.

PS, I have recently opened sponsorship spots where you can promote your product/services via my newsletter/blog. Do get in touch with me if you are interested -

Mukesh Murugan

Join the conversation

or to participate.