在C#中如何收集堆栈跟踪的程序崩溃 [英] In C# how to collect stack trace of program crash
本文介绍了在C#中如何收集堆栈跟踪的程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果应用程序随时崩溃,应该有最后的机会该应用程序收集堆栈跟踪并将其发送回我...
请给我指导。
我需要一个try catch覆盖我的应用程序的主要入口点?或者在C#应用程序中处理这些事情的最好方法是什么。
谢谢,
解决方案
要捕获所有未处理的异常,请将其添加到program.cs:
[STAThread]
static void Main()
{
AppDomain currentDomain = default(AppDomain);
currentDomain = AppDomain.CurrentDomain;
//处理未处理异常的处理程序。
currentDomain.UnhandledException + = GlobalUnhandledExceptionHandler;
//处理表单后面的线程中的异常。
System.Windows.Forms.Application.ThreadException + = GlobalThreadExceptionHandler;
...
}
private static void GlobalUnhandledExceptionHandler(object sender,UnhandledExceptionEventArgs e)
{
异常ex = default(Exception);
ex =(Exception)e.ExceptionObject;
ILog log = LogManager.GetLogger(typeof(Program));
log.Error(ex.Message +\\\
+ ex.StackTrace);
}
private static void GlobalThreadExceptionHandler(object sender,System.Threading.ThreadExceptionEventArgs e)
{
异常ex = default(异常);
ex = e.Exception;
ILog log = LogManager.GetLogger(typeof(Program)); // Log4NET
log.Error(ex.Message +\\\
+ ex.StackTrace);
}
堆栈跟踪可以通过 exception.StackTrace
I am new to C#. I am writing a small desktop form based application and I need to have this feature in the app.
If the application crashes at any time, there should be a last opportunity for the app to collect stack trace and send it back to me...
Please give me directions on this.
Do I need a try catch covering the main the entry point of my app ?? or what is the best way to handle such things in a C# app.
thank you,
解决方案
To catch all unhandled exceptions, Add this to program.cs:
[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);
}
Stack trace you can get by exception.StackTrace
这篇关于在C#中如何收集堆栈跟踪的程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文