Author : HASSAN MD TAREQ

Available configuration options

  • AuditTo: An optional feature of Auditing events not generally used with async style sinks (HTTP destinations).
  • Destructure: The ability to destructure events via message templates (https://messagetemplates.org)
  • Enrich: Used to add property data related to a specific event. Enrichers will implement ILogEventEnricher.
  • Filter: A filter will implement ILogEventFilter in order to filter events based on log event data.
  • ReadFrom: A way of applying configuration to the log configuration, common examples include from configuration files.
  • WriteTo: The way sinks are wired into the pipeline, as per the console example above. Sinks implement the ILogEventSink interface.
{
    "Serilog": {
      "Using": [],
      "MinimumLevel": "",
      "WriteTo": [
          { 
            "Name": "",
            "Args": { "": "" }
          }
      ]
      "Enrich": [],
      "Destructure": [],
      "Properties": {
      }
    }
}

See

Using

Sink names as array

{
    "Serilog": {
      "Using": ["Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Async", "Serilog.Sinks.Seq"],
    }
}

Notes

  • ‘Using’ become redundant when ‘WriteTo’ is used
  • ‘Using’ is for sinks without configuration, ‘WriteTo’ is for sinks with configuration

MinimumLevel

Default Level: Information

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
  }
}

Fluent API

Log.Logger = new LoggerConfiguration()
	.MinimumLevel.Information()
	.MinimumLevel.Override(MicrosoftLog, Warning)
	.MinimumLevel.Override(SystemtLog, Warning)
	.WriteTo.XXX()
	.CreateLogger();

See:

Overriding MinimumLevel Per Sink

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.File("log.txt")
    .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Information)
    .CreateLogger();

Output template

  • log-event properties and properties attached using enricher
  • {LogEvent.Prop} => {Timestamp}
  • {LogEvent.Prop:<format_specifier>} => {Timestamp:HH:mm:ss}

appsettings.json

{
    "Serilog": {

      "WriteTo": [
        { 
            "Name": "sink_name",
            "Args": {
              "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}",
            }
        },
      ]
    }
}

There is special handling for Log level with w|u|t)[1-4]

  • w - lowercase
  • u - uppercase
  • t - titlecase

Sample Configuration

appsettings.Development.json

{
  "Serilog": {
    "MinimumLevel": "Warning",
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:yyyy/MM/dd HH:mm:ss} {Level:u10}] {Message:lj} {NewLine}{Exception}{NewLine}",
          "theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Literate, Serilog.Sinks.Console"
        }
      },
      {
        "Name": "File",
        "Args": {
          "outputTemplate": "[{Timestamp:yyyy/MM/dd HH:mm:ss} {Level:u10}] {Message:lj} {NewLine}{Exception}{NewLine}",
          "path": "C:/Serilog/test.txt",
          "shared": "true",
          "fileSizeLimitBytes": "1000000",
          "rollOnFileSizeLimit": true,
          "flushToDiskInterval": "1"
        }
      }
    ]
  }
}