捕获异常, 写出日志

This commit is contained in:
liufei
2022-01-09 17:33:49 +08:00
parent fdd8c8bb10
commit 145564bfc5
5 changed files with 35 additions and 52 deletions

View File

@@ -1,7 +1,9 @@
using GeekDesk.Constant; using GeekDesk.Constant;
using GeekDesk.Util;
using Microsoft.Win32; using Microsoft.Win32;
using System; using System;
using System.IO; using System.IO;
using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Media.Animation; using System.Windows.Media.Animation;
using System.Windows.Threading; using System.Windows.Threading;
@@ -19,6 +21,8 @@ namespace GeekDesk
public App() public App()
{ {
this.Startup += new StartupEventHandler(App_Startup); this.Startup += new StartupEventHandler(App_Startup);
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
} }
private void App_Startup(object sender, StartupEventArgs e) 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);
// }
// }
//}
} }

View File

@@ -89,9 +89,10 @@ namespace GeekDesk.Control.Other
info.BitmapImage = ImageUtil.GetBitmapIconByPath(ofd.FileName); info.BitmapImage = ImageUtil.GetBitmapIconByPath(ofd.FileName);
CommonCode.SaveAppData(MainWindow.appData); CommonCode.SaveAppData(MainWindow.appData);
} }
} catch (Exception) } catch (Exception ex)
{ {
HandyControl.Controls.Growl.WarningGlobal("修改图标失败,已重置为默认图标!"); HandyControl.Controls.Growl.WarningGlobal("修改图标失败,已重置为默认图标!");
LogUtil.WriteErrorLog(ex, "修改图标失败!");
} }
} }

View File

@@ -53,8 +53,9 @@ namespace GeekDesk.Control.UserControls.Config
appConfig.BacImgName = ofd.FileName; appConfig.BacImgName = ofd.FileName;
} }
} }
catch (Exception) catch (Exception ex)
{ {
LogUtil.WriteErrorLog(ex, "修改背景失败,已重置为默认背景!");
HandyControl.Controls.Growl.WarningGlobal("修改背景失败,已重置为默认背景!"); HandyControl.Controls.Growl.WarningGlobal("修改背景失败,已重置为默认背景!");
} }
@@ -69,8 +70,9 @@ namespace GeekDesk.Control.UserControls.Config
appConfig.BitmapImage = ImageUtil.Base64ToBitmapImage(Constants.DEFAULT_BAC_IMAGE_BASE64); appConfig.BitmapImage = ImageUtil.Base64ToBitmapImage(Constants.DEFAULT_BAC_IMAGE_BASE64);
appConfig.BacImgName = "系统默认"; appConfig.BacImgName = "系统默认";
} }
catch (Exception) catch (Exception ex)
{ {
LogUtil.WriteErrorLog(ex, "修改背景失败2,已重置为默认背景!");
HandyControl.Controls.Growl.WarningGlobal("修改背景失败,已重置为默认背景!"); HandyControl.Controls.Growl.WarningGlobal("修改背景失败,已重置为默认背景!");
} }

View File

@@ -202,8 +202,9 @@ namespace GeekDesk.Util
return bm; return bm;
} }
} }
catch (Exception) catch (Exception ex)
{ {
LogUtil.WriteErrorLog(ex, "获取缩略图失败!filePath=" + filePath);
return Base64ToBitmapImage(Constants.DEFAULT_IMG_IMAGE_BASE64); return Base64ToBitmapImage(Constants.DEFAULT_IMG_IMAGE_BASE64);
} }
@@ -302,8 +303,9 @@ namespace GeekDesk.Util
ms.Close(); ms.Close();
return Convert.ToBase64String(arr); return Convert.ToBase64String(arr);
} }
catch (Exception) catch (Exception ex)
{ {
LogUtil.WriteErrorLog(ex, "文件转base64失败!Imagefilename=" + Imagefilename);
return null; return null;
} }
} }

View File

@@ -48,10 +48,9 @@ namespace GeekDesk.Util
key.Close(); key.Close();
} }
#pragma warning disable CS0168 // 声明了变量“ex”但从未使用过
catch (Exception ex) catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”但从未使用过
{ {
LogUtil.WriteErrorLog(ex, "设置开机/取消失败!started=" + started);
return false; return false;
} }
} }
@@ -62,19 +61,17 @@ namespace GeekDesk.Util
key.DeleteValue(exeName);//取消开机启动 key.DeleteValue(exeName);//取消开机启动
key.Close(); key.Close();
} }
#pragma warning disable CS0168 // 声明了变量“ex”但从未使用过
catch (Exception ex) catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”但从未使用过
{ {
LogUtil.WriteErrorLog(ex, "取消开机启动失败!started=" + started);
return false; return false;
} }
} }
return true; return true;
} }
#pragma warning disable CS0168 // 声明了变量“ex”但从未使用过
catch (Exception ex) catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”但从未使用过
{ {
LogUtil.WriteErrorLog(ex, "取消/开机/失败!started=" + started);
if (key != null) if (key != null)
{ {
key.Close(); key.Close();