使用异步和 UWP 进行异常调试

Exceptions debugging with async and UWP

我正在开发 UWP 应用程序。我经常使用 async/await。总是当我的代码中发生异常时,调试器设置中断 App.g.i.cs

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
            UnhandledException += (sender, e) =>
            {
                if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
            };
#endif

但是我想看到异常发生时的那一行。如何实现这种行为?

将以下方法添加到您的 App class:

private static void UnhandledError(object sender, UnhandledErrorDetectedEventArgs eventArgs)
{
    try
    {
        // A breakpoint here is generally uninformative
        eventArgs.UnhandledError.Propagate();
    }
    catch (Exception e)
    {
        // Set a breakpoint here:
        Debug.WriteLine("Error: {0}", e);
        throw;
    }
}

在您的 App 构造函数中:

public UnitTestApp()
{
    CoreApplication.UnhandledErrorDetected += UnhandledError;

    // ...and any other initialization.
    // The InitializeComponent call just sets up error handlers,
    // and you can probably do without in the case of the App class.

    // This can be useful for debugging XAML:
    DebugSettings.IsBindingTracingEnabled = true;
    DebugSettings.BindingFailed +=
        (sender, args) => Debug.WriteLine(args.Message);

}

仍然存在无法获得良好堆栈跟踪的情况,但这通常很有帮助。

另一种方法是在抛出异常时中断(通过DebugWindows异常设置).