@@ -62,66 +62,47 @@ private void OnUnhandledExceptionOccurred(object sender, UnhandledExceptionEvent
6262 //var message = $"Message: {(e.ExceptionObject as Exception)?.Message}, StackTrace: {(e.ExceptionObject as Exception)?.StackTrace}";
6363 //Logger.Log(message, Category.Exception, Priority.High);
6464 // ------------------------------------------------------------------------------------------------------------------------------------
65- var exception = ( Exception ) e . ExceptionObject ;
66- var log = new StringBuilder ( ) ;
67- log . AppendLine ( "程序在运行时遇到不可预料的错误" ) ;
68- log . AppendLine ( "=======追踪开始=======" ) ;
69- log . AppendLine ( ) ;
70- log . AppendLine ( "Time: " + DateTime . Now ) ;
71- log . AppendLine ( "Type: " + exception . GetType ( ) . Name ) ;
72- log . AppendLine ( "Message: " + exception . Message ) ;
73- log . AppendLine ( "Version: 0.1.0.63" ) ;
74- log . AppendLine ( "StackTrace: " ) ;
75- log . AppendLine ( exception . StackTrace ) ;
76- log . AppendLine ( ) ;
77- log . AppendLine ( "=======追踪结束=======" ) ;
78- log . AppendLine ( "请将以上信息提供给开发者以供参考" ) ;
79- if ( File . Exists ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "Error.log" ) ) )
80- {
81- try
82- {
83- File . Delete ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "Error.log" ) ) ;
84- }
85- catch
86- {
87- throw exception ;
88- }
89- }
90- File . WriteAllText ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "Error.log" ) , log . ToString ( ) ) ;
91- throw exception ;
65+ this . CatchException ( e . ExceptionObject as Exception ) ;
9266 }
9367
9468 private void OnDispatcherUnhandledExceptionOccurred ( object sender , DispatcherUnhandledExceptionEventArgs e )
9569 {
9670 var message = $ "Message: { e . Exception . Message } , StackTrace: { Environment . NewLine } { e . Exception . StackTrace } { Environment . NewLine } ";
9771 Logger . Log ( message , Category . Exception , Priority . High ) ;
98- // ------------------------------------------------------------------------------------------------------------------------------------
72+ this . CatchException ( e . Exception ) ;
73+ }
74+
75+ private void CatchException ( Exception error )
76+ {
77+ if ( error == null ) return ;
78+
9979 var log = new StringBuilder ( ) ;
10080 log . AppendLine ( "程序在运行时遇到不可预料的错误" ) ;
10181 log . AppendLine ( "=======追踪开始=======" ) ;
10282 log . AppendLine ( ) ;
10383 log . AppendLine ( "Time: " + DateTime . Now ) ;
104- log . AppendLine ( "Type: " + e . GetType ( ) . Name ) ;
84+ log . AppendLine ( "Type: " + error . GetType ( ) . Name ) ;
10585 log . AppendLine ( "Version: 0.1.0.63" ) ;
106- log . AppendLine ( "Message: " + e . Exception == null ? "无信息" : e . Exception . Message ) ;
86+ log . AppendLine ( "Message: " + error . Message ) ;
10787 log . AppendLine ( "StackTrace: " ) ;
108- log . AppendLine ( e . Exception == null ? "无信息" : e . Exception . StackTrace ) ;
88+ log . AppendLine ( error . StackTrace ) ;
10989 log . AppendLine ( ) ;
11090 log . AppendLine ( "=======追踪结束=======" ) ;
11191 log . AppendLine ( "请将以上信息提供给开发者以供参考" ) ;
112- if ( File . Exists ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "Error.log" ) ) )
92+
93+ var path = Path . Combine ( Directory . GetCurrentDirectory ( ) , "Error.log" ) ;
94+ try
95+ {
96+ File . WriteAllText ( path , log . ToString ( ) ) ;
97+ }
98+ catch ( Exception e )
99+ {
100+ this . Logger . Log ( e . ToString ( ) , Category . Exception , Priority . High ) ;
101+ }
102+ finally
113103 {
114- try
115- {
116- File . Delete ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "Error.log" ) ) ;
117- }
118- catch
119- {
120- throw e . Exception ;
121- }
104+ Environment . Exit ( - 1 ) ;
122105 }
123- File . WriteAllText ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "Error.log" ) , log . ToString ( ) ) ;
124- throw e . Exception ;
125106 }
126107 }
127108}
0 commit comments