11using GeekDesk . Constant ;
2+ using GeekDesk . Util ;
23using Microsoft . Win32 ;
34using System ;
45using System . IO ;
6+ using System . Text ;
57using System . Windows ;
68using System . Windows . Media . Animation ;
79using System . Windows . Threading ;
@@ -19,6 +21,8 @@ public partial class App : Application
1921 public App ( )
2022 {
2123 this . Startup += new StartupEventHandler ( App_Startup ) ;
24+ Application . Current . DispatcherUnhandledException += Current_DispatcherUnhandledException ;
25+ AppDomain . CurrentDomain . UnhandledException += CurrentDomain_UnhandledException ;
2226 }
2327
2428 private void App_Startup ( object sender , StartupEventArgs e )
@@ -35,47 +39,24 @@ private void App_Startup(object sender, StartupEventArgs e)
3539 }
3640 }
3741 }
38- }
39-
40-
41-
42-
43- // private void WriteLog(object exception)
44- // {
45- // Exception ex = exception as Exception;
46-
47- // using (FileStream fs = File.Open(".//ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
48- // {
49- // fs.Seek(0, SeekOrigin.End);
50- // byte[] buffer = Encoding.Default.GetBytes("-------------------------------------------------------\r\n");
51- // fs.Write(buffer, 0, buffer.Length);
52-
53- // buffer = Encoding.Default.GetBytes(DateTime.Now.ToString() + "\r\n");
54- // fs.Write(buffer, 0, buffer.Length);
55-
56- // if (ex != null)
57- // {
58- // buffer = Encoding.Default.GetBytes("成员名: " + ex.TargetSite + "\r\n");
59- // fs.Write(buffer, 0, buffer.Length);
6042
61- // buffer = Encoding.Default.GetBytes("引发异常的类: " + ex.TargetSite.DeclaringType + "\r\n");
62- // fs.Write(buffer, 0, buffer.Length);
43+ void Current_DispatcherUnhandledException ( object sender , System . Windows . Threading . DispatcherUnhandledExceptionEventArgs e )
44+ {
45+ e . Handled = true ; //使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
46+ LogUtil . WriteErrorLog ( e , "未捕获异常!" ) ;
47+ if ( Constants . DEV )
48+ {
49+ MessageBox . Show ( "GeekDesk遇到一个问题, 不用担心, 这不影响其它操作!" ) ;
50+ }
51+ }
6352
64- // buffer = Encoding.Default.GetBytes("异常信息: " + ex.Message + "\r\n");
65- // fs.Write(buffer, 0, buffer.Length);
53+ void CurrentDomain_UnhandledException ( object sender , UnhandledExceptionEventArgs e )
54+ {
55+ LogUtil . WriteErrorLog ( e , "严重异常!" ) ;
56+ MessageBox . Show ( "GeekDesk遇到未知问题崩溃!" ) ;
57+ }
6658
67- // buffer = Encoding.Default.GetBytes("引发异常的程序集或对象: " + ex.Source + "\r\n");
68- // fs.Write(buffer, 0, buffer.Length);
6959
70- // buffer = Encoding.Default.GetBytes("栈:" + ex.StackTrace + "\r\n");
71- // fs.Write(buffer, 0, buffer.Length);
72- // }
73- // else
74- // {
75- // buffer = Encoding.Default.GetBytes("应用程序错误: " + exception.ToString() + "\r\n");
76- // fs.Write(buffer, 0, buffer.Length);
77- // }
78- // }
60+ }
7961
80- //}
8162}
0 commit comments