C# WPF未处理的异常处理程序和保留调用堆栈

C# WPF未处理的异常处理程序和保留调用堆栈,c#,.net,wpf,exception,C#,.net,Wpf,Exception,我有一个WPF应用程序,每当我的应用程序遇到未处理的异常时,我需要编写一个小型转储文件,以帮助调试。但是,每次调用异常处理程序时,堆栈都会完全展开到处理程序,并且转储文件中没有可用的状态 我尝试订阅这两个版本,但这两个版本的堆栈都已展开: Application.Current.DispatcherUnhandledException AppDomain.CurrentDomain.UnhandledException 我在一个控制台应用程序中尝试了同样的方法,但堆栈没有展开,因此它肯定与WP

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

我尝试订阅这两个版本,但这两个版本的堆栈都已展开:

Application.Current.DispatcherUnhandledException
AppDomain.CurrentDomain.UnhandledException
我在一个控制台应用程序中尝试了同样的方法,但堆栈没有展开,因此它肯定与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");
    }
}
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;
    }
}

刚刚在WPF(.NET 4.5)中尝试了CurrentDomain_UnhandledException,它工作正常。虽然我在Window中设置了它,但只需点击一下按钮就导致了崩溃。我知道它在.NET4.0中也可以正常工作。您正在使用哪个.NET版本?你能试试我的方案吗,而不是App.OnStartup(也许WPF还没有完全初始化)?否则发布这两个异常(控制台和WPF)的完整堆栈,我想看看它们有什么不同。。。