C# winform 全局异常处理

废话不说,直接上代码:

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怎么找,大家百度,嘿嘿

你可能感兴趣的:(C# winform 全局异常处理)