简体   繁体   English

Starts..c中的Global.asax Application_Error等价物

[英]Global.asax Application_Error equivalent in Startup.cs

I am trying to catch errors from my .NET application before they are written to the event log. 我试图在将.NET应用程序写入事件日志之前捕获错误。 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. 我已经在接受这个问题的答案中看到,这样做的方法是在Global.asax中包含一个Application_Error (或它说的Application_OnError )函数。

However, we have switched to using Startup.cs only now, and can't work out how to make the equivalent call. 但是,我们现在只切换到使用Startup.cs,并且无法确定如何进行等效调用。 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. 通过说你已经“切换到使用Startup”我假设你的意思是使用OWIN管道。

Application_Error only gets called when an exception is thrown during a request, and is unhandled. Application_Error仅在请求期间抛出异常时被调用,并且未被处理。

So the equivalent in an OWIN pipeline is to catch any unhandled exceptions at the start of your OWIN pipeline. 因此,OWIN管道中的等效项是在OWIN管道的开头捕获任何未处理的异常。

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. 换句话说,您需要创建一个自定义中间件,并在调用_next时使用try / catch ,然后您可以使用catch子句中的异常执行您想要的操作。

This other StackOverlow answer shows how it is done. 另一个StackOverlow答案显示了它是如何完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM