serilog-enrichers-clientinfo
                                
                                
                                
                                    serilog-enrichers-clientinfo copied to clipboard
                            
                            
                            
                        CorrelationId not working with .NET 8
dotnet new webapi -o ApiTest -f net8.0
using Serilog;
using Serilog.Events;
using Serilog.Exceptions;
using Serilog.Formatting.Json;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// SERILOG ================================================================================================
builder.Services.AddSerilog((services, logger) => logger
    .MinimumLevel.Information()
    .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
    .Enrich.FromLogContext()
    .Enrich.WithThreadId()
    .Enrich.WithMachineName()
    .Enrich.WithEnvironmentUserName()
    .Enrich.WithEnvironmentName()
    .Enrich.WithMachineName()
    .Enrich.WithProcessId()
    .Enrich.WithProcessName()
    .Enrich.WithExceptionDetails()
    .Enrich.WithClientIp()
    .Enrich.WithCorrelationId(addValueIfHeaderAbsence: true)
    .WriteTo.File(formatter: new JsonFormatter(), "log.json")
    .WriteTo.Console()
    .WriteTo.OpenTelemetry(options =>
    {
        options.Endpoint = "http://host.docker.internal:4317";
        options.ResourceAttributes = new Dictionary<string, object>
        {
            ["service.name"] = "basket-api"
        };
    })
);
// ========================================================================================================
var app = builder.Build();
// SERILOG ================================================================================================
app.UseSerilogRequestLogging();
// ========================================================================================================
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("/", () => "TEST API!");
app.Run();
No correlationId on json:
{
    "Timestamp": "2024-05-13T04:58:01.2798131-03:00",
    "Level": "Information",
    "MessageTemplate": "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms",
    "TraceId": "b16858179222d2ad68f368015c24c708",
    "SpanId": "dd04f5cda4b0ad0f",
    "Properties": {
        "RequestMethod": "GET",
        "RequestPath": "/",
        "StatusCode": 200,
        "Elapsed": 13.5145,
        "SourceContext": "Serilog.AspNetCore.RequestLoggingMiddleware",
        "RequestId": "0HN3J49KKL45L:00000001",
        "ConnectionId": "0HN3J49KKL45L",
        "ThreadId": 15,
        "MachineName": "DESKTOP",
        "EnvironmentUserName": "DESKTOP\\tfsan",
        "EnvironmentName": "Development",
        "ProcessId": 21532,
        "ProcessName": "ApiTest"
    },
    "Renderings": {
        "Elapsed": [
            {
                "Format": "0.0000",
                "Rendering": "13.5145"
            }
        ]
    }
}
                                    
                                    
                                    
                                
@tfsantosbr - it seems you're missing
builder.Services.AddHttpContextAccessor();
which is required for enriching with the client info, as per the documentation
@tfsantosbr feel free to reopen if the problem is not fixed.