Home  Dotnet   Why we use ...

why we use serilog in dotnet when we have built in logging class


1️⃣ Default Logging in .NET

Example: Built-in logging

using Microsoft.Extensions.Logging;

var loggerFactory = LoggerFactory.Create(builder => {
    builder.AddConsole();
});

ILogger logger = loggerFactory.CreateLogger<Program>();

logger.LogInformation("App started");
logger.LogWarning("This is a warning");

✅ Works fine for basic logging needs.


2️⃣ Why use Serilog?

Serilog is a structured logging library for .NET with additional features that the default logger doesn’t provide out-of-the-box.

Top Reasons:

FeatureBuilt-in LoggerSerilog
Structured LoggingNo (just text messages)Yes (key-value pairs, JSON)
Sinks (Outputs)LimitedHundreds of sinks: Console, File, Seq, Elasticsearch, etc.
Asynchronous loggingLimitedBuilt-in async logging for high performance
Custom FormattingBasicRich formatting with templates ({UserId}, {OrderId})
EnrichmentLimitedEasily add contextual info (MachineName, ThreadId)

3️⃣ Example: Serilog Setup

using Serilog;

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Information()
    .Enrich.WithMachineName() // add context
    .WriteTo.Console()
    .WriteTo.File("logs/app.log", rollingInterval: RollingInterval.Day)
    .CreateLogger();

Log.Information("User {UserId} logged in", 123);
Log.Warning("Disk space is low on {Drive}", "C:");

Output (structured):

[10:30:45 INF] User 123 logged in
[10:31:00 WRN] Disk space is low on C:
{
  "Timestamp": "2025-10-06T10:30:45Z",
  "Level": "Information",
  "Message": "User logged in",
  "UserId": 123,
  "MachineName": "SERVER01"
}

4️⃣ Advantages of Serilog in real-world apps

  1. Structured logging for analytics – easier to query in ELK/Seq.
  2. Multiple sinks – write logs to files, databases, cloud services, dashboards.
  3. Contextual enrichment – automatically include environment, user, thread info.
  4. High-performance async logging – important for large applications.
  5. Flexible configuration – JSON, code, or appsettings.

5️⃣ Interview Answer

“.NET has a built-in logging framework (Microsoft.Extensions.Logging), which is sufficient for simple apps. Serilog is used when we need structured logging, multiple sinks, contextual enrichment, and high-performance async logging, making logs easier to query, monitor, and integrate with analytics platforms.”


Published on: Oct 06, 2025, 12:00 AM  
 

Comments

Add your comment