static class PRogram{ /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { try { //處理未捕獲的異常 application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //處理UI線程異常 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //處理非UI線程異常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
#region 應用程序的主入口點
Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Main());
#endregion
} catch (Exception ex) { string str = ""; string strDateInfo = "出現應用程序未處理的異常:" + DateTime.Now.ToString() + "/r/n";
if (ex != null) { str = string.Format(strDateInfo + "異常類型:{0}/r/n異常消息:{1}/r/n異常信息:{2}/r/n", ex.GetType().Name, ex.Message, ex.StackTrace); } else { str = string.Format("應用程序線程錯誤:{0}", ex); }
//MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); LogManager.WriteLog(str); }
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { string str = ""; string strDateInfo = "出現應用程序未處理的異常:" + DateTime.Now.ToString() + "/r/n"; Exception error = e.Exception as Exception; if (error != null) { str = string.Format(strDateInfo + "異常類型:{0}/r/n異常消息:{1}/r/n異常信息:{2}/r/n", error.GetType().Name, error.Message, error.StackTrace); } else { str = string.Format("應用程序線程錯誤:{0}", e); }
//MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); LogManager.WriteLog(str); }
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { string str = ""; Exception error = e.ExceptionObject as Exception; string strDateInfo = "出現應用程序未處理的異常:" + DateTime.Now.ToString() + "/r/n"; if (error != null) { str = string.Format(strDateInfo + "Application UnhandledException:{0};/n/r堆棧信息:{1}", error.Message, error.StackTrace); } else { str = string.Format("Application UnhandledError:{0}", e); }
//MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); LogManager.WriteLog(str); }}
新聞熱點
疑難解答