No RouteData when using custom exception handler
See original GitHub issueWe have a problem with prometheus-net reporting empty controller
and action
labels when the exception occurs:
http_request_duration_seconds_count{code="500",method="GET",controller="",action=""}
...
We do have a custom exception handler set up:
app.UseHttpMetrics();
app.UseMetricServer();
// ....
app.UseExceptionHandler(errorApp =>
{
errorApp.UseMiddleware<ExceptionHandlerMiddleware>();
});
The root of the problem seems to be in these lines in the ExceptionHandlerMiddleware class:
// An endpoint may have already been set. Since we're going to re-invoke the middleware pipeline we need to reset
// the endpoint and route values to ensure things are re-calculated.
context.SetEndpoint(endpoint: null);
var routeValuesFeature = context.Features.Get<IRouteValuesFeature>();
routeValuesFeature?.RouteValues?.Clear();
I presume that this change was made based on this issue and made it to ASP.NET Core 3.0 that we use.
Is there a workaround? Can it probably be fixed via different order of middleware registrations?
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
c# - Passing the current url with routedata to custom error ...
I think you're over complicating it a bit. Just create a shared function that logs the exception and page in your error controller...
Read more >Handle errors in ASP.NET Core
An alternative to a custom exception handler page is to provide a lambda to UseExceptionHandler. Using a lambda allows access to the error ......
Read more >Exception or Error Handling in Asp.Net MVC Using Custom ...
For simplicity, I have not included the logging code. You can use log4net or ELMAH or any other logging framework of your choice...
Read more >Custom Exception Filter In ASP.NET MVC
Introduction · The error won't get logged anywhere. · Exceptions raised outside controllers will not be handled. Example- exception raised because ...
Read more >Custom Exception Filter in ASP.NET MVC
txt) file. First, it checks whether the exception has been handled or not by using the ExceptionHandled property. If the exception is not...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Updated preview build of v3.4.0 is uploading now. Just call
UseHttpMetrics
afterUseRouting
and it will persist the route data. Any later changes to route data (e.g. in exception handlers) will not affect metrics.Edit: Now published as stable. Eager to hear your feedback!
Please let me know if this appears to solve your problem.
Yes, everything works fine, thanks!
The problem with error codes as due to incorrect order of middleware registrations.