记录外部但不记录自己的程序集的第一次机会异常
捕获自己的第一次机会异常
AppDomain.CurrentDomain.FirstChanceException += FirstChanceException;
private static void FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
// I would like to log exceptions happening outside my assembly?
// But this event does not receive external FirstChanceExceptions
// (unlike Visual Studio debug output)
myLogger.Log(e.Exception);
}
示例
我想记录
WindowsBase.dll 中发生的“System.IO.IOException”类型的第一次机会异常
但不记录
MyApp 中发生“System.DivideByZeroException”类型的第一次偶然异常.exe
解决方案?
- 有没有办法获得有关 mscorlib.dll 等 dll 中出现的异常的通知?
- 请注意,我们感兴趣的是登录生产版本,而不仅仅是调试。
为什么
外部 dll 依赖项中发生的异常可能会导致问题,即使它们已在外部 dll 依赖项中得到处理。
例如,由于异常,外部方法可能会返回 null 值(而不是返回 x 的所需输出),即使我的代码主要处理此类异常输出了解到底出了什么问题以及在哪里出了问题很有用 - 因为虽然我可能能够避免致命异常,但通常 null 值会使我的应用程序的一部分无法运行。因此,记录外部第一次机会异常将提供有价值的信息,以帮助纠正问题/将 null 转换为 x。这可能仅存在于给定环境/特定用户等中......
Catching Own First Chance Exceptions
AppDomain.CurrentDomain.FirstChanceException += FirstChanceException;
private static void FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
// I would like to log exceptions happening outside my assembly?
// But this event does not receive external FirstChanceExceptions
// (unlike Visual Studio debug output)
myLogger.Log(e.Exception);
}
Example
I would like to log
A first chance exception of type 'System.IO.IOException' occurred in WindowsBase.dll
But not log
A first chance exception of type 'System.DivideByZeroException' occurred in MyApp.exe
Solution?
- Is there a way to get notified of those exceptions arising in dlls such as mscorlib.dll?
- Note the interest is to log in the production release, not only in debugging.
Why
Exceptions that occur in external dll dependencies can cause trouble, even if they are handled there.
For example, due to an exception a null value may be returned from an external method (instead of returning the desired output of x) and even though my code mostly handles such abnormal output it is useful to know what exactly went wrong and where - because while I may be able to avoid fatal exceptions then more often than not that null value makes part of my app non-functional. So logging the external first chance exception would provide valuable information to help rectify the issue / turn that null into x. And this may be present in only given environments / for specific users etc...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 StackTrace 类来检查源代码是什么例外情况:
You can use StackTrace class to check what is the source of the exception: