'Check that Serilog is configured correctly

In my ASP.NET Core Web API application I have a Serilog set up to log different types of events (Information, Warning etc) into different tables in a SQL Server database with subloggers and also all events goes into one File.

File logging works fine, but SQL Server logging is not. Dedicated tables in database are being created automatically, but nothing is being written into them. I use the sa login while connecting to database, so it is not about roles and permissions.

My current configuration is as follows:

"WriteTo": [
  {
    "Name": "Debug",
    "Args": {
      "outputTemplate": "[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:l} {NewLine}"
    }
  },
  {
    "Name": "File",
    "Args": {
      "Path": "D:/file.txt",
      "rollingInterval": "Day",
      "outputTemplate": "[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:l} {NewLine}"
    }
  },
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {
        "Filter": [
          {
            "Name": "ByIncludingOnly",
            "Args": {
              "expression": "(@Level = 'Error' or @Level = 'Fatal')"
            }
          }
        ],
        "WriteTo": [
          {
            "Name": "MSSqlServer",
            "Args": {
              "connectionString": "Data Source=.\\SQLEXPRESS;User Id=sa;Password=*****; Initial Catalog=LogsDb",
              "tableName": "NmsRecipesLogs_Errors",
              "autoCreateSqlTable": true,
              "batchPostingLimit": 1,
              "columnOptionsSection": {
                "removeStandardColumns": [ "MessageTemplate", "Properties" ]
              }
            }
          }
        ]
      }
    }
  },
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {
        "Filter": [
          {
            "Name": "ByIncludingOnly",
            "Args": {
              "expression": "(@Level = 'Information' or @Level = 'Warning')"
            }
          }
        ],
        "WriteTo": [
          {
            "Name": "MSSqlServer",
            "Args": {
              "connectionString": "Data Source=.\\SQLEXPRESS;User Id=sa;Password=****;Initial Catalog=LogsDb",
              "tableName": "NmsRecipesLogs_Info",
              "autoCreateSqlTable": true,
              "batchPostingLimit": 1,
              "columnOptionsSection": {
                "removeStandardColumns": [ "MessageTemplate", "Properties" ]
              }
            }
          }
        ]
      }
    }
  }
]

I know that MSSQLServer sink does not write logs to the database immediately, so I set batchPostingLimit to 1, but with no effect.

The question is how to ensure that this configuration is properly consumed by application and doesn't have any mistakes?



Solution 1:[1]

Serilog has a SelfLog feature that allows you to output diagnostic information about the Serilog configuration. You can find out more information about this feature here.

#tl;dr you can output the diagnostic info like so...

Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pmcilreavy