why we use serilog in dotnet when we have built in logging class
1️⃣ Default Logging in .NET
- .NET Core / .NET 5+ provides a built-in logging framework in the
Microsoft.Extensions.Loggingnamespace. - Supports multiple log levels:
Trace,Debug,Information,Warning,Error,Critical. - Supports pluggable providers: Console, Debug, EventSource, EventLog, etc.
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:
| Feature | Built-in Logger | Serilog |
|---|---|---|
| Structured Logging | No (just text messages) | Yes (key-value pairs, JSON) |
| Sinks (Outputs) | Limited | Hundreds of sinks: Console, File, Seq, Elasticsearch, etc. |
| Asynchronous logging | Limited | Built-in async logging for high performance |
| Custom Formatting | Basic | Rich formatting with templates ({UserId}, {OrderId}) |
| Enrichment | Limited | Easily 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:
- Can also output JSON for log aggregation tools:
{
"Timestamp": "2025-10-06T10:30:45Z",
"Level": "Information",
"Message": "User logged in",
"UserId": 123,
"MachineName": "SERVER01"
}
4️⃣ Advantages of Serilog in real-world apps
- Structured logging for analytics – easier to query in ELK/Seq.
- Multiple sinks – write logs to files, databases, cloud services, dashboards.
- Contextual enrichment – automatically include environment, user, thread info.
- High-performance async logging – important for large applications.
- 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