【发布时间】:2019-05-14 12:31:12
【问题描述】:
我的情况如下:
- 我有一个表格
FormBase - 从此
FormBaseList和FormBaseDetail继承
现在在我的主窗体上,我想从 FirstChanceException 事件中记录一些异常。我记录了Exception.Message 和完整的stacktrace
但是在stacktrace我有问题。
例如,我创建了一个表单 FormCustomerDetail 及其派生自 FormBaseDetail。
并且在FormBaseDetail 中定义的受保护或私有方法中发生异常,则堆栈跟踪将显示FormBaseDetail 而不是FormCustomerDetail
这对我来说是个问题,因为现在我不知道异常发生的实际形式。从FormBaseDetail派生出来的表格大概有50种
所以问题是,FirstChanceException 事件中有没有办法检索实际表单的类名?还是名字?
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
private void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
StackTrace stackTraceObject = new StackTrace();
string stackTrace = stackTraceObject.ToString();
// I need to retrieve the actual form name or classname here...
LogError(
//ActualFormName + Environment.NewLine +
e.Exception.GetType().FullName + Environment.NewLine +
e.Exception.Message + Environment.NewLine +
stackTrace);
}
日志条目示例:
System.FormatException
Cannot convert xx to type boolean...
...
at Test_app.FormBaseDetail.OpenTables()
at Test_app.FormBaseDetail.PrepareFormLoad()
...
我想得到什么
Exeption occured in FormCustomerDetail
System.FormatException
Cannot convert xx to type boolean...
...
at Test_app.FormBaseDetail.OpenTables()
at Test_app.FormBaseDetail.PrepareFormLoad()
...
【问题讨论】:
-
您正在寻找一种方法来在发生异常时解析堆栈上的局部变量实例。不幸的是,
StackTrace类似乎不支持这一点。调试器可以,但这在这里可能没有帮助。 -- 顺便说一句,这是第一次机会,而不是第一次改变。
标签: c# .net winforms exception-handling stack-trace