diff --git a/App.xaml.cs b/App.xaml.cs index b145026..e950c4e 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -1,7 +1,9 @@ using GeekDesk.Constant; +using GeekDesk.Util; using Microsoft.Win32; using System; using System.IO; +using System.Text; using System.Windows; using System.Windows.Media.Animation; using System.Windows.Threading; @@ -19,6 +21,8 @@ namespace GeekDesk public App() { this.Startup += new StartupEventHandler(App_Startup); + Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException; + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; } private void App_Startup(object sender, StartupEventArgs e) @@ -35,47 +39,24 @@ namespace GeekDesk } } } + + void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) + { + e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。 + LogUtil.WriteErrorLog(e, "未捕获异常!"); + if (Constants.DEV) + { + MessageBox.Show("GeekDesk遇到一个问题, 不用担心, 这不影响其它操作!"); + } + } + + void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + LogUtil.WriteErrorLog(e, "严重异常!"); + MessageBox.Show("GeekDesk遇到未知问题崩溃!"); + } + + } - - - - // private void WriteLog(object exception) - // { - // Exception ex = exception as Exception; - - // using (FileStream fs = File.Open(".//ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)) - // { - // fs.Seek(0, SeekOrigin.End); - // byte[] buffer = Encoding.Default.GetBytes("-------------------------------------------------------\r\n"); - // fs.Write(buffer, 0, buffer.Length); - - // buffer = Encoding.Default.GetBytes(DateTime.Now.ToString() + "\r\n"); - // fs.Write(buffer, 0, buffer.Length); - - // if (ex != null) - // { - // buffer = Encoding.Default.GetBytes("成员名: " + ex.TargetSite + "\r\n"); - // fs.Write(buffer, 0, buffer.Length); - - // buffer = Encoding.Default.GetBytes("引发异常的类: " + ex.TargetSite.DeclaringType + "\r\n"); - // fs.Write(buffer, 0, buffer.Length); - - // buffer = Encoding.Default.GetBytes("异常信息: " + ex.Message + "\r\n"); - // fs.Write(buffer, 0, buffer.Length); - - // buffer = Encoding.Default.GetBytes("引发异常的程序集或对象: " + ex.Source + "\r\n"); - // fs.Write(buffer, 0, buffer.Length); - - // buffer = Encoding.Default.GetBytes("栈:" + ex.StackTrace + "\r\n"); - // fs.Write(buffer, 0, buffer.Length); - // } - // else - // { - // buffer = Encoding.Default.GetBytes("应用程序错误: " + exception.ToString() + "\r\n"); - // fs.Write(buffer, 0, buffer.Length); - // } - // } - - //} } diff --git a/Control/Other/IconInfoUrlDialog.xaml.cs b/Control/Other/IconInfoUrlDialog.xaml.cs index c3bd8ca..6962caa 100644 --- a/Control/Other/IconInfoUrlDialog.xaml.cs +++ b/Control/Other/IconInfoUrlDialog.xaml.cs @@ -89,9 +89,10 @@ namespace GeekDesk.Control.Other info.BitmapImage = ImageUtil.GetBitmapIconByPath(ofd.FileName); CommonCode.SaveAppData(MainWindow.appData); } - } catch (Exception) + } catch (Exception ex) { HandyControl.Controls.Growl.WarningGlobal("修改图标失败,已重置为默认图标!"); + LogUtil.WriteErrorLog(ex, "修改图标失败!"); } } diff --git a/Control/UserControls/Config/ThemeControl.xaml.cs b/Control/UserControls/Config/ThemeControl.xaml.cs index 6b54c22..9e8d2f4 100644 --- a/Control/UserControls/Config/ThemeControl.xaml.cs +++ b/Control/UserControls/Config/ThemeControl.xaml.cs @@ -53,8 +53,9 @@ namespace GeekDesk.Control.UserControls.Config appConfig.BacImgName = ofd.FileName; } } - catch (Exception) + catch (Exception ex) { + LogUtil.WriteErrorLog(ex, "修改背景失败,已重置为默认背景!"); HandyControl.Controls.Growl.WarningGlobal("修改背景失败,已重置为默认背景!"); } @@ -69,8 +70,9 @@ namespace GeekDesk.Control.UserControls.Config appConfig.BitmapImage = ImageUtil.Base64ToBitmapImage(Constants.DEFAULT_BAC_IMAGE_BASE64); appConfig.BacImgName = "系统默认"; } - catch (Exception) + catch (Exception ex) { + LogUtil.WriteErrorLog(ex, "修改背景失败2,已重置为默认背景!"); HandyControl.Controls.Growl.WarningGlobal("修改背景失败,已重置为默认背景!"); } diff --git a/Util/ImageUtil.cs b/Util/ImageUtil.cs index 25ac354..7463f23 100644 --- a/Util/ImageUtil.cs +++ b/Util/ImageUtil.cs @@ -202,8 +202,9 @@ namespace GeekDesk.Util return bm; } } - catch (Exception) + catch (Exception ex) { + LogUtil.WriteErrorLog(ex, "获取缩略图失败!filePath=" + filePath); return Base64ToBitmapImage(Constants.DEFAULT_IMG_IMAGE_BASE64); } @@ -302,8 +303,9 @@ namespace GeekDesk.Util ms.Close(); return Convert.ToBase64String(arr); } - catch (Exception) + catch (Exception ex) { + LogUtil.WriteErrorLog(ex, "文件转base64失败!Imagefilename=" + Imagefilename); return null; } } diff --git a/Util/RegisterUtil.cs b/Util/RegisterUtil.cs index d6f723c..b6572e8 100644 --- a/Util/RegisterUtil.cs +++ b/Util/RegisterUtil.cs @@ -48,10 +48,9 @@ namespace GeekDesk.Util key.Close(); } -#pragma warning disable CS0168 // 声明了变量“ex”,但从未使用过 catch (Exception ex) -#pragma warning restore CS0168 // 声明了变量“ex”,但从未使用过 { + LogUtil.WriteErrorLog(ex, "设置开机/取消失败!started=" + started); return false; } } @@ -62,19 +61,17 @@ namespace GeekDesk.Util key.DeleteValue(exeName);//取消开机启动 key.Close(); } -#pragma warning disable CS0168 // 声明了变量“ex”,但从未使用过 catch (Exception ex) -#pragma warning restore CS0168 // 声明了变量“ex”,但从未使用过 { + LogUtil.WriteErrorLog(ex, "取消开机启动失败!started=" + started); return false; } } return true; } -#pragma warning disable CS0168 // 声明了变量“ex”,但从未使用过 catch (Exception ex) -#pragma warning restore CS0168 // 声明了变量“ex”,但从未使用过 { + LogUtil.WriteErrorLog(ex, "取消/开机/失败!started=" + started); if (key != null) { key.Close();