C# winform 全局異常處理

廢話不說,直接上代碼: c#

namespace HeatingBalance
{
    static class Program
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Program));
        /// <summary>
        /// 應用程序的主入口點。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                log.Info("start application.");

                //設置應用程序處理異常方式:ThreadException處理
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //處理UI線程異常
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_UIThreadException);
                //處理非UI線程異常
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                #region 應用程序的主入口點
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                // 啓動歡迎窗體
                Application.Run(new WelcomeForm());

                //InfoAction infoAction = new InfoAction();

                //// 啓動信息窗口
                //Application.Run(new HeatingPowerSettingForm(infoAction));

                //if (ActionType.Start == infoAction.InfoActionType)
                //{
                //    // 啓動主窗體
                //    Application.Run(new MainForm());
                //    return;
                //}

                //// 啓動設置窗體
                //Application.Run(new CommunicationSettingForm());
                FunctionMode functionMode = new FunctionMode(SoftMode.HeatingSource);

                Application.Run(new FunctiongSelectForm(functionMode));

                MainForm mainForm = new MainForm();
                // mainForm.SoftMode = functionMode.SoftMode;

                // 啓動主窗體
                Application.Run(mainForm);
                #endregion
            }
            catch (Exception ex)
            {
                RecordExceptionMsg(ex, string.Empty);
            }
        }

        static void Application_UIThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            RecordExceptionMsg(e.Exception, e.ToString());
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            RecordExceptionMsg(e.ExceptionObject as Exception, e.ToString());
        }

        /// <summary>
        /// 生成自定義異常消息
        /// </summary>
        /// <param name="ex">異常對象</param>
        /// <param name="backStr">備用異常消息:當ex爲null時有效</param>
        /// <returns>異常字符串文本</returns>
        static void RecordExceptionMsg(Exception ex, string backStr)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("");
            sb.AppendLine("****************************異常文本****************************");
            sb.AppendLine("【出現時間】:" + DateTime.Now.ToString());
            if (ex != null)
            {
                sb.AppendLine("【異常類型】:" + ex.GetType().Name);
                sb.AppendLine("【異常信息】:" + ex.Message);
                sb.AppendLine("【堆棧調用】:" + ex.StackTrace);
            }
            else
            {
                sb.AppendLine("【未處理異常】:" + backStr);
            }
            sb.AppendLine("***************************************************************");
            log.Error(sb.ToString());
        }
    }
}
log4net怎麼找,你們百度,嘿嘿
相關文章
相關標籤/搜索