0

我正在尝试处理 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

}
4

1 回答 1

0

您可以将异常输出到命令提示符。

“dnx 命令引用了几个影响其行为的环境变量,例如 DNX_TRACE。

将 DNX_TRACE 环境变量设置为 1,然后再次运行应用程序。你应该会看到更多的输出。”

你可以在这里阅读更多关于它的信息

于 2016-03-14T09:29:49.130 回答