如何在Windows Azure(工作器)角色中捕获未处理的异常

gum*_*umo 9 c# exception-handling azure

我正试图捕捉我的工作角色中所有未处理的异常.我尝试将try- catch块放入Run()方法(如此处所示),但没有成功.

public override void Run()
{
  try
  {
    base.Run();
  }
  catch (Exception ex)
  {
    Trace.TraceError("Unhandled Exception: {0}", ex);    
    throw ex;
  }
}
Run Code Online (Sandbox Code Playgroud)

该角色承载WCF服务,因此该Run()方法中没有其他逻辑.还有另一种可能性来捕获此级别的异常吗?

更新1 澄清问题:角色self托管WCF服务(已初始化OnStart()),其中某些操作是后台操作.当调用该服务并且该方法抛出意外异常时,我想抓住它将其写入日志.

解决方案: 显然它就像在普通的C#应用​​程序中一样:只需UnhandledException像这样添加一个处理程序

AppDomain.CurrentDomain.UnhandledException +=
  new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Run Code Online (Sandbox Code Playgroud)

OnStart()角色里面.我是如此专注于Azure,我认为这毕竟不起作用,我甚至没有尝试过:-)

gum*_*umo 14

正如我在问题中已经更新的那样,这里为了完整性而作为答案:

显然它就像在普通的c#应用程序中一样:只需UnhandledException像这样添加一个处理程序

AppDomain.CurrentDomain.UnhandledException +=
   new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Run Code Online (Sandbox Code Playgroud)

OnStart()角色里面.