如何在Windows Azure(Worker)角色中捕获未处理的异常 [英] How to catch an unhandled exception in Windows Azure (Worker) Role
问题描述
我正在尝试捕获我的worker角色中的所有未处理的异常。我尝试将尝试
- catch
block into Run()
方法(如建议的 here )但没有成功。
public override void Run()
{
try
{
base.Run();
}
catch(Exception ex)
{
Trace.TraceError(未处理的异常:{0},ex);
throw ex;
}
}
角色承载一个WCF服务,所以没有 Run()
方法中的其他逻辑。是否有另一种可能性在此级别捕获异常?
更新1
澄清问题:角色自己承载一个WCF service(初始化为 OnStart()
)其中一些操作是后台操作。当服务被调用并且该方法引发意外的异常时,我喜欢将其写入日志。
解决方案:
显然这就像在正常的C#应用程序中:
只需在 UnhandledException
事件中添加一个处理程序,就像这样
AppDomain.CurrentDomain.UnhandledException + =
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);角色中的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 。我非常专注于Azure,所以我以为这根本就不行,甚至没有尝试过: - )解决方案在我的问题中已经更新了,为了完整性,我们回答:
显然这就像一个正常的c#应用程序:只需添加一个处理程序到 UnhandledException
这样的事件
AppDomain.CurrentDomain.UnhandledException + =
新的UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);角色中的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 。
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;
}
}
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?
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.
Solution:
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);
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:
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);
inside the OnStart()
of the Role.
这篇关于如何在Windows Azure(Worker)角色中捕获未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!