WPF监测试全局异常

发布时间 2023-04-10 16:35:33作者: 湘灵
 1         public App()
 2         {
 3             ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;
 4 
 5             App.Current.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
 6             AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
 7             Dispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(Dispatcher_UnhandledException);
 8         }
 9 
10         /// <summary>
11         /// Handles the UnhandledException event of the Dispatcher.
12         /// </summary>
13         /// <param name="sender">The source of the event.</param>
14         /// <param name="e">The <see cref="System.Windows.Threading.DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param>
15         void Dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
16         {
17             string str = GetExceptionMsg(e.Exception as Exception, e.ToString());
18             Util.Log.Logger.Error("Dispatcher_UnhandledException: " + str);
19             //MessageBox.Show(str, "系统错误", MessageBoxButton.OK, MessageBoxImage.Error);
20             Helper.Notice.Error("未知系统异常," + e.Exception.Message);
21 
22             App.Current.Shutdown();
23             Environment.Exit(-1);
24         }
25 
26         /// <summary>
27         /// Handles the UnhandledException event of the current application domain.
28         /// </summary>
29         /// <param name="sender">The source of the event.</param>
30         /// <param name="e">The <see cref="System.UnhandledExceptionEventArgs"/> instance containing the event data.</param>
31         void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
32         {
33             string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
34             Util.Log.Logger.Error("CurrentDomain_UnhandledException: " + str);
35             //MessageBox.Show(str, "系统错误", MessageBoxButton.OK, MessageBoxImage.Error);
36             Helper.Notice.Error("未知系统异常," + (e.ExceptionObject as Exception).Message);
37 
38             App.Current.Shutdown();
39             Environment.Exit(-1);
40         }
41 
42         /// <summary>
43         /// Handles the DispatcherUnhandledException event of the current Application.
44         /// </summary>
45         /// <param name="sender">The source of the event.</param>
46         /// <param name="e">The <see cref="System.Windows.Threading.DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param>
47         void Current_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs ex)
48         {
49             var str = GetExceptionMsg(ex.Exception, ex.ToString());
50             Util.Log.Logger.Error("Current_DispatcherUnhandledException: " + str);
51             //MessageBox.Show(str, "系统错误", MessageBoxButton.OK, MessageBoxImage.Error);
52             Helper.Notice.Error("未知系统异常," + ex.Exception.Message);
53             System.Threading.Thread.Sleep(2000);
54             App.Current.Shutdown();
55             Environment.Exit(-1);
56         }
57 
58         private static string GetExceptionMsg(Exception ex, string backStr)
59         {
60             var sb = new StringBuilder();
61             sb.AppendLine("****************************异常文本****************************");
62             sb.AppendLine("【出现时间】:" + DateTime.Now);
63             if (ex != null)
64             {
65                 sb.AppendLine("【异常类型】:" + ex.GetType().Name);
66                 sb.AppendLine("【异常信息】:" + ex.Message);
67                 if (ex is ReflectionTypeLoadException)
68                 {
69                     var le = ex as ReflectionTypeLoadException;
70                     foreach (var exception in le.LoaderExceptions)
71                     {
72                         sb.AppendLine("【加载异常】:" + exception.Message);
73                     }
74                 }
75                 else
76                 {
77                     sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
78                     sb.AppendLine("【其他错误】:" + ex.Source);
79                 }
80                 var innexcpetion = ex.InnerException;
81                 var innIndex = 0;
82                 while (innexcpetion != null)
83                 {
84                     sb.AppendLine($"【内部异常{innIndex++}】:" + innexcpetion.Source);
85                     innexcpetion = innexcpetion.InnerException;
86                 }
87             }
88             else
89             {
90                 sb.AppendLine("【未处理异常】:" + backStr);
91             }
92             sb.AppendLine("***************************************************************");
93 
94             return sb.ToString();
95         }

在App里面定义,然后捕捉异常,记录日志。