Structured logging is essential for debugging and monitoring modern applications. In this post, I'll walk through configuring Serilog to ship logs directly to Elasticsearch.
Prerequisites
Before getting started, you'll need:
- .NET 8 SDK
- Elasticsearch instance (local or hosted)
- Basic familiarity with ASP.NET Core
Installing the Packages
Add the required NuGet packages to your project:
dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Sinks.Elasticsearch
Configuration
Add the following to your appsettings.json:
{
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "Elasticsearch",
"Args": {
"nodeUris": "http://localhost:9200",
"indexFormat": "myapp-logs-{0:yyyy.MM}"
}
}
]
}
}
Wiring It Up
In Program.cs, configure the host to use Serilog:
builder.Host.UseSerilog((context, config) =>
{
config.ReadFrom.Configuration(context.Configuration);
});
Conclusion
With this setup, your logs will be searchable in Kibana within seconds. Structured logging makes it much easier to diagnose issues in production.
Have questions? Feel free to reach out or leave a comment below.