serilog-enrichers-aspnetcore-httpcontext icon indicating copy to clipboard operation
serilog-enrichers-aspnetcore-httpcontext copied to clipboard

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

Open xantari opened this issue 4 years ago • 3 comments

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 avatar Mar 11 '20 20:03 xantari

@xantari +1

garild avatar Mar 18 '20 11:03 garild

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.

IngoVals avatar Apr 20 '20 14:04 IngoVals

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?

terryaney avatar Jan 15 '21 00:01 terryaney