asp.net-core - 带有自定义模板和 JSON 格式化程序的 Serilog

标签 asp.net-core serilog asp.net-core-2.1

我在 ASP.NET Core 2.1 中使用 Serilog,并通过 appsettings.json 配置它.

默认模板不包括 {SourceContext} ,所以我使用我自己的模板,其中包含它。但我也希望使用 JSON 进行结构化日志记录。

我在 Serilog wiki 的某个地方读到我无法指定 formatter (用于 JSON 输出)和 outputTemplate同时。

所以我不能这样做,例如:

"outputTemplate": "{Timestamp:yyyy-MM-dd} {Level:u3} {SourceContext} {Message:lj}{NewLine}{Exception}",
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"

那么如何获得 JSON 输出,同时获得 SourceContext我需要的数据?

最佳答案

我不确定自提出问题以来发生了什么变化,但现在我确实得到了 SourceContext多变的。我正在使用 Serilog.AspNetCore版本3.4.0 , 在 ASP.NET Core 5 上。
这有效:

{
  "Name": "File",
  "Args": {
    // ...
    //"outputTemplate": "",                          // DO NOT USE THIS
    "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
  }
}
只是不要使用 outputTemplateformatter同时。
这个配置给了我变量 @t , @mt , @l , SourceContext .
备注 @l不包括在 Information 中级别,因为这被认为是默认值。

关于asp.net-core - 带有自定义模板和 JSON 格式化程序的 Serilog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51211127/

相关文章:

c# - 模型枚举字段的 ASP.NET 自定义错误消息

c# - 为什么 web.config 是在发布时创建的,而不是在 Asp.Net Core API 中构建项目时创建的?

docker - 对 docker 容器中的 dotnet 进程进行故障排除

c# - 登录后阶段超时时间已过

c# - Serilog:如何记录,将具有不同 "origins"的消息记录到不同的文件

c# - 如何通过进程ID设置滚动文件名唯一

c# - 有没有办法关闭 Hangfire 使用 serilog 进行的日志记录?

asp.net-core - ASP.NET Core中的项目之间共享配置文件

c# - 网络核心 : Create Generic Repository Interface Id Mapping for All Tables Auto Code Generation

c# - 在 Angular ASP.NET Core 2.0 应用程序中使用 LinkedIn api 和 Oauth 2.0 进行身份验证