[英]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.