|
|
@@ -0,0 +1,48 @@
|
|
|
+using System;
|
|
|
+using System.IO;
|
|
|
+
|
|
|
+namespace AutoFocusTool.Logging
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// 静态文件日志器:程序启动时按时间戳建一个文件,本次运行所有日志写入它。
|
|
|
+ /// 线程安全(lock),每条 flush。格式:HH:mm:ss.fff [级别] [类别] 消息
|
|
|
+ /// </summary>
|
|
|
+ public static class FileLogger
|
|
|
+ {
|
|
|
+ private static readonly object _lock = new object();
|
|
|
+ private static string _path;
|
|
|
+
|
|
|
+ /// <summary>启动时调用一次:在 Logs/ 下建 yyyy-MM-dd_HHmmss.log。</summary>
|
|
|
+ public static void Init()
|
|
|
+ {
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ if (_path != null) return;
|
|
|
+ string dir = @"C:\claudeFile\TL\AutoFocusTool\Logs";
|
|
|
+ Directory.CreateDirectory(dir);
|
|
|
+ _path = Path.Combine(dir, $"{DateTime.Now:yyyy-MM-dd_HHmmss}.log");
|
|
|
+ Write("INFO", "APP", "==== 日志开始 ====");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Info(string category, string msg) => Write("INFO", category, msg);
|
|
|
+ public static void Action(string category, string msg) => Write("ACTION", category, msg);
|
|
|
+ public static void Data(string category, string msg) => Write("DATA", category, msg);
|
|
|
+ public static void Warn(string category, string msg) => Write("WARN", category, msg);
|
|
|
+ public static void Error(string category, string msg) => Write("ERROR", category, msg);
|
|
|
+
|
|
|
+ private static void Write(string level, string category, string msg)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ if (_path == null) return; // 未 Init 则静默丢弃
|
|
|
+ string line = $"{DateTime.Now:HH:mm:ss.fff} [{level,-6}] [{category,-6}] {msg}";
|
|
|
+ File.AppendAllText(_path, line + Environment.NewLine);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch { /* 日志失败不能影响主流程 */ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|