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

Hosted example docs

builder.Services.AddSerilog((services, loggerConfiguration) => loggerConfiguration
    .ReadFrom.Configuration(builder.Configuration));

Configuration

Config docs

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

  1. Verbose - tracing information and debugging minutiae; generally only switched on in unusual situations
  1. Debug - internal control flow and diagnostic state dumps to facilitate pinpointing of recognised problems
  1. Information - events of interest or that have relevance to outside observers; the default enabled minimum logging level
  1. Warning - indicators of possible issues or service/functionality degradation
  1. Error - indicating a failure within the application or connected system
  1. Fatal - critical errors causing complete failure of the application