ASP.NET 5: What is the recommended approach for catching and reporting all unhandled exceptions?

I am creating an api application. In old ASP.NET there was Application_Error () to catch all unhandled exceptions

protected void Application_Error() { var exception = Server.GetLastError(); _logger.FatalException("Fatal error.", exception); } 

What should be used in ASP.NET 5?

+5
source share
1 answer

The solution is to add a custom filter. Here's how to do it:

 public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => options.Filters.Add(new MyExceptionFilter())); } 

Now the custom filter should be obtained from IExceptionFilter:

 public class MyExceptionFilter : ActionFilterAttribute, IExceptionFilter { public void OnException(ExceptionContext context) { } } 

Unfortunately, it does not detect exceptions during Startup.Configuration ()

+5
source

Source: https://habr.com/ru/post/1239960/


All Articles