记录外部但不记录自己的程序集的第一次机会异常

发布于 2024-12-27 04:13:56 字数 1225 浏览 0 评论 0原文

捕获自己的第一次机会异常

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦罢 2025-01-03 04:13:56

您可以使用 StackTrace 类来检查源代码是什么例外情况:

var stackTrace = new StackTrace(e.Exception);
var sourceFrame = stackTrace.GetFrame(0);
var throwingMethod = sourceFrame.GetMethod();
var sourceAssembly = throwingMethod.DeclaringType.Assembly;
var assemblyName = sourceAssembly.GetName().Name;

bool isMyApp = assemblyName == "MyApp";

You can use StackTrace class to check what is the source of the exception:

var stackTrace = new StackTrace(e.Exception);
var sourceFrame = stackTrace.GetFrame(0);
var throwingMethod = sourceFrame.GetMethod();
var sourceAssembly = throwingMethod.DeclaringType.Assembly;
var assemblyName = sourceAssembly.GetName().Name;

bool isMyApp = assemblyName == "MyApp";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文