【发布时间】:2021-08-20 03:38:29
【问题描述】:
我正在尝试为无状态服务中的任何未处理异常添加默认异常处理程序,但它似乎没有捕获其中任何一个。这是我正在使用的代码:
protected override async Task RunAsync(CancellationToken cancellationToken)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(this.HandleUnhandledExceptions);
throw new Exception("This is an unhandled exception");
}
private void HandleUnhandledExceptions(object sender, UnhandledExceptionEventArgs args)
{
Exception exception = (Exception)args.ExceptionObject;
this.Logger.LogError("The application encountered unhandled exception: {exception}", exception.ToString());
}
我之前使用 AppDomain.CurrentDomain.UnhandledException 添加异常处理程序,但在这种情况下,它似乎从未进入处理程序方法。我怀疑这可能是线程或进程相关的问题。你知道为什么它不起作用吗?还有其他方法可以设置异常处理程序吗?
【问题讨论】:
标签: .net azure-service-fabric appdomain unhandled-exception service-fabric-stateless