I want to show the developer exception page when there is an unhandled exception but I also want to add logging to it. But when I use app.UseDeveloperExceptionPage
together with app.UseExceptionHandler
, only the last one I add works. How can I make the two work together?
The default ExceptionHandler does not work with the DeveloperExceptionPage because it catches the Exception.
What you can do is add an ExceptionFilter, log the Exception and let it be caught by the DeveloperExceptionPage:
public class ExceptionFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
Log.Error(context.Exception, $"{context.ActionDescriptor.DisplayName}");
// the important bit here
context.ExceptionHandled = false;
}
}
And then add it to the filters:
services.AddMvc(setup => { /* ... */ setup.Filters.Add<ExceptionFilter>(); });
You don't need to move your logic to an exception filter.
You can simply re-throw the exception received from the IExceptionHandlerFeature
you used (IExceptionHandlerFeature
/IExceptionHandlerPathFeature
)
For example:
var exceptionFeature = context.Features.Get<IExceptionHandlerFeature>();
throw exceptionFeature.Error;
Then your DeveloperExceptionPage will catch the exception down the pipeline.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With