简体   繁体   English

在AppDomain.UnhandledException处理程序中获取例外对象

[英]Get excepting object in AppDomain.UnhandledException handler

I'm using an AppDomain.UnhandledException event handler to perform some logging when my application crashes due to an unhandled exception. 当我的应用程序由于未处理的异常而崩溃时,我正在使用AppDomain.UnhandledException事件处理程序执行一些日志记录。

void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {...}

I would like to be able to log the typeof(object) and various object fields/properties when the source of the unhandled exception is one of my custom object classes, but it seems that this information is not included either in the UnhandledExceptionEventArgs or as the sender object. 当未处理的异常的源是我的自定义对象类之一时,我希望能够记录typeof(object)和各种object字段/属性,但是似乎此信息未包含在UnhandledExceptionEventArgs或作为sender对象。 I tried casting sender but understandably the sender is simply the AppDomain which has raised the exception. 我尝试过强制转换sender但可以理解, sender只是引发异常的AppDomain

Is there a way to retrieve this information from within this event handler? 有没有办法从这个事件处理程序的信息? The only other thing I can think of is to use a static field to store a reference to an object each time that an object throws, but I think that requires that I have a static field for each type I want to do this for. 我唯一想到的另一件事是每次对象抛出时都使用静态字段存储对对象的引用,但是我认为这要求我要为此的每种类型都有一个静态字段。

Note: I am aware that the stack trace may lead me to the source of the exception, however this does not allow me to log important state information regarding that object during the crash. 注意:我知道stack trace可能会将我引导到异常的来源,但是,这不允许我在崩溃期间记录有关该对象的重要状态信息。

For example, if my stack trace from e.ExceptionObject is 例如,如果我来自e.ExceptionObject堆栈跟踪为

Stack Trace: 
   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
   at CustomLibrary.CustomObject.readCallback(IAsyncResult ar)
   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
   ...

I would like to be able to get a reference to the NetworkStream object, which will allow me to locate the associated CustomObject in a collection. 我希望能够获得对NetworkStream对象的引用,这将使我能够在集合中找到关联的CustomObject

Try Using below code and see that exception is getting caught or not.. Also just check whether anywhere in your code your are rethrowing the exception like throw ex . 尝试使用下面的代码,看看是否捕获到异常。也只需检查代码中是否有任何地方正在抛出异常(例如throw ex If yes than it will eat up the stacktrace that you are looking for, try rethrowing using just throw . 如果可以,那么它将耗尽您要查找的堆栈跟踪,请尝试使用just throw重新throw

  [STAThread]
    static void Main()
    {
    AppDomain currentDomain = default(AppDomain);
    currentDomain = AppDomain.CurrentDomain;
    // Handler for unhandled exceptions.
    currentDomain.UnhandledException += GlobalUnhandledExceptionHandler;
    // Handler for exceptions in threads behind forms.
    System.Windows.Forms.Application.ThreadException += GlobalThreadExceptionHandler;
    ...
    }

private static void GlobalUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
   Exception ex = default(Exception);
   ex = (Exception)e.ExceptionObject;
   ILog log = LogManager.GetLogger(typeof(Program));
   log.Error(ex.Message + "\n" + ex.StackTrace);
}


private static void GlobalThreadExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs e)
{
   Exception ex = default(Exception);
   ex = e.Exception;
   ILog log = LogManager.GetLogger(typeof(Program)); //Log4NET
   log.Error(ex.Message + "\n" + ex.StackTrace);
}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AppDomain.UnhandledException处理程序不会在单元测试中触发 - AppDomain.UnhandledException handler doesn't fire in unit test The AppDomain.UnhandledException未捕获BackgroundWorker _异常 - BackgroundWorker _ Exception not caught by The AppDomain.UnhandledException Application.ThreadException 与 AppDomain.UnhandledException - Application.ThreadException vs AppDomain.UnhandledException AppDomain.UnhandledException的UnhandledExceptionMode.ThrowException - UnhandledExceptionMode.ThrowException for AppDomain.UnhandledException 在多线程环境中使用AppDomain.UnhandledException - Using AppDomain.UnhandledException in a multithreaded environment 如何处理C#AppDomain.UnhandledException - How to handle C# AppDomain.UnhandledException 在模块化表单应用程序中使用AppDomain.UnhandledException - Use AppDomain.UnhandledException in modular forms application AppDomain.UnhandledException与RegisterApplicationRecoveryCallback - AppDomain.UnhandledException vs. RegisterApplicationRecoveryCallback AppDomain.UnhandledException应该在哪里订阅? - Where should AppDomain.UnhandledException be subscribed to? 如何处理AppDomain.UnhandledException,得到“ [[MyApp]]遇到问题,需要关闭”消息? - How can I get “[MyApp] encountered a problem and needs to close” message with AppDomain.UnhandledException handled?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM