【问题标题】:ASP.NET Core 1.0 Global exception handler in console application (DNX)ASP.NET Core 1.0 控制台应用程序 (DNX) 中的全局异常处理程序
【发布时间】:2016-01-14 01:38:11
【问题描述】:

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

}

【问题讨论】:

  • 它没有被调用,因为您的异常是在控制台应用程序中引发的,而不是您的应用程序域的级别?换句话说,您在控制台应用程序中的异常被抛出的级别高于您侦听 UnhandledException 事件的级别。这有意义吗?
  • 我想我明白你的意思,那么我需要做些什么来获取这些异常?
  • @DotnetShadow 请考虑下面的答案。

标签: asp.net asp.net-core dnx asp.net-core-1.0


【解决方案1】:

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

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

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

您可以阅读更多关于它的信息here

【讨论】:

  • 感谢您提供额外信息,但我正在尝试捕获错误以便记录它。尽管您的信息显示了如何获取额外信息。 .NET 团队正在解决这个问题,以提供一种捕获异常的方法。 github.com/dotnet/coreclr/issues/2999
猜你喜欢
  • 1970-01-01
  • 2011-03-09
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 2012-06-05
相关资源
最近更新 更多