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

Add elapsedMs parameter to the EnrichDiagnosticContext delegate

Open jmenziessmith opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe. I want to use the diagnostic context to log a structured object containing enhanced request information. I would like this to include the elapsed duration, but the value is not accessible outside of the middleware. I believe this is a simple fix, to just include the elapsedMs value as a parameter to the EnrichDiagnosticContext action.

Describe the solution you'd like

RequestLoggingMiddleware.cs#L30

readonly Action<IDiagnosticContext, HttpContext>? _enrichDiagnosticContext; // current
readonly Action<IDiagnosticContext, HttpContext, double>? _enrichDiagnosticContext; // proposed

RequestLoggingMiddleware.cs#L85

_enrichDiagnosticContext?.Invoke(_diagnosticContext, httpContext); // current
_enrichDiagnosticContext?.Invoke(_diagnosticContext, httpContext, elapsedMs); // proposed

Describe alternatives you've considered n/a

Additional context n/a

jmenziessmith avatar Nov 28 '24 12:11 jmenziessmith

Thanks for dropping us a line!

How does the duration you want to pass through the IDiagnosticContext differ from the one computed by the middleware?

nblumhardt avatar Nov 29 '24 06:11 nblumhardt

Hi. Thanks for the quick reply.

I don't expect the duration to differ - I would expect the same value. The desire is to be able to access that value when enriching the DiagnoticContext.

jmenziessmith avatar Nov 29 '24 10:11 jmenziessmith

Ah sorry, I see I misunderstood 😅 - thanks for the clarification.

nblumhardt avatar Nov 30 '24 20:11 nblumhardt

@nblumhardt I have raised a PR with an alternative solution in order to avoid introducing breaking changes to the EnrichDiagnosticContext action.

https://github.com/serilog/serilog-aspnetcore/pull/391

jmenziessmith avatar Jan 06 '25 11:01 jmenziessmith