Description
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
- Use
app.UseSerilogRequestLogging();
- Have
throw new BadHttpRequestException("Test");
in an API controller action method. - Call the method.
- See the log output "HTTP GET /api/test responded 500 in 55.9105 ms"
- See that the actual status code received by the client is 400.
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