Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog.Enrichers.ClientInfo not logging request headers

I am using Serilog.Enrichers.ClientInfo as Serilog enricher in two dotNet Core 6 web apis. Both apis have the same Serilog packages references, both have identical Serilog configuration. One api is loggnig headers properly, second does not. What can be an issue here?

Api 1 Configuration:

var loggers = new LoggerConfiguration()
                .MinimumLevel.Information()
                .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] {Level:u3} CLient IP: {ClientIp} Correlation Id: {CorrelationId} header-name: {Host} user: {UserName} {Message:lj}{NewLine}{Exception}")
                .Enrich.WithClientIp()
                .Enrich.WithCorrelationId()
                .Enrich.WithRequestHeader("Host")
                .Enrich.WithRequestHeader("UserName")
                .Enrich.WithRequestHeader("another-header-name", "SomePropertyName")
                .CreateLogger();
builder.Host.UseSerilog(loggers);
//
var app = builder.Build();
app.UseSerilogRequestLogging();

Api 1 controller test:

public async Task<IActionResult> Get(int id)
 {
     logger.LogInformation("CONTROLLER-1 TEST LOG");
     var getExamCommand = new GetExamQuery { Id = id };
     var exam = await mediator.Send(getExamCommand);
     return Ok(exam);
 }

Api 1 log (missing Ip, correlaion and test header name):

[11:39:50] INF CLient IP: Correlation Id: header-name: user: CONTROLLER-1 TEST LOG

Api 2 Configuration:

var loggers = new LoggerConfiguration()
                .MinimumLevel.Information()
                .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] {Level:u3} CLient IP: {ClientIp} Correlation Id: {CorrelationId} header-name: {Host} user: {UserName} {Message:lj}{NewLine}{Exception}")
                .Enrich.WithClientIp()
                .Enrich.WithCorrelationId()
                .Enrich.WithRequestHeader("Host")
                .Enrich.WithRequestHeader("UserName")
                .Enrich.WithRequestHeader("another-header-name", "SomePropertyName")
                .CreateLogger();
builder.Host.UseSerilog(loggers);

Api 2 controller test:

public async Task<LoginUserDetails> GetUser(int id)
        {
            logger.LogInformation("CONTROLLER-2 TEST LOG");
            return await userService.Get(id);
        }

Api 2 log (Ip, correlaion and test header are present (or null)):

[11:39:34] INF CLient IP: ::1 Correlation Id: null header-name: localhost:7082 user: null CONTROLLER-2 TEST LOG

Both Apis have those packages referenced:

api packages

I just can't figure out why first api is not enriched with headers, which are present in request. Any help would be appreciated.

Thanks.

like image 430
lasjan Avatar asked Sep 08 '26 06:09

lasjan


1 Answers

Omg. If anyone will ever stumble on this - do not forget to add builder.Services.AddHttpContextAccessor(); in your DI. Took me 2 days and writing custom log enricher to understand what is going on:)

like image 178
lasjan Avatar answered Sep 11 '26 12:09

lasjan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!