Search code examples
serilog

Using RestrictedToMinimumLevel to Override the Default Minimum Level in Serilog


I have configured Serilog to have a default MinimumLevel of Verbose. But I want to confine the logging to SQL Server to Warning (and up).

My config (in JSON) looks like this:

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer", "My.Framework.AspNet" ],
    "Enrich": [ "FromLogContext", "WithEventType" ],
    "MinimumLevel": {
      "Default": "Verbose"
    },
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "restrictedToMinimumLevel": "Warning",
        "Args": {
          "connectionString": "Server=yada yada yada ;MultipleActiveResultSets=True",
          "tableName": "ErrorLogs",
          "autoCreateSqlTable": false,
          "columnOptionsSection": {
            "disableTriggers": true,
            "clusteredColumnstoreIndex": false,
            "primaryKeyColumnName": "Id",
            "addStandardColumns": [ "LogEvent" ],
            "removeStandardColumns": [ "Properties" ],
            "additionalColumns": [
              {
                "ColumnName": "EventType",
                "DataType": "bigint",
                "AllowNull": true
              }
            ],
            "id": {
              "columnName": "Id",
              "nonClusteredIndex": true
            },
            "eventType": {
              "columnName": "EventType"
            },
            "message": {
              "columnName": "Message"
            },
            "messageTemplate": {
              "columnName": "MessageTemplate"
            },
            "level": {
              "columnName": "Level",
              "storeAsEnum": false
            },
            "timeStamp": {
              "columnName": "TimeStamp",
              "convertToUtc": true
            },
            "exception": {
              "columnName": "Exception"
            },
            "logEvent": {
              "columnName": "LogEvent"
            }
          }
        }        
      }
    ],
    "Properties": {
      "Application": "App API"
    }
  }
}

Upon inspecting my logs, I'm seeing everything from Verbose up.

Is there something I'm doing wrong with the restrictedToMinimumLevel property? I also tried placing it inside the Args property, with no luck.

Cheers


Solution

  • I haven't tried it but upon this GitHub issue I think restrictedToMinimumLevel should be in Args. So change your WriteTo configuration like this:

    "WriteTo": [
          {
            "Name": "MSSqlServer",
            "Args": {
              "restrictedToMinimumLevel": "Warning",
              "connectionString": "Server=yada yada yada ;MultipleActiveResultSets=True",
              "tableName": "ErrorLogs",
              "autoCreateSqlTable": false,
              "columnOptionsSection": {
                "disableTriggers": true,
                "clusteredColumnstoreIndex": false,
                "primaryKeyColumnName": "Id",
                "addStandardColumns": [ "LogEvent" ],
                "removeStandardColumns": [ "Properties" ],
                "additionalColumns": [
                  {
                    "ColumnName": "EventType",
                    "DataType": "bigint",
                    "AllowNull": true
                  }
                ],
                "id": {
                  "columnName": "Id",
                  "nonClusteredIndex": true
                },
                "eventType": {
                  "columnName": "EventType"
                },
                "message": {
                  "columnName": "Message"
                },
                "messageTemplate": {
                  "columnName": "MessageTemplate"
                },
                "level": {
                  "columnName": "Level",
                  "storeAsEnum": false
                },
                "timeStamp": {
                  "columnName": "TimeStamp",
                  "convertToUtc": true
                },
                "exception": {
                  "columnName": "Exception"
                },
                "logEvent": {
                  "columnName": "LogEvent"
                }
              }
            }        
          }
        ]