I am trying to catch errors from my .NET application before they are written to the event log. I have seen in the accepted answer to this question that the way to do it would be to include an Application_Error
(or Application_OnError
it says) function in the Global.asax.
However, we have switched to using Startup.cs only now, and can't work out how to make the equivalent call. The accepted answer to this question suggests that it may not be possible. Does anyone know for sure if there is an equivalent way to handle these errors?
By saying you've "switched to using Startup" I'm assuming you mean using an OWIN pipeline.
Application_Error
only gets called when an exception is thrown during a request, and is unhandled.
So the equivalent in an OWIN pipeline is to catch any unhandled exceptions at the start of your OWIN pipeline.
In other words you need to create a custom middle-ware with a try
/catch
around the call to _next
and then you can do what you want with the exception in the catch clause.
This other StackOverlow answer shows how it is done.