You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Serilog.Sinks.MSSqlServer的JSON配置中设置UTC时间戳?

在JSON配置中为Serilog.Sinks.MSSqlServer设置UTC时间戳

要在JSON配置中实现代码里columnOptions.TimeStamp.ConvertToUtc = true;的效果,你需要在columnOptionsSection下添加timeStamp节点并配置convertToUtc属性,而非放在sinkOptions中。

修改后的完整JSON配置如下:

{
    "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" ],
                        // 配置UTC时间戳
                        "timeStamp": {
                            "convertToUtc": true
                        },
                        // Add a Process column 
                        "additionalColumns": [
                            {
                                "ColumnName": "Process",
                                "DataType": "varchar",
                                "DataLength": 255
                            }
                        ]
                    }
                }
            }
        ],
        "Enrich": [ "FromLogContext" ]
    }
}

说明

之前在sinkOptions中添加"UseUtcTimestamp": true无效,是因为该配置项并不属于sinkOptions的参数范围。正确的配置位置是在columnOptionsSection下的timeStamp节点中设置convertToUtc: true,这和代码里columnOptions.TimeStamp.ConvertToUtc = true;的逻辑完全对应。

内容的提问来源于stack exchange,提问作者JoeMcAlly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 16:43:35