Search code examples
c#.netserilogserilog.sinks.mssqlserver

Serilog SQL Server Sink use UTC for TimeStamp


When using Serilog.Sinks.MSSqlServer configured via JSON file the timestamp isn't in UTC.

In code doing this work:

columnOptions.TimeStamp.ConvertToUtc = true;

How do I do the same thing in a JSON config file?

Here is the complete current JSON config file:

{
    "Serilog": {
        "MinimumLevel": "Verbose",
        "WriteTo": [
            {
                "Name": "Console",
                "Args": {
                    "restrictedToMinimumLevel": "Debug"
                }
            },
            {
                "Name": "File",
                "Args": {
                    "path": "\\Logs\\log.log",
                    "rollingInterval": "Day",
                    "restrictedToMinimumLevel": "Error"
                }
            },
            {
                "Name": "MSSqlServer",
                "Args": {
                    "restrictedToMinimumLevel": "Debug",
                    "connectionString": "XXXX",
                    "sinkOptions": {
                        "autoCreateSqlTable": true,
                        "tableName": "Logs"
                    },
                    "columnOptionsSection": {
                        "disableTriggers": true,
                        "clusteredColumnstoreIndex": false,
                        // Add an id column
                        "primaryKey": { "ColumnName": "Id" },
                        // Add the event in json for searching
                        "addStandardColumns": [ "LogEvent" ],
                        // This is the same info as the LogEvnt but in XML.
                        "removeStandardColumns": [ "Properties" ],
                        // Add a Process column 
                        "additionalColumns": [
                            {
                                "ColumnName": "Process",
                                "DataType": "varchar",
                                "DataLength": 255
                            }
                        ]
                    }
                }
            }
        ],
        "Enrich": [ "FromLogContext" ]
    }
}

Adding to UseUtcTimestamp in the args.sinkOptions doesn't work.

"Args": {
          "connectionString": "XXXX",
          "sinkOptions": {
            "UseUtcTimestamp": true
          }
        }

Solution

  • It should be in the columnOptionsSection (docs):

    "columnOptionsSection": {
        // ...
        "timeStamp": { "columnName": "Timestamp", "convertToUtc": true },
        // ...
    }