提交记录修改Core/Librarys/Logger.cs+102 −10看文件
| @@ -4,16 +4,41 @@ using System.Collections.Generic; | ||
| 4 | 4 | using System.Diagnostics; |
| 5 | 5 | using System.IO; |
| 6 | 6 | using System.Linq; |
| 7 | 新增:using System.Management; | |
| 8 | 新增:using System.Reflection; | |
| 7 | 9 | using System.Runtime.CompilerServices; |
| 8 | 10 | using System.Text; |
| 9 | 11 | using System.Threading.Tasks; |
| 12 | 新增:using System.Windows.Forms; | |
| 13 | 新增:using System.Windows.Threading; | |
| 10 | 14 | |
| 11 | 15 | namespace Core.Librarys |
| 12 | 16 | { |
| 13 | 删除: public class Logger | |
| 17 | 新增: public static class Logger | |
| 14 | 18 | { |
| 19 | 新增: private static readonly int threshold = 50; | |
| 20 | 新增: | |
| 15 | 21 | private static readonly object writeLock = new object(); |
| 16 | 22 | |
| 23 | 新增: private static List<string> loggers = new List<string>(); | |
| 24 | 新增: | |
| 25 | 新增: | |
| 26 | 新增: static Logger() | |
| 27 | 新增: { | |
| 28 | 新增: // 创建计时器,定时保存log | |
| 29 | 新增: | |
| 30 | 新增: DispatcherTimer dispatcherTimer = new DispatcherTimer(); | |
| 31 | 新增: dispatcherTimer.Interval = new TimeSpan(0, 5, 0); | |
| 32 | 新增: dispatcherTimer.Tick += DispatcherTimer_Tick; | |
| 33 | 新增: dispatcherTimer.Start(); | |
| 34 | 新增: | |
| 35 | 新增: } | |
| 36 | 新增: | |
| 37 | 新增: private static void DispatcherTimer_Tick(object sender, EventArgs e) | |
| 38 | 新增: { | |
| 39 | 新增: Save(true); | |
| 40 | 新增: } | |
| 41 | 新增: | |
| 17 | 42 | private enum LogLevel |
| 18 | 43 | { |
| 19 | 44 | Info, |
| @@ -23,31 +48,46 @@ namespace Core.Librarys | ||
| 23 | 48 | |
| 24 | 49 | public static void Info(string message, [CallerLineNumber] int callerLineNumber = -1, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null) |
| 25 | 50 | { |
| 26 | 删除: message = message + "\r\n[Caller Info] Line:" + callerLineNumber + ",File:" + callerFilePath + ",name:" + callerMemberName; | |
| 27 | 删除: Save(Fromat(LogLevel.Info, message)); | |
| 51 | 新增: message = message + "\r\n[ Caller Info ] Line:" + callerLineNumber + ",File:" + callerFilePath + ",name:" + callerMemberName; | |
| 52 | 新增: message = Fromat(LogLevel.Info, message); | |
| 53 | 新增: | |
| 54 | 新增: loggers.Add(message); | |
| 28 | 55 | } |
| 29 | 56 | |
| 30 | 57 | public static void Warn(string message, [CallerLineNumber] int callerLineNumber = -1, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null) |
| 31 | 58 | { |
| 32 | 删除: message = message + "\r\n[Caller Info] Line:" + callerLineNumber + ",File:" + callerFilePath + ",name:" + callerMemberName; | |
| 33 | 删除: Save(Fromat(LogLevel.Warn, message)); | |
| 59 | 新增: message = message + "\r\n[ Caller Info ] Line:" + callerLineNumber + ",File:" + callerFilePath + ",name:" + callerMemberName; | |
| 60 | 新增: message = Fromat(LogLevel.Warn, message); | |
| 61 | 新增: | |
| 62 | 新增: loggers.Add(message); | |
| 34 | 63 | } |
| 35 | 64 | public static void Error(string message, [CallerLineNumber] int callerLineNumber = -1, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null) |
| 36 | 65 | { |
| 37 | 删除: message = message + "\r\n[Caller Info] Line:" + callerLineNumber + ",File:" + callerFilePath + ",name:" + callerMemberName; | |
| 38 | 删除: Save(Fromat(LogLevel.Error, message)); | |
| 66 | 新增: message = message + "\r\n[ Caller Info ] Line:" + callerLineNumber + ",File:" + callerFilePath + ",name:" + callerMemberName; | |
| 67 | 新增: message = Fromat(LogLevel.Error, message); | |
| 68 | 新增: | |
| 69 | 新增: loggers.Add(message); | |
| 39 | 70 | } |
| 40 | 71 | |
| 41 | 72 | private static string Fromat(LogLevel logLevel, string message) |
| 42 | 73 | { |
| 43 | 74 | message = HandleMessage(message); |
| 44 | 删除: string logText = $"[{logLevel.ToString()}] [{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] \r\n{message}\r\n------------------------\r\n\r\n"; | |
| 75 | 新增: string logText = $"[ {logLevel.ToString()} ] [ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} ] \r\n{message}\r\n------------------------\r\n\r\n"; | |
| 45 | 76 | Debug.WriteLine(logText); |
| 46 | 77 | return logText; |
| 47 | 78 | } |
| 48 | 79 | |
| 49 | 删除: private static void Save(string message) | |
| 80 | 新增: /// <summary> | |
| 81 | 新增: /// 将log写入文件 | |
| 82 | 新增: /// </summary> | |
| 83 | 新增: /// <param name="isNow">是否强制立即写入</param> | |
| 84 | 新增: public static void Save(bool isNow = false) | |
| 50 | 85 | { |
| 86 | 新增: if ((!isNow && loggers.Count < threshold) || loggers.Count == 0) | |
| 87 | 新增: { | |
| 88 | 新增: return; | |
| 89 | 新增: } | |
| 90 | 新增: | |
| 51 | 91 | string loggerName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, |
| 52 | 92 | "Log", DateTime.Now.ToString("yyyy-MM-dd") + ".log"); |
| 53 | 93 | lock (writeLock) |
| @@ -57,8 +97,60 @@ namespace Core.Librarys | ||
| 57 | 97 | { |
| 58 | 98 | Directory.CreateDirectory(dir); |
| 59 | 99 | } |
| 60 | 删除: File.AppendAllText(loggerName, message); | |
| 100 | 新增: | |
| 101 | 新增: if (!File.Exists(loggerName)) | |
| 102 | 新增: { | |
| 103 | 新增: List<string> clientInfo = new List<string>(); | |
| 104 | 新增: | |
| 105 | 新增: // 记录客户端信息 | |
| 106 | 新增: | |
| 107 | 新增: | |
| 108 | 新增: // tai版本号 | |
| 109 | 新增: clientInfo.Add(FromatItem("Tai Version", Assembly.GetExecutingAssembly().GetName().Version.ToString())); | |
| 110 | 新增: clientInfo.Add(FromatItem("WindowsOS Name", GetWindowsVersionName())); | |
| 111 | 新增: clientInfo.Add(FromatItem("Screen Size", GetScreenSize())); | |
| 112 | 新增: clientInfo.Add("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++\r\n\r\n"); | |
| 113 | 新增: | |
| 114 | 新增: File.WriteAllText(loggerName, string.Join("\r\n", clientInfo.ToArray())); | |
| 115 | 新增: } | |
| 116 | 新增: | |
| 117 | 新增: | |
| 118 | 新增: File.AppendAllText(loggerName, string.Join("", loggers.ToArray())); | |
| 119 | 新增: } | |
| 120 | 新增: } | |
| 121 | 新增: | |
| 122 | 新增: | |
| 123 | 新增: private static string GetScreenSize() | |
| 124 | 新增: { | |
| 125 | 新增: return SystemInformation.VirtualScreen.Width + " × " + SystemInformation.VirtualScreen.Height; | |
| 126 | 新增: } | |
| 127 | 新增: private static string GetWindowsVersionName() | |
| 128 | 新增: { | |
| 129 | 新增: string name = string.Empty; | |
| 130 | 新增: try | |
| 131 | 新增: { | |
| 132 | 新增: ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"); | |
| 133 | 新增: foreach (ManagementObject obj in managementObjectSearcher.Get()) | |
| 134 | 新增: { | |
| 135 | 新增: name = obj["Name"].ToString(); | |
| 136 | 新增: } | |
| 137 | 新增: | |
| 138 | 新增: if (!string.IsNullOrEmpty(name) && name.IndexOf("|") != -1) | |
| 139 | 新增: { | |
| 140 | 新增: name = name.Split('|')[0]; | |
| 141 | 新增: } | |
| 61 | 142 | } |
| 143 | 新增: catch | |
| 144 | 新增: { | |
| 145 | 新增: return "[无法获取系统版本]"; | |
| 146 | 新增: } | |
| 147 | 新增: | |
| 148 | 新增: return name; | |
| 149 | 新增: } | |
| 150 | 新增: | |
| 151 | 新增: private static string FromatItem(string name, string text) | |
| 152 | 新增: { | |
| 153 | 新增: return $"[ {name} ] {text};\r\n"; | |
| 62 | 154 | } |
| 63 | 155 | |
| 64 | 156 | private static string HandleMessage(string message) |

