在 .NET 中对未处理的异常编写小型转储是否有意义
本文关键字:小型 转储 是否 有意义 异常 NET 未处理 | 更新日期: 2023-09-27 18:32:10
当我在这样的异常处理程序中时,似乎
:AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
或者像这样:
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
堆栈已经展开,可以调用我的自定义未处理异常处理程序。此时编写小型转储似乎没有意义,因为堆栈已经展开。如果不展开堆栈,应用程序就无法理解此异常是否未处理。
即使我可以在UnhandledExceptionEventArgs.ExceptionObject中看到堆栈,我也无法在应用程序崩溃的确切位置获得小型转储。
还有别的办法吗?
我知道我可以要求系统编写转储,但我应该为此担任管理员。
更新:
还行。我有一个想法)如果在FirstChanceException处理程序中我可以返回堆栈并查看此异常是否未处理,那就太好了。但这应该足够快,可以在生产中工作。
您正在寻找FirstChanceException
事件,该事件在堆栈展开之前引发。
我自己从未使用过转储。通常只知道异常发生的位置就足够了。但是你是对的,知道值等会更方便。在最新版本的错误记录器中,我什至传递了多达 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);
}
这不是您问题的答案,而是如何在不倾倒的情况下舒适生活的解决方案。