在.NET中的Unhandled Exception上编写minidump是否有意义?

时间:2014-11-26 14:44:23

标签: c# .net wpf

似乎我在这样的异常处理程序中:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

或者像这样:

Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;

堆栈已经解除调用我的自定义未处理异常处理程序。似乎在这一点上编写一个minidump是没有意义的,因为堆栈已经放松了。 在没有展开的情况下,堆栈应用程序无法理解此异常是否未处理。

即使我可以在UnhandledExceptionEventArgs.ExceptionObject中看到堆栈,我也无法在应用程序崩溃的确切位置获取minidump。

还有其他办法吗?

我知道我可以要求系统编写转储,但我应该是管理员。

更新:

确定。我有一个想法)如果在FirstChanceException处理程序中我可以走回堆栈并查看此异常是否未处理,那将会很好。但这应该足够快,可以在生产中使用。

2 个答案:

答案 0 :(得分:2)

您正在寻找FirstChanceException event,这是在堆栈展开之前引发的。

答案 1 :(得分:0)

我自己从未使用过垃圾箱。通常只知道发生异常的地方。但是你是对的,知道值等会更方便。在最新版本的错误记录器中,我甚至传递了3个可选参数,这些参数被转储到日志中,这会有所帮助。像(简化)的东西:

public void Log(string tag, string message = "", Exception exception = null, [CallerMemberName] string caller = "",
    object param = null, object param2 = null, object param3 = null, object param4 = null)
{
    DateTime time = DateTime.Now;
    var method = caller;
    if (param != null || param2 != null || param3 != null)
        method = string.Format("{0}({1}{2}{3}{4})", caller, param != null ? param : "", param2 != null ? ", " + param2 : "",
            param3 != null ? ", " + param3 : "", param4 != null ? ", " + param4 : "");
    try
    {
        ...
        if (exception != null)
            using (StreamWriter file = new StreamWriter(_errorFileName, true))
            {
                file.WriteLine(string.Format("[{0}] {1} {2}: {3}", time, tag, method, message));
                file.WriteLine(exception);
            }
    }
    catch { }
}

用法是

public static T Deserialize<T>(string file)
{
    try
    {
        ...
    }
    catch (Exception e)
    {
        Log("xml", exception: e, param: file, param2: typeof(T));
    }
    return default(T);

}

这不是你的问题的答案,而是如何在没有转储的情况下舒适地生活的解决方案。