【问题标题】:Serilog ThreadId is always output as 1Serilog ThreadId 始终输出为 1
【发布时间】:2021-04-09 02:18:24
【问题描述】:

我在 outputTemplate 中有一个 ThreadId,但日志始终将其记录为 1。我有多个应用程序实例同时运行到同一个日志中,因此希望我可以根据 ThreadId 分离实例。

谁能建议如何解决这个问题? ThreadName 在应用程序设置一些东西并识别它正在运行的区域后被分配,因此不是最好的分隔符。也可以在一个区域内同时运行多个功能。因此需要 ThreadId

Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(builder.Build())
            .Enrich.FromLogContext()
            .Enrich.WithThreadId()
            .Enrich.WithThreadName()
            .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ThreadId}> {Message:lj}{NewLine}{Exception}")      
            .WriteTo.File(@".\log\log.txt", 
                             rollingInterval: RollingInterval.Day, 
                             shared: true,
                             outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ThreadId}> {Message:lj}{NewLine}{Exception}")
            .CreateLogger();

日志输出

2021-04-09 06:30:09.059 [INF] <blackstone> <1> Action : Task A
2021-04-09 06:30:09.059 [INF] <wavell> <1> Action : Task A
2021-04-09 06:30:09.060 [INF] <forest> <1> Action : Task A
2021-04-09 06:30:09.130 [INF] <wavell> <1> Loading CentreDetails
2021-04-09 06:30:09.130 [INF] <forest> <1> Loading CentreDetails
2021-04-09 06:30:09.132 [INF] <blackstone> <1> Loading CentreDetails
2021-04-09 06:30:09.560 [INF] <wavell> <1> Loading ParentDetails
2021-04-09 06:30:09.554 [INF] <blackstone> <1> Loading ParentDetails
2021-04-09 06:30:09.560 [INF] <forest> <1> Loading ParentDetails

【问题讨论】:

  • 什么是'separate the instances'中的实例?
  • 如果您将Thread.CurrentThread.ManagedThreadId 添加到您的日志消息中,它会显示什么?
  • @tymtam 我希望 Serilog 将线程 ID 放入它的模式中。每次我调用 Log.Information("Action : Task A") 时都没有明确记录它。
  • 明白,我只是想看看id是否匹配。

标签: c# logging serilog-sinks-file


【解决方案1】:

听起来你想要Serilog.Enrichers.ProcessEnrich.WithProcessId() 而不是WithThreadId()

【讨论】:

  • Serilog.Enrichers.Process nuget 已在解决方案中。我已经在 LoggerConfiguration() 中有 .Enrich.WithThreadId()。你有一个例子来说明你的意思吗?
【解决方案2】:

好的,我遵循@Nicholas Blumhardt 的目标......

添加了 Serilog.Enrichers.Process nuget 并将代码更新到此

Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(builder.Build())
                .Enrich.FromLogContext()
                .Enrich.WithProcessId()
                .Enrich.WithThreadName()
                .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ProcessId}> {Message:lj}{NewLine}{Exception}")      
                .WriteTo.File(@".\log\log.txt", 
                                 rollingInterval: RollingInterval.Day, 
                                 shared: true,
                                 outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ProcessId}> {Message:lj}{NewLine}{Exception}")
                .CreateLogger();

日志现在推出一个唯一的进程 ID

【讨论】:

    【解决方案3】:

    这对我有用:

    nuget 包

    <PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
    

    appsettings.json {ThreadId}

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information"
        }
      },
      "Serilog": {
        "MinimumLevel": {
          "Default": "Information",
          "Override": {
            "Microsoft": "Error"
          }
        },
          {
            "Name": "File",
            "Args": {
              "path": "/log/api.log",
              "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] Th:{ThreadId} {Message} {NewLine}{Exception}",
              "rollingInterval": "Day"
            }
          }
        ],
        "Properties": {
          "Application": "acme"
        }
      }
    }
    

    启动 .Enrich.WithThreadId()

    public Startup(IConfiguration configuration)
    {
        Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).Enrich.WithThreadId()
            .CreateLogger();
        Configuration = configuration;
    }
    

    示例日志

    2022-03-11 14:49:25.425 -05:00 [Information] Th:27 foo
    2022-03-11 14:49:25.428 -05:00 [Information] Th:27 Bar
    

    【讨论】:

      猜你喜欢
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2014-03-04
      • 2016-07-15
      相关资源
      最近更新 更多