WPF未处理的异常处理程序并保留调用堆栈

Rad*_*ado 5 .net c# wpf exception

我有一个WPF应用程序,每当我的应用程序遇到未处理的异常以帮助调试时,我需要编写一个小型转储文件.但是,每次调用异常处理程序时,堆栈都完全展开到处理程序,并且没有可用于转储文件的有用状态.

我试图订阅这两个并且堆栈已经解开了两个:

Application.Current.DispatcherUnhandledException
AppDomain.CurrentDomain.UnhandledException
Run Code Online (Sandbox Code Playgroud)

我尝试使用控制台应用程序,并且堆栈没有解开,因此它肯定与WPF相关.异常和处理程序都发生在主线程中.

这里有两个代码示例,您可以轻松地看到这一点.只需在每个处理程序中设置断点,并在遇到断点时观察调用堆栈.

控制台应用:

class Program
{
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        int foo = 5;
        ++foo;
        throw new ApplicationException("blah");
        ++foo;
    }

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Console.WriteLine("blah");
    }
}
Run Code Online (Sandbox Code Playgroud)

WPF应用:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        int foo = 5;
        ++foo;
        throw new ApplicationException("blah");
        ++foo;
        base.OnStartup(e);
    }

    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {}

    void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        e.Handled = false;
    }
}
Run Code Online (Sandbox Code Playgroud)