5

I'm building an api app. In the 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?

2

1 Answer 1

5

The solution is to add a custom filter. This is how it could be done:

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

Now the custom filter should derive from IExceptionFilter:

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

    }
}

Unfortunately, it doesn't catch exceptions during Startup.Configuration()

Sign up to request clarification or add additional context in comments.

1 Comment

Have you been able to find a better way of doing this like "Application_Error" on asp. net 4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.