serilog日期为UTC格式

发布于 2025-02-10 06:28:24 字数 855 浏览 3 评论 0原文

我想将Serilog配置为以UTC格式进行日期日期,但目前使用了服务器的时区。我们正在使用以下配置文件:

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.File"
    ],
    "LevelSwitches": { "$controlSwitch": "Verbose" },
    "MinimumLevel": { "ControlledBy": "$controlSwitch" },
    "WriteTo": [
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "outputTemplate": "{Timestamp:yyyy-MM-dd:HH:mm:ss.ffff} - {Message,-20:l}{NewLine}{Exception}",
                  "path": "./logs/my.log"
                }
              }
            ]
          }
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithExceptionDetails", "ProcessId" ]
  }
}

我知道可以通过编写自定义浓度来完成,但是使用模板有任何简单的解决方案吗?

I want to configure Serilog to log date in UTC format but currently the timezone of the server is used. We are using the following configuration file:

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.File"
    ],
    "LevelSwitches": { "$controlSwitch": "Verbose" },
    "MinimumLevel": { "ControlledBy": "$controlSwitch" },
    "WriteTo": [
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "outputTemplate": "{Timestamp:yyyy-MM-dd:HH:mm:ss.ffff} - {Message,-20:l}{NewLine}{Exception}",
                  "path": "./logs/my.log"
                }
              }
            ]
          }
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithExceptionDetails", "ProcessId" ]
  }
}

I know it can be done by writing custom enricher, but is there any simple solution for this with a template?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乄_柒ぐ汐 2025-02-17 06:28:24

您可以将时间转换为UTC。但是您没有任何格式的选择。

"outputTemplate": "{Timestamp:u} - {Message,-20:l}{NewLine}{Exception}"

会给你的

2024-04-16 22:47:09Z - Message

是对我有用的JSON。

{
    "Serilog": {
        "MinimumLevel": "Verbose",
        "WriteTo": [
            {
                "Name": "Console",
                "Args": {
                    "restrictedToMinimumLevel": "Debug",
                    "outputTemplate": "{Timestamp:u} - {Message,-20:l}{NewLine}{Exception}"
                }
            }
        ],
        "Enrich": [ "FromLogContext" ]
    }
}

You can convert the time to UTC. But you don't have any options for the format.

"outputTemplate": "{Timestamp:u} - {Message,-20:l}{NewLine}{Exception}"

Will give you

2024-04-16 22:47:09Z - Message

Here is the json that worked for me.

{
    "Serilog": {
        "MinimumLevel": "Verbose",
        "WriteTo": [
            {
                "Name": "Console",
                "Args": {
                    "restrictedToMinimumLevel": "Debug",
                    "outputTemplate": "{Timestamp:u} - {Message,-20:l}{NewLine}{Exception}"
                }
            }
        ],
        "Enrich": [ "FromLogContext" ]
    }
}
安稳善良 2025-02-17 06:28:24

尝试{timestamp:g}

因此,在您的代码中:

"outputTemplate": "{Timestamp:G} - {Message,-20:l}{NewLine}{Exception}",

Try {Timestamp:G}

So, in your code:

"outputTemplate": "{Timestamp:G} - {Message,-20:l}{NewLine}{Exception}",
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文