serilog-enrichers-aspnetcore-httpcontext
serilog-enrichers-aspnetcore-httpcontext copied to clipboard
Whats the difference between this and the built in Serilog AspNetCore function?
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?
@xantari +1
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.
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?