如何捕捉在Windows Azure中(工人)角色未处理的异常 [英] How to catch an unhandled exception in Windows Azure (Worker) Role
问题描述
我试图抓住我的辅助角色所有未处理的异常。我试图把一个尝试
- 抓
块到运行()
办法(所建议的<一个href=\"http://social.msdn.microsoft.com/Forums/en/windowsazuretroubleshooting/thread/11019f45-0ab6-403b-8908-fa40766af875\">here)但没有成功。
I'm trying to catch all unhandled exceptions in my worker role. I tried putting a try
-catch
block into the Run()
method (as suggested here) but with no success.
public override void Run()
{
try
{
base.Run();
}
catch (Exception ex)
{
Trace.TraceError("Unhandled Exception: {0}", ex);
throw ex;
}
}
该角色托管WCF服务,于是就有了运行()
方法内没有其他逻辑。是否有另一种可能性在这个级别捕获异常?
The role hosts a WCF service, so there is no other logic inside the Run()
method. Is there another possibility to catch exceptions at this level?
更新1
为了澄清这个问题:角色的自我承载WCF服务(在的OnStart初始化()
)其中的一些操作是后台操作。当服务调用和方法抛出一个意外的异常,我想赶上那将它写入日志。
Update 1
To clarify the problem: The role self hosts a WCF service (initialized in OnStart()
) where some operations are background operations. When the service is called and that method throws an unexpected exception, I like to catch that to write it to the log.
解决方案:
显然,这就像在一个正常的C#应用程序:
只是一个处理程序添加到这样的 UnhandledException
事件
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
在的OnStart()
中的作用。我太专注在Azure上,我认为这可能毕竟不是工作,我甚至没有尝试: - )
inside the OnStart()
of the Role. I was so focused on Azure that I supposed this couldn't work after all, that I didn't even try it :-)
推荐答案
前面已经更新了我的问题,是为了完整性的缘故,因为答案是:
As already updated in my question, here for completeness's sake as answer:
显然,这就像在一个正常的C#应用程序:只需添加一个处理程序,像这样的 UnhandledException
事件
Obviously it is like in a normal c# application: Just add a handler to the UnhandledException
event like this
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
在的OnStart()
中的作用。
这篇关于如何捕捉在Windows Azure中(工人)角色未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!