Serilog
Home
Simple .NET logging with fully-structured events. Contribute to serilog/serilog development by creating an account on GitHub.
https://github.com/serilog/serilog/wiki
Package
dotnet add package Serilog.Extensions.Hosting
dotnet add package Serilog.Settings.Configuration
// you will also need to add a sink aka
dotnet add package Serilog.Sinks.Console
Initialisation for Host
builder.Services.AddSerilog((services, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(builder.Configuration));
Configuration
Serilog settings must be at root
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
},
{
"Name": "File",
"Args": {
"path": "Logs/interpreter.ndjson",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithEnvironmentName" ],
"Destructure": [
{ "Name": "ToMaximumDepth", "Args": { "maximumDestructuringDepth": 4 } },
{ "Name": "ToMaximumStringLength", "Args": { "maximumStringLength": 100 } },
{ "Name": "ToMaximumCollectionCount", "Args": { "maximumCollectionCount": 10 } }
],
"Properties": {
"Application": "interpreter"
}
}
}
How to write logs
https://github.com/serilog/serilog/wiki/Writing-Log-Events
Log.Information("The time is {Now}", DateTime.Now);Log events should use PascalCase
Log Levels
- Verbose - tracing information and debugging minutiae; generally only switched on in unusual situations
- Debug - internal control flow and diagnostic state dumps to facilitate pinpointing of recognised problems
- Information - events of interest or that have relevance to outside observers; the default enabled minimum logging level
- Warning - indicators of possible issues or service/functionality degradation
- Error - indicating a failure within the application or connected system
- Fatal - critical errors causing complete failure of the application