我在这篇文章中读到,为了避免“程序停止工作”对话框的出现,我需要从AppDomain中捕获未处理的异常。
public Form1()
{
///Code
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
///more code
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var excep = e.ExceptionObject;
//Writing the exception to log file with the stack flow
Logger.Logger.LogException("UNHANDLED EXCEPTION:"+Environment.NewLine +excep.ToString(), this);
//Terminate the logger (manual event waiting for file write to finish)
Logger.Logger.Terminate();
Environment.Exit(1);
}
但是当我遇到异常时,虽然日志里面有显示错误信息,但应用程序却显示“程序停止工作”的对话框。
这可能是由于Logger.Terminate
命令导致的吗?
(再次强调-terminate命令会等待所有日志写入日志文件后才终止)
StackOverflowException
)可能会在任何处理程序有机会运行之前使整个进程崩溃。 - user743382