问题描述
我正在将控制台应用程序移植到 .NET Core
,并且我试图替换以下行:
I'm porting an console application to .NET Core
, and I'm trying to replace this line:
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
阅读,似乎没有内置的方法可以实现。
After reading this, it seems there is no built-in way to do this.
所以我的问题:用 try / catch
替换整个代码这一行的唯一方法是吗?
So my question: is the only way to replace this line surrounding my entire code with a try/catch
?
通过阅读,似乎还有另一种方法,即继续使用 System.AppDomain
,但我似乎找不到此类/方法。我唯一的猜测是,但它明确指出,如果可能的话,不应使用它,因此我不想这么做。
By reading this, it seems like there is another way, namely by keep on using System.AppDomain
, but I can't seem to find this class/method. My only guess was this library, but it clearly states that it should not be used if possible, so I would like not to.
推荐答案
您说得对, AppDomain.UnhandledException
或类似的,因此现在您应该等待或添加其他 try / catch
块。但是,如果您正在使用任务,则可以使用,其中获得。
You're right, the AppDomain.UnhandledException
or it's analog will be available only in .Net Core 2.0, so for now you should either wait or add additional try/catch
blocks. However, if you're using the tasks, you can use TaskScheduler.UnobservedTaskException
, which is available from first version of .Net Core
.
这篇关于控制台应用程序中的.NET Core全局异常处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!