增加日志写入

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

@@ -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;
}
}