question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No RouteData when using custom exception handler

See original GitHub issue

We 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:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sandersaarescommented, Dec 26, 2019

Updated preview build of v3.4.0 is uploading now. Just call UseHttpMetrics after UseRouting 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.

0reactions
ivonincommented, Jan 15, 2020

Yes, everything works fine, thanks!

The problem with error codes as due to incorrect order of middleware registrations.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found