DEV Community

Cover image for Enterprise-Ready Logging with Serilog in .NET
Nikhil Wagh
Nikhil Wagh

Posted on

Enterprise-Ready Logging with Serilog in .NET

Logging isn't just about printing errors — it's about observability, traceability, and debugging production issues before they become outages. In this post, let’s explore how to implement enterprise-grade logging using Serilog in .NET — from basic setup to advanced features.

Why Serilog?
Serilog is a structured logging library for .NET that makes your logs queryable, filterable, and machine-readable. It supports:

  • Multiple sinks (console, file, Seq, Elasticsearch, etc.)
  • Enrichers for contextual data
  • Asynchronous logging
  • Easy integration with ASP.NET Core

Getting Started

Install the required NuGet packages:

dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.Sinks.File
Enter fullscreen mode Exit fullscreen mode

In Program.cs (for .NET 6+):

using Serilog;

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day)
    .Enrich.FromLogContext()
    .CreateLogger();

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog(); // Replace default logging

var app = builder.Build();
Enter fullscreen mode Exit fullscreen mode

Add Contextual Information

Serilog’s enrichers make your logs more meaningful:

dotnet add package Serilog.Enrichers.Environment
dotnet add package Serilog.Enrichers.Thread
Enter fullscreen mode Exit fullscreen mode

Update configuration:

Log.Logger = new LoggerConfiguration()
    .Enrich.WithEnvironmentUserName()
    .Enrich.WithThreadId()
    .WriteTo.Console()
    .CreateLogger();
Enter fullscreen mode Exit fullscreen mode

This adds thread ID, machine name, and user context to every log.

Logging in Action

Use it anywhere in your app:

public class ProductService
{
    private readonly ILogger<ProductService> _logger;

    public ProductService(ILogger<ProductService> logger)
    {
        _logger = logger;
    }

    public void Process()
    {
        _logger.LogInformation("Processing started at {Time}", DateTime.UtcNow);
    }
}
Enter fullscreen mode Exit fullscreen mode

Use AppSettings (Optional)

You can configure Serilog in appsettings.json for flexibility:

"Serilog": {
  "MinimumLevel": "Information",
  "WriteTo": [
    { "Name": "Console" },
    { "Name": "File", "Args": { "path": "logs/app.txt" } }
  ]
}
Enter fullscreen mode Exit fullscreen mode

And in Program.cs:

builder.Host.UseSerilog((ctx, lc) =>lc.ReadFrom.Configuration(ctx.Configuration));
Enter fullscreen mode Exit fullscreen mode

Sinks You Should Know

  • Console/File — Local development & basic diagnostics
  • Seq — Beautiful web UI for structured logs
  • Elasticsearch — Ideal for large-scale distributed logging
  • Application Insights — Works well in Azure environment

Best Practices for Production

  • Use asynchronous sinks to avoid blocking threads.
  • Avoid logging sensitive info (e.g., passwords, tokens).
  • Add correlation IDs for request tracing (via middleware).
  • Log levels properly: Use Debug, Info, Warn, Error, Fatal.
  • Monitor log size & rotation to prevent disk bloat.

Conclusion

With Serilog, you get powerful, extensible logging out of the box. It gives you structure, performance, and flexibility — everything you need for enterprise-ready applications.

Want to see more on advanced sinks like Elasticsearch + Kibana, or how to build a correlation ID middleware? Drop a comment below!

Top comments (0)