serilog / serilog-aspnetcore

Serilog integration for ASP.NET Core
Apache License 2.0
1.29k stars 203 forks source link

Make exception logging in `RequestLoggingMiddleware` optional #341

Open cremor opened 1 year ago

cremor commented 1 year ago

Is your feature request related to a problem? Please describe. If my application uses UseSerilogRequestLogging all unhandled exceptions are logged twice. This causes logs to be more long/complicated than they need to be.

Describe the solution you'd like RequestLoggingOptions should have an option to not log the exception.

Describe alternatives you've considered Maybe exception logging should even be disabled by default since I couldn't find any case where the exception isn't logged by something else.

Additional context There are multiple other classes which already log the exception, depending on your middleware configuration:

sungam3r commented 1 year ago

It looks like you can disable particular log sources by Serilog's level overrides.

cremor commented 1 year ago

I don't want to disable the log. I still want to see the log line HTTP GET /api/TodoItems responded 500 in 8.9821 ms. I just don't want to see the exception details with that log line.

nblumhardt commented 1 year ago

I think this is doable, but we'd have to be careful about how we handle any exceptions captured by IDiagnosticContext (collector, in the middleware code), since those won't propagate and would still need to be logged by the middleware regardless of the optional "include exceptions" setting.

This pushes the setting name/semantics towards IncludeEscapingExceptions or something of that kind - meaning, turning the option off would not necessarily suppress all exception information in the completion events.

🤔

cremor commented 11 months ago

Where/how is this IDiagnosticContext used? Or rather, who calls its SetException method?

pcmcoomans commented 3 months ago

Have there any updates regarding this issue?

nblumhardt commented 2 weeks ago

@cremor users can call IDiagnosticContext.SetException() from anywhere in an executing web request to set the Exception included in the final log event. I think we'd still need to record these, because they'll never reach any other exception handler.

Given the possible additional complications around logging cancellation exceptions, how about an enum for the option, which gives us some flexibility for future extensions?

The final values for the IncludeEscapingExceptions setting then might be along the lines of:

Initially we could consider implementing only the first and last, or some selection of those.

cremor commented 2 weeks ago

I think this is a good idea. Some details: