Skip to content

Instantly share code, notes, and snippets.

@bitbonk
Last active November 11, 2019 03:43
Show Gist options
  • Save bitbonk/45a5eb6b17b3ebb13fb038f522409da9 to your computer and use it in GitHub Desktop.
Save bitbonk/45a5eb6b17b3ebb13fb038f522409da9 to your computer and use it in GitHub Desktop.
Example Serilog Config with C# and JSON

C# config:

var logConfig = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .Enrich.FromLogContext()
    .WriteTo.ColoredConsole(LogEventLevel.Error, "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}")
    .WriteTo.Logger(
        c => c.WriteTo.ColoredConsole(LogEventLevel.Information, "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}")
            .Filter.ByIncludingOnly("SourceContext = 'Microsoft.Hosting.Lifetime'"))
    .WriteTo.File(
        "logs\\log-.log",
        LogEventLevel.Information,
        rollingInterval: RollingInterval.Day,
        rollOnFileSizeLimit: true,
        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\t{Level:u3}\t{SourceContext}\t{Message:lj}{NewLine}{Exception}")
    .WriteTo.Logger(
        domainLogConfig => domainLogConfig
            .WriteTo.File(
                "logs\\domain\\log-.log",
                rollingInterval: RollingInterval.Day,
                rollOnFileSizeLimit: true,
                outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\t{Level:u3}\t{DomainContext}\t{Message:lj}{NewLine}")
            .Filter.ByIncludingOnly("DomainContext is not null"))
    .CreateLogger();
builder.AddSerilog(logConfig, true);

JSON config:

  "Serilog": {
    "Using": [
      "Serilog",
      "Serilog.Sinks.File",
      "Serilog.Sinks.ColoredConsole",
      "Serilog.Filters.Expressions"
    ],
    "Enrich": [
      "FromLogContext"
    ],
    "MinimumLevel": "Verbose",
    "WriteTo": [
      {
        "Name": "ColoredConsole",
        "Args": {
          "restrictedToMinimumLevel": "Error",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}"
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "ColoredConsole",
                "Args": {
                  "restrictedToMinimumLevel": "Information",
                  "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}"
                }
              }
            ],
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "SourceContext = 'Microsoft.Hosting.Lifetime'"
                }
              }
            ]
          }
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "logs\\log-.log",
          "restrictedToMinimumLevel": "Information",
          "rollingInterval": "Day",
          "rollOnFileSizeLimit": "True",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\t{Level:u3}\t{SourceContext}\t{Message:lj}{NewLine}{Exception}"
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "logs\\domain\\log-.log",
                  "restrictedToMinimumLevel": "Information",
                  "rollingInterval": "Day",
                  "rollOnFileSizeLimit": "True",
                  "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\t{Level:u3}\t{DomainContext}\t{Message:lj}{NewLine}"
                }
              }
            ],
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "DomainContext is not null"
                }
              }
            ]
          }
        }
      }
    ]
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment