增加日志写入

This commit is contained in:
liufei
2021-12-22 13:14:54 +08:00
parent 8d4201f5ba
commit 353acec75e
22 changed files with 160 additions and 151 deletions

View File

@@ -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;
@@ -35,47 +37,20 @@ namespace GeekDesk
}
}
}
void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
e.Handled = true;//使用这一行代码告诉运行时该异常被处理了不再作为UnhandledException抛出了。
LogUtil.WriteErrorLog(e, "未捕获异常!");
}
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

@@ -20,6 +20,10 @@ namespace GeekDesk.Constant
/// </summary>
public static string DATA_FILE_PATH = APP_DIR + "Data"; //app数据文件路径
public static string LOG_FILE_PATH = APP_DIR + "Log.log";
public static string ERROR_FILE_PATH = APP_DIR + "Error.log";
//默认文件夹图标
public static string DEFAULT_DIR_IMAGE_BASE64 = "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAASISURBVHhe7dvPi9R1HMfx/Qu6RYfqEp1CZ8Yko0MQRNEPgnQ36FJEFyPoF/0DHbx06dSlnVkF6SgEgocOYUEG0cFWpQ476yhbQfTDslJxk+k79S1IP/NSF3f2O+7jBY/TKgvz/j5nXMUZMzMzMzMzMzMzMzMzMzMzu959023dsbSw9YGlbuchyk7v3XbP0t5tt9Yvmd3MW3r//luWu623TvbahytDrtvBQa/9xvJ85776JbWbZf359s7quCcKR2cNqjeaQ/35zgv1y2vTvOqgOy4/MDdG9abzwWC+06lfapu2jf4cXTosN9SZ5W7n9folt2nZ6Ifw6niDy47JelloH6hfepuGLffa7xQPyfpZaL9Xv/zW5K307r27eEDWXfXG9GJ9BmvqqneyV0rHYzL63a3b61NYEzfodT4rHY4J8UetZq/6mF8pHo6J8SnS4JUOxqS19tTnsKatfDAm7Hh9DmvaCsdiAwx6rcfqk1iTVjoWkzfotd+uT2JNWulYTN5yr/VFfRJr0krHYoN0t7Tqs1hTVjwUG6L6OeS1+izWlJUOxYb5xb+JNGyFI7Gxfqp+YN83+h+drK/+wtbH+3u33FmnUF7hQLCpLPfaSycX2u/WSfx/pd8Am1H1yf3d6FOlTuOflX4hbGrd1vN1HgKBou6W2wQC4+0XCASnuu1dAoGxWnsEAmMMeq1DAoExRn/tKxAIBALBmgMZ7Nsx/Pbgc9B4J3udK57fa7WmQEbf1GyatnLg6eKzfDVrCuTcypH625pNx37rHyo+y1ezpkAufL9Yf1uz6dgfpz8uPstXIxDbFBOIWZhAzMIEYhYmELMwgZiFCcQsTCBmYQIxCxOIWZhAzMIEYhYmELMwgZiFrTmQ0/u3D89+8tjw/OdPXbPVr18eXlp6E6bG6le7i8/yOL8efnR4at+24czvR54Y/nl8DrjM6INjpvQFYG548ehOgcA4AoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCC4e3XVJIDBG9QmyKhAY4+Li7HmBwBirx2bPCgTGOTH3g0BgHIFAIBAI6kDOXPEFoDLbn7l0bG53+YuwyS3OPTsz2oVjs3etLs49OM65T5/c9fNHj7z044cPvwrTbvQsj57p0rP+r3NfPnP733GYmZmZmZmZmZmZmZmZmZnZf5uZ+QsdP8v10G4AYgAAAABJRU5ErkJggg==";

View File

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

View File

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

View File

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

View File

@@ -195,9 +195,10 @@ namespace GeekDesk.Control.UserControls.PannelCard
p.Start();
icon.Count++;
}
catch (Exception)
catch (Exception e)
{
HandyControl.Controls.Growl.WarningGlobal("程序启动失败(不支持的启动方式)!");
LogUtil.WriteErrorLog(e, "程序启动失败:path=" + icon.Path + ",type=" + type);
}
}

View File

@@ -168,9 +168,10 @@ namespace GeekDesk.Control.Windows
LoadingEle.Visibility = Visibility.Collapsed;
CustomIcon.Visibility = Visibility.Visible;
}
catch (Exception)
catch (Exception e)
{
HandyControl.Controls.Growl.WarningGlobal("加载远程图标异常!");
LogUtil.WriteErrorLog(e, "加载远程图标异常!");
}
}

View File

@@ -38,9 +38,9 @@ namespace GeekDesk.Control.Windows
InitializeComponent();
DataHandle(jo);
}
catch (Exception)
catch (Exception e)
{
LogUtil.WriteErrorLog(e, "更新信息格式化失败!");
}
}

View File

@@ -195,13 +195,13 @@
<Compile Include="Thread\DispatcherBuild.cs" />
<Compile Include="Thread\UpdateThread.cs" />
<Compile Include="Util\AeroGlassHelper.cs" />
<Compile Include="Util\Functions.cs" />
<Compile Include="Util\GlobalHotKey.cs" />
<Compile Include="Util\CommonCode.cs" />
<Compile Include="Util\ConsoleManager.cs" />
<Compile Include="Util\DragAdorner.cs" />
<Compile Include="Util\FileIcon.cs" />
<Compile Include="Util\FileUtil.cs" />
<Compile Include="Util\LogUtil.cs" />
<Compile Include="Util\MarginHide.cs" />
<Compile Include="Util\HttpUtil.cs" />
<Compile Include="Util\ImageUtil.cs" />

View File

@@ -59,12 +59,9 @@ namespace GeekDesk.Thread
}));
}
}
#pragma warning disable CS0168 // 声明了变量“e”但从未使用过
} catch (Exception e)
#pragma warning restore CS0168 // 声明了变量“e”但从未使用过
{
//不做处理
//MessageBox.Show(e.Message);
LogUtil.WriteErrorLog(e, "检测更新失败!");
}
}
}

View File

@@ -36,6 +36,7 @@ namespace GeekDesk.Util
catch (Exception e)
#pragma warning restore CS0168 // 声明了变量“e”但从未使用过
{
LogUtil.WriteErrorLog(e, "获取文件图标失败! filePath=" + filePath);
return null;
}
}

View File

@@ -1,87 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace GeekDesk.Util
{
public class Functions
{
public static string GetShortcutTarget(string file)
{
try
{
if (System.IO.Path.GetExtension(file).ToLower() != ".lnk")
{
throw new Exception("Supplied file must be a .LNK file");
}
FileStream fileStream = File.Open(file, FileMode.Open, FileAccess.Read);
using (System.IO.BinaryReader fileReader = new BinaryReader(fileStream))
{
fileStream.Seek(0x14, SeekOrigin.Begin); // Seek to flags
uint flags = fileReader.ReadUInt32(); // Read flags
if ((flags & 1) == 1)
{ // Bit 1 set means we have to
// skip the shell item ID list
fileStream.Seek(0x4c, SeekOrigin.Begin); // Seek to the end of the header
uint offset = fileReader.ReadUInt16(); // Read the length of the Shell item ID list
fileStream.Seek(offset, SeekOrigin.Current); // Seek past it (to the file locator info)
}
long fileInfoStartsAt = fileStream.Position; // Store the offset where the file info
// structure begins
uint totalStructLength = fileReader.ReadUInt32(); // read the length of the whole struct
fileStream.Seek(0xc, SeekOrigin.Current); // seek to offset to base pathname
uint fileOffset = fileReader.ReadUInt32(); // read offset to base pathname
// the offset is from the beginning of the file info struct (fileInfoStartsAt)
fileStream.Seek((fileInfoStartsAt + fileOffset), SeekOrigin.Begin); // Seek to beginning of
// base pathname (target)
long pathLength = (totalStructLength + fileInfoStartsAt) - fileStream.Position - 2; // read
// the base pathname. I don't need the 2 terminating nulls.
char[] linkTarget = fileReader.ReadChars((int)pathLength); // should be unicode safe
var link = new string(linkTarget);
int begin = link.IndexOf("\0\0");
if (begin > -1)
{
int end = link.IndexOf("\\\\", begin + 2) + 2;
end = link.IndexOf('\0', end) + 1;
string firstPart = link.Substring(0, begin);
string secondPart = link.Substring(end);
return firstPart + secondPart;
}
else
{
return link;
}
}
}
catch
{
return "";
}
}
}
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
System.Console.WriteLine("Please try again with a file path");
}
else
{
System.Console.WriteLine("LNK File: " + args[0]);
System.Console.WriteLine("LNK Path: " + Functions.GetShortcutTarget(args[0]));
}
}
}
}

View File

@@ -53,9 +53,10 @@ namespace GeekDesk.Util
UnregisterHotKey(handleTemp[id].Handle, id);
GlobalHotKey.handleTemp[id].Dispose();
GlobalHotKey.handleTemp.Remove(id);
} catch
} catch (Exception e)
{
//nothing
LogUtil.WriteErrorLog(e, "取消注册热键异常!");
}

View File

@@ -202,8 +202,9 @@ namespace GeekDesk.Util
return bm;
}
}
catch (Exception)
catch (Exception e)
{
LogUtil.WriteErrorLog(e, "获取文件缩略图失败!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 e)
{
LogUtil.WriteErrorLog(e, "图片文件转base64失败!Imagefilename=" + Imagefilename + ",ImageFormat="+format);
return null;
}
}

76
Util/LogUtil.cs Normal file
View File

@@ -0,0 +1,76 @@
using GeekDesk.Constant;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeekDesk.Util
{
public class LogUtil
{
public static void WriteErrorLog(object exception, string msg = "")
{
try
{
Exception ex = exception as Exception;
using (FileStream fs = File.Open(Constants.ERROR_FILE_PATH, 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() + msg + "\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);
}
}
} catch
{
}
}
public static void WriteLog(string msg)
{
try {
using (FileStream fs = File.Open(Constants.LOG_FILE_PATH, 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() + msg + "\r\n" );
fs.Write(buffer, 0, buffer.Length);
}
} catch { }
}
}
}

View File

@@ -52,6 +52,7 @@ namespace GeekDesk.Util
catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”但从未使用过
{
LogUtil.WriteErrorLog(ex, "注册启动Error1!");
return false;
}
}
@@ -66,6 +67,7 @@ namespace GeekDesk.Util
catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”但从未使用过
{
LogUtil.WriteErrorLog(ex, "注册启动Error2!");
return false;
}
}
@@ -79,6 +81,7 @@ namespace GeekDesk.Util
{
key.Close();
}
LogUtil.WriteErrorLog(ex, "注册启动Error3!");
return false;
}
}

View File

@@ -1,6 +1,7 @@

using GeekDesk.Constant;
using GeekDesk.Util;
using Newtonsoft.Json;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
@@ -722,5 +723,9 @@ namespace GeekDesk.ViewModel
#endregion
public override String ToString()
{
return JsonConvert.SerializeObject(this);
}
}
}

View File

@@ -1,4 +1,5 @@
using GeekDesk.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@@ -84,6 +85,11 @@ namespace GeekDesk.ViewModel
}
}
public override String ToString()
{
return JsonConvert.SerializeObject(this);
}
[field: NonSerializedAttribute()]
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)

View File

@@ -1,8 +1,11 @@
using GeekDesk.Constant;
using GeekDesk.Util;
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Media.Imaging;
/// <summary>
@@ -150,10 +153,10 @@ namespace GeekDesk.ViewModel
}
}
public override String ToString()
{
return JsonConvert.SerializeObject(this);
}
[field: NonSerializedAttribute()]

View File

@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -36,5 +37,11 @@ namespace GeekDesk.ViewModel
name = value;
}
}
public override String ToString()
{
return JsonConvert.SerializeObject(this);
}
}
}

View File

@@ -1,5 +1,6 @@
using GeekDesk.Constant;
using GeekDesk.Util;
using Newtonsoft.Json;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
@@ -142,6 +143,11 @@ namespace GeekDesk.ViewModel
}
}
public override String ToString()
{
return JsonConvert.SerializeObject(this);
}
[field: NonSerializedAttribute()]
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)

View File

@@ -1,5 +1,6 @@
using GeekDesk.Constant;
using GeekDesk.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@@ -129,7 +130,10 @@ namespace GeekDesk.ViewModel
// }
//}
public override String ToString()
{
return JsonConvert.SerializeObject(this);
}
[field: NonSerializedAttribute()]