登录外部而不是自己组装的第一机会异常 [英] Log external but not own assembly first chance exceptions

查看:169
本文介绍了登录外部而不是自己组装的第一机会异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
    }

示例

  • 我想记录 A第一次机会异常类型发生在WindowsBase.dll'System.IO.IOException'的

    但是无法登录 A第一次机会异常的类型'System.DivideByZeroException发生在MyApp.exe的

    • 有没有办法在DLL中出现的那些例外,如mscorlib.dll中的得到通知?
    • 请注意,兴趣是登录的产能释放,不仅在调试。

    这是发生在外部DLL依赖异常会引起麻烦,即使被处理那里。

    Exceptions that occur in external dll dependencies can cause trouble, even if they are handled there.

    例如,由于一个异常的的值可以从外部方法返回(而不是返回的的 X 的所需的输出),即使我的code主要是处理这种不正常的输出知道究竟错在哪里,它是有用的 - 因为虽然我可能能够避免致命异常则往往不是说的的值,使得我的应用程序非一部分 - 功能。所以记录外第一次机会异常将提供有价值的信息,以帮助纠正问题/反过来说的的成的 X 的。这可能是present在只给环境/为特定用户等.​​..

    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...

    推荐答案

    您可以使用的堆栈跟踪类来检查什么是异常的来源:

    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";
    

    这篇关于登录外部而不是自己组装的第一机会异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆