trenoncourt / serilog-enrichers-aspnetcore-httpcontext

Enriches Serilog events with Aspnetcore HttpContext.
MIT License
38 stars 17 forks source link

Whats the difference between this and the built in Serilog AspNetCore function? #12

Open xantari opened 4 years ago

xantari commented 4 years ago

In serilog.aspnetcore nuget package you can do this:

app.UseSerilogRequestLogging(options =>
{
    options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
    {
        diagnosticContext.Set("UserAgent", httpContext.Request.Headers["User-Agent"].ToString());
        diagnosticContext.Set("HttpRequestClientHostIP", httpContext.Connection.RemoteIpAddress);
        diagnosticContext.Set("HttpRequestUrl", httpContext.Request.GetDisplayUrl());
        diagnosticContext.Set("UserName", httpContext.User.Identity.Name == null ? "(anonymous)" : httpContext.User.Identity.Name);
    };
});

And the logs for HTTP requests/responses will have that extra information in it.

What does this nuget do in addition?

garild commented 4 years ago

@xantari +1

IngoVals commented 4 years ago

I think this package precedes the UseSerilogRequrestLogging. It also has a lot of helper methods for logging the HttpContext. However it introduces a UseSerilog method which now exists in Serilog itself. I think this package would need a update to be better compatible with 3.0.

terryaney commented 3 years ago

Reading/testing code from this answer (https://stackoverflow.com/a/59292116/166231), the constructor with the IHttpContextAccessor param seems to never used via the asp.net dependency injection. But rather TraceIdentifierEnricher is constructed one time from the Program.Main with the empty constructor (creating the HttpContextAccessor itself). It seems to work. Looking at this repo, it seems to be doing something much more complicated. Know Pros/Cons of each method?