Introduction to Serilog: What It Is and Why You Need It
Serilog is a powerful, flexible, and modern library for structured logging in the .NET ecosystem (C#, F#, VB.NET). Unlike classic logging frameworks (e.g., NLog or log4net) that work with flat text messages, Serilog is designed from the ground up to work with structured data.
Why do you need Serilog?
- Structured logging: You log not just a string, but objects with named properties. This allows you to efficiently filter, search, and analyze logs in systems like Elasticsearch, Seq, Datadog.
- Flexibility (Sinks): Serilog uses the concept of "sinks". You can send logs to the console, file, database, cloud services (Azure, AWS), Elasticsearch, Telegram, Slack, and hundreds of other destinations — simply by adding the required package.
- Performance: Serilog is optimized for high-load applications. It uses deferred string formatting (message templates) to avoid unnecessary memory allocations if the logging level is not active.
- Ease of configuration: Configuration via code or JSON file. Minimal code to get started — just a couple of lines.
- Diagnostics: Allows you to easily add contextual information (e.g., user ID, order number, method name) to every log.
Installing Serilog
Serilog is distributed via NuGet, the package manager for .NET. Installation is done through the Package Manager Console or .NET CLI.
Installation via .NET CLI (recommended):
dotnet add package Serilogdotnet add package Serilog.Sinks.Consoledotnet add package Serilog.Sinks.Filedotnet add package Serilog.Settings.Configurationdotnet add package Serilog.AspNetCoreInstallation via Package Manager Console (Visual Studio):
Install-Package SerilogInstall-Package Serilog.Sinks.ConsoleInstall-Package Serilog.Sinks.FileInstall-Package Serilog.Settings.ConfigurationInstall-Package Serilog.AspNetCoreExplanation of packages:
- Serilog — the core library.
- Serilog.Sinks.Console — sink for console output.
- Serilog.Sinks.File — sink for writing to files.
- Serilog.Settings.Configuration — allows reading settings from
appsettings.json. - Serilog.AspNetCore — integration with ASP.NET Core (adds HTTP context to logs).
Note for PythonLib readers: In Python, the equivalent of installing via pip is pip install serilog (but Serilog is a .NET library; Python has its own tools, e.g., structlog).
Quick Start: Your First Log in 30 Seconds
Create a .NET console application and add the following code to Program.cs:
using Serilog;
// 1. Configure SerilogLog.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .WriteTo.File("logs/myapp.log", rollingInterval: RollingInterval.Day) .CreateLogger();
try{ Log.Information("Application started successfully.");
int userId = 42; string userName = "Alice"; Log.Information("User {UserId} with name {UserName} logged in.", userId, userName);
// Example with error try { throw new InvalidOperationException("Invalid operation"); } catch (Exception ex) { Log.Error(ex, "An error occurred while performing operation for user {UserId}", userId); }
Log.Warning("This is a warning. Level: {Leve