AngryHacker
AngryHacker

Reputation: 61626

Writing custom columns to MS SQL Server with Serilog sink

I am trying to use Serilog.Sinks.MSSqlServer sink to write custom columns to log table. Per documentation, I've added a columnOptionsSection:

"columnOptionsSection": {
  "additionalColumns": [
    {
      "ColumnName": "MachineName",
      "DataType": "nvarchar",
      "DataLength": 100
    }
  ]
}

and I have "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ] further down (and it works fine with File sink).

The log is written but MachineName column is NULL. @AlexRiabov states that the current version of the sink doesn't have doesn't have Microsoft.Extensions.Configuration support. This is contradicted by the documentation which has an entire section devoted to it. He also states that updating to the latest dev version resolves the issue - however, that version throws exceptions so I can't use it.

So my questions...

  1. Is it possible to define custom columns to use with the MSSqlServer sink from appsettings.json? If so, how?

  2. Is Serilog.Sinks.MSSqlServer sink still being worked on? Last major release appears to have been over a year ago.

Upvotes: 1

Views: 4455

Answers (1)

Phil Boyd
Phil Boyd

Reputation: 390

5.1.3-dev-00236 worked for me

make sure you have the Environment enrichers library added

my appSettings json:

    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "WriteTo": [{
            "Name": "MSSqlServer",
            "Args": {
                "connectionString": --removed--,
                "schemaName": "dbo",
                "tableName": "Logs",
                "autoCreateSqlTable": false,
                "restrictedToMinimumLevel": "Debug",
                "batchPostingLimit": 5,
                "period": "0.00:00:02",
                "columnOptionsSection": {
                    "addStandardColumns": [ "LogEvent" ],
                    "removeStandardColumns": [ "MessageTemplate", "Properties" ],
                    "additionalColumns": [
                        {
                            "ColumnName": "UtcTimeStamp",
                            "DataType": "datetimeoffset",
                            "AllowNull": false
                        },
                        {
                            "ColumnName": "MachineName",
                            "DataType": "nvarchar",
                            "DataLength": 128,
                            "AllowNull": false
                        }
                    ]
                }
            }
        }]

Upvotes: 3

Related Questions