使用 Serilog 在日志文件中添加新属性

问题描述 投票:0回答:1

使用 Serilog,我想在日志文件中拥有一个新属性 Identifier(位于“Timestamp”之后),如下所示:

{"Timestamp":"2019-10-11T12:18:51.1488404+08:00", "Identifier":"", "Level":"Information","MessageTemplate":"Log {CurrentThreadId}","Properties":{"CurrentThreadId":4,"MainThreadId":1}}

我添加了 LogContext.PushProperty,如下所示。

builder.Services.AddProblemDetails(options =>
        options.CustomizeProblemDetails = (context) =>
        {

            var mathErrorFeature = context.HttpContext.Features
                                                       .Get<MathErrorFeature>();
            if (mathErrorFeature is not null)
            {
                (string Detail, string Type) details = mathErrorFeature.MathError switch
                {
                    MathErrorType.DivisionByZeroError =>
                    ("Divison by zero is not defined.",
                                          "https://wikipedia.org/wiki/Division_by_zero"),
                    _ => ("Negative or complex numbers are not valid input.",
                                          "https://wikipedia.org/wiki/Square_root")
                };

                context.ProblemDetails.Type = details.Type;
                context.ProblemDetails.Title = "Bad Input";
                context.ProblemDetails.Detail = details.Detail;

        var identifier = $"Iden:{Guid.New()}";
        LogContext.PushProperty("Identifier", identifier);
            }
        }
    );

这是否可以按照我希望将标识符附加到日志文件的方式工作?如果没有,如何在 Serilog 中做到这一点?

.net-core serilog serilog-aspnetcore
1个回答
0
投票

快速回答是肯定的。但是,我认为您需要将“标识符”作为所有生成的日志条目的全局值,如果是这样,您必须稍微不同。

顺便说一句,LogContext.PushProperty 可以帮助您动态向日志上下文添加属性,例如使用将生成的日志的范围。

首先,您必须配置 Serilog Enrichers 以丰富上下文中的日志。因此,必须在配置时将该功能添加到记录器中。

  1. 将标识符作为全局属性来执行以下操作:

    //Configure your logging
    var log = new LoggerConfiguration()
                    .Enrich.WithProperty("Identifier", $"Iden:{Guid.NewGuid().ToString()}"))
    
  2. 或者,如果您动态需要它,请执行以下操作:

     //Configure your logging
     var log = new LoggerConfiguration()
                      .Enrich.FromLogContext()
    
    
     builder.Services.AddProblemDetails(options =>
     options.CustomizeProblemDetails = (context) =>
     {
    
         var mathErrorFeature = context.HttpContext.Features
                                                    .Get<MathErrorFeature>();
         if (mathErrorFeature is not null)
         {
             (string Detail, string Type) details = mathErrorFeature.MathError switch
             {
                 MathErrorType.DivisionByZeroError =>
                 ("Divison by zero is not defined.",
                                       "https://wikipedia.org/wiki/Division_by_zero"),
                 _ => ("Negative or complex numbers are not valid input.",
                                       "https://wikipedia.org/wiki/Square_root")
             };
    
             context.ProblemDetails.Type = details.Type;
             context.ProblemDetails.Title = "Bad Input";
             context.ProblemDetails.Detail = details.Detail;
    
             var identifier = $"Iden:{Guid.New()}";
    
             using (LogContext.PushProperty("Identifier", identifier))
             {
                 log.Information("This log entry carries the property Identifier");
             }
       }
     }
    );
    

另外,请查看此 GitHub wiki 页面。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.