-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RequestLoggingMiddleware
assumes HTTP status 500 for all exceptions which is not correct
#344
Comments
Some exploration around how to support this would be welcome; seems like a great improvement to make, if it can be done 👍 |
I don't think this can be achieved. As every middleware can change the result code (as DeveloperExceptionPageMiddlewareImpl does) we cannot know at this point which status code will be returned finally. You can also use a middleware that catches an Exception which is not relevant for the caller and still returns a 200/OK message. The 500 status code is just a good guess in this case. |
This is why I came here. I (of course) agree that no middleware can predict what other middlwares in the pipeline will do eventually, but with this specific detail from the "Additional context",
As a power user, I'm happy to
Edit to add: or, probably better, a clone of this block serilog-aspnetcore/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs Lines 66 to 71 in 68f2f3d
|
I'm running into the same issue, having a lot of TaskCanceledException log spam. Aditionally you have the annoying '... Cancelled by user.' SqlException for later. |
Just chiming in with another workable option; since ASP.NET Core completes the |
As a work around, if you catch exceptions in a middleware after
This may not work with error pages, I haven't tried it public class ExceptionHandlingMiddleware( RequestDelegate next, DiagnosticContext diagnosticContext )
{
public async Task Invoke( HttpContext context )
{
try
{
await next( context );
}
catch ( Exception ex )
{
var response = await BuildResponseAndSetStatusCode( context, ex );
diagnosticContext.SetException( ex );
// don't rethrow the exception
}
}
private async Task BuildResponseAndSetStatusCode(HttpContext context, Exception exception)
{
/* handle the error */
} And during startup: // this order ensures that `ExceptionHandlingMiddleware` gets the chance to manage the error and set a status code before serilog sees it
app.UseSerilogRequestLogging();
app.UseMiddleware<ExceptionHandlingMiddleware>(); |
Joining in, because I'm surprised to see that in .net 8 when the clients disconnects, the response code is set to 499, but serilog happily logs a 200 response code in the requestloggingmiddleware. |
Seems like this will get more complicated. ASP.NET Core 9 will allow user code to determine the returned status code, see https://github.com/dotnet/core/blob/main/release-notes/9.0/preview/preview7/aspnetcore.md#exceptionhandlermiddleware-option-to-choose-the-status-code-based-on-the-exception I don't know if there is any way how this could be supported in |
Mirroring that setting sounds reasonable @cremor 👍 - thanks for the heads-up. |
Although I think it should be possible to just inject |
Description
RequestLoggingMiddleware
logs all exceptions with HTTP status code 500 even if the actual status code sent to the client is a different one.Reproduction
app.UseSerilogRequestLogging();
throw new BadHttpRequestException("Test");
in an API controller action method.Expected behavior
RequestLoggingMiddleware
should use the actual HTTP status code in the log message.Relevant package, tooling and runtime versions
Serilog.AspNetCore 7.0.0 on .NET 7
Additional context
I think in this special case the HTTP status code is set by the DeveloperExceptionPageMiddlewareImpl. But there might be other cases/middlewares that affect the status code.
ASP.NET Core 8 will improve the default exception handling middlewares for cancellation, see dotnet/aspnetcore#46330
As you can see in that PR, cancelled requests will then use the status code
Status499ClientClosedRequest
.ASP.NET Core 9 will allow user code to determine the returned status code, see https://github.com/dotnet/core/blob/main/release-notes/9.0/preview/preview7/aspnetcore.md#exceptionhandlermiddleware-option-to-choose-the-status-code-based-on-the-exception
The text was updated successfully, but these errors were encountered: