我正在尝试处理 asp.net 5 控制台应用程序中未处理的异常,但我似乎无法使用相信的代码捕获这些错误。一旦抛出异常,代码就会中断。dnx 应用程序中有什么方法可以捕获未处理的异常吗?
public class Program
{
[STAThread]
public static void Main(string[] args)
{
#if !DNXCORE50
// Register unhandled exception handler
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);
// Throw exception
throw new Exception("1");
#endif
Console.ReadLine();
}
#if !DNXCORE50
/// <summary>
/// Catch all unhandled exceptions in all threads.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
{
try
{
// Get the exception that was thrown
Exception exceptionThrown = (Exception)args.ExceptionObject;
Console.WriteLine("Information coming from exception handler");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
#endif
}