WPF处理未捕获异常

如果程序中出现未捕获的异常,程序通常会直接崩溃,也不知道哪里出了问题。在App.xaml.cs文件中加入以下代码,记录未捕获的异常,便于查找错误。

    public partial class App : Application
    {
        public App()
        {            
            this.Startup += (sender, e) =>
            {
                this.DispatcherUnhandledException += App_DispatcherUnhandledException;
                TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            };
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                if (e.ExceptionObject is Exception)
                    LogException("CurrentDomain_UnhandledException", e.ExceptionObject as Exception);
                else
                    MessageBox.Show(e.ExceptionObject.ToString(), "CurrentDomain_UnhandledException");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionToString(ex), "CurrentDomain_UnhandledException");
            }
        }

        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            try
            {
                LogException("TaskScheduler_UnobservedTaskException", e.Exception);
                e.SetObserved();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionToString(ex), "TaskScheduler_UnobservedTaskException");
            }
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                LogException("App_DispatcherUnhandledException", e.Exception);
                if (!(e.Exception is OutOfMemoryException))
                    e.Handled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionToString(ex), "App_DispatcherUnhandledException");
            }
        }

        void ExToStr(StringBuilder sb, Exception ex)
        {
            sb.AppendLine(ex.GetType().ToString());
            sb.AppendLine(ex.Message);
            sb.AppendLine(ex.StackTrace);
            if (ex.InnerException != null)
            {
                sb.AppendLine("InnerException");
                ExToStr(sb, ex.InnerException);
            }
        }

        string ExceptionToString(Exception ex)
        {
            var sb = new StringBuilder();
            sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            ExToStr(sb, ex);
            return sb.ToString();
        }

        void LogException(string caption, Exception ex)
        {
            string msg = ExceptionToString(ex);
            using (var sw = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "error.log"), true))
            {
                sw.WriteLine(caption);
                sw.WriteLine(msg);
            }
            MessageBox.Show(msg, caption);
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。