0

Using Serilog, I want to have a new property Identifier (right after "Timestamp") in the log file like below:

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

I have added LogContext.PushProperty like below.

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);
            }
        }
    );

Will this work the way I want the Identifier to be appended to the log file? If not, how to do that in Serilog?

1 Answer 1

0

The quick answer is yes. However, I think you need to have the "identifier" as a global value for all produced log entries, if so, you have to do it slightly differently.

BTW, the LogContext.PushProperty can help you to add properties to your log Context dynamically, like using a scope for the logs that will be produced.

First of all, you have to configure the Serilog Enrichers to enrich the logs from the context. So the feature must be added to the logger at configuration-time.

  1. To have the Identifier as a global property to do something like this:

    //Configure your logging
    var log = new LoggerConfiguration()
                    .Enrich.WithProperty("Identifier", $"Iden:{Guid.NewGuid().ToString()}"))
    
  2. Or, if you need it dynamically, do something like this:

     //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");
             }
       }
     }
    );
    

Also, check this GitHub wiki page.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.