美文网首页
WPF处理未捕获异常

WPF处理未捕获异常

作者: 奔跑伯爵 | 来源:发表于2020-08-16 21:25 被阅读0次

    如果程序中出现未捕获的异常,程序通常会直接崩溃,也不知道哪里出了问题。在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);
            }
        }
    

    相关文章

      网友评论

          本文标题:WPF处理未捕获异常

          本文链接:https://www.haomeiwen.com/subject/wsmbhftx.html