Introduction
This article focus is on way to implement structured logging in ASP.NET Core using Serilog. Setting up robust logging from the start is essential for effective monitoring and debugging. This guide explores how to implement Serilog to create a high-performance, structured logging system for modern applications.
Serilog is premier third-party logging library that seamlessly integrates with the native .NET ILogger interface. It allows applications to direct logs to diverse destinations—including consoles, files, databases etc. By providing structured logging, Serilog ensures data is stored in a searchable format for cleaner analysis. In terms of performance, Serilog has a negligible impact on application speed. By utilizing features such as asynchronous logging and message batching, it ensures that logging operations do not hinder the responsiveness of the application.
Setup
To begin, install the Serilog integration package for ASP.NET Core using the following command:
Install-Package Serilog.AspNetCoreAfter installation, configure the logger within
Program.cs. The following setup initializes the logger at the application entry point and integrates it into the host:
using Serilog;
// Setup a temporary logger for the startup process
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
try
{
Log.Information("Initializing the Web Host...");
var webAppBuilder = WebApplication.CreateBuilder(args);
// Replace default logging with Serilog
webAppBuilder.Host.UseSerilog((hostingContext, loggerConfig) =>
{
loggerConfig
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.WriteTo.Console();
});
webAppBuilder.Services.AddEndpointsApiExplorer();
webAppBuilder.Services.AddSwaggerGen();
var appInstance = webAppBuilder.Build();
if (appInstance.Environment.IsDevelopment())
{
appInstance.UseSwagger();
appInstance.UseSwaggerUI();
}
appInstance.UseHttpsRedirection();
appInstance.Run();
}
catch (Exception startupException)
{
Log.Fatal(startupException, "The application host terminated unexpectedly during startup.");
}
finally
{
// Ensure all logs are written before the app closes
Log.CloseAndFlush();
}
Initially, we are creating the logger instance using Serilog while enabling it to write logs to the console. Note that the only purpose of this piece of code is to enable logging within the Program.cs.
As we navigate, we are integrating Serilog into the ASP.NET Core Dependency Injection (DI) container that ensures its implementation is used by every ILogger<> instance throughout the application. In this setup, two primary configurations are established: directing output to the Console and enabling the logger to ReadFrom.Configuration. Consequently, the framework bypasses the standard "Logging" section in appsettings.json, relying exclusively on the "Serilog" section for all logging behavior.
The logging configuration is consolidated under the
Serilogkey to define clear boundaries for log filtering and verbosity
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.AspNetCore.Hosting.Diagnostics": "Error",
"System": "Warning"
}
},
"WriteTo": [
{ "Name": "Console" }
]
},
"AllowedHosts": "*"
}
The configuration sets the MinimumLevel to Information, ensuring only events of that priority or higher are recorded. Serilog utilizes six distinct log levels to categorize system events:

Join the conversation! Your thoughts help the community grow.