【问题标题】:How to get Serilog to enrich logs如何让 Serilog 丰富日志
【发布时间】:2019-03-05 19:49:23
【问题描述】:

我已将 Serilog 设置为从 appsettings.json 读取其配置:

return WebHost.CreateDefaultBuilder(args)
   .UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); })

appsettings.json 具有以下指定丰富器的相关信息:

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": {
          "pathFormat": "%WIDGETSAPIBASEDIR%\\logs\\log-{Date}.txt"
        }
      },
      {
        "Name": "Debug"
      }
    ],
    "Enrich": [ "CorrelationId" ]
  }
}

生成的日志不包含Enrich 属性中指定的任何数据。

我已导入 Serilog.Enrichers.CorrelationId,但仍然一无所获。

我也试过"Enrich": [ "WithCorrelationId" ]。我也尝试过其他的浓缩器("FromLogContext", "WithMachineName", "WithThreadId"),但仍然一无所获。

我错过了什么?

【问题讨论】:

  • 是否添加了相关的 NuGet 包?例如Serilog.Enrichers.Thread?
  • 您是否将输出模板添加到 args 中,例如 "outputTemplate": "\"{Timestamp:yyyy-MM-dd HH:mm:ss.fff} | {shortloglevel} | {SourceContext} | {RequestId} | {请求路径} | {提供者系统} | {Message}{NewLine}{Exception}\".",
  • @SoumenMukherjee 你是说要让丰富者登录我必须提供 outputTemplate?
  • @AngryHacker ,好吧,由于某些原因,我在模板中使用了浓缩器,但模板不起作用,我确实试图找出原因,但文档并不是很好......我确实有一个使用的 LogEnricher 类。

标签: c# .net .net-core serilog asp.net-core-2.2


【解决方案1】:

这是我的配置文件的一个例子。请注意我有丰富的输出模板?

 "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Async",
        "Args": {
          "configure": [
            {
              "Name": "File",
              "Args": {
                "path": ".//Logs//app.log-.txt",
                "rollingInterval": "Day",
                "rollOnFileSizeLimit": true,
                "outputTemplate": "[{Timestamp :HH:mm:ss} {Level:u3} {SourceContext, -20} {ProcessId} {ProcessName} {ThreadId}] {Message}\n{Exception}",
            ,
                "shared": true
              }
            },
            {
              "Name": "Console"
            }
          ]
        }
      },
      {
        "Name": "SpectreConsole",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} [{Level:u3} {ProcessId}] {Message:lj}{NewLine}{Exception}",
          "minLevel": "Verbose"
        }
      }
    ],
    "Enrich": [
      "FromLogContext",
      "WithMemoryUsage",
      "WithProcessId",
      "WithProcessName",
      "WithThreadId",
      "WithThreadName"
    ]
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 2019-12-18
    相关资源
    最近更新 更多