diff --git a/DellMonitorControl/DebugLogger.cs b/DellMonitorControl/DebugLogger.cs new file mode 100644 index 0000000..cf2f070 --- /dev/null +++ b/DellMonitorControl/DebugLogger.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace DellMonitorControl; + +public static class DebugLogger +{ + private static readonly List _logs = new(); + private static readonly object _lock = new(); + private static readonly string _logFilePath; + + static DebugLogger() + { + var tempPath = Path.Combine(Path.GetTempPath(), "CMM"); + Directory.CreateDirectory(tempPath); + _logFilePath = Path.Combine(tempPath, "debug.log"); + } + + public static void Log(string message) + { + var entry = $"[{DateTime.Now:HH:mm:ss.fff}] {message}"; + lock (_lock) + { + _logs.Add(entry); + try + { + File.AppendAllText(_logFilePath, entry + Environment.NewLine); + } + catch { } + } + } + + public static void LogError(string message, Exception? ex = null) + { + var entry = ex != null + ? $"[{DateTime.Now:HH:mm:ss.fff}] ERROR: {message} - {ex.GetType().Name}: {ex.Message}" + : $"[{DateTime.Now:HH:mm:ss.fff}] ERROR: {message}"; + lock (_lock) + { + _logs.Add(entry); + if (ex != null) + _logs.Add($" Stack: {ex.StackTrace}"); + try + { + File.AppendAllText(_logFilePath, entry + Environment.NewLine); + if (ex != null) + File.AppendAllText(_logFilePath, $" Stack: {ex.StackTrace}" + Environment.NewLine); + } + catch { } + } + } + + public static string GetLogs() + { + lock (_lock) + { + return string.Join(Environment.NewLine, _logs); + } + } + + public static void Clear() + { + lock (_lock) + { + _logs.Clear(); + try + { + File.WriteAllText(_logFilePath, ""); + } + catch { } + } + } + + public static string LogFilePath => _logFilePath; +} diff --git a/DellMonitorControl/MainWindow.xaml b/DellMonitorControl/MainWindow.xaml index e827b64..4da7a43 100644 --- a/DellMonitorControl/MainWindow.xaml +++ b/DellMonitorControl/MainWindow.xaml @@ -215,6 +215,9 @@ +