Add debug logging with Show Log button
- DebugLogger class saves logs to %TEMP%\CMM\debug.log - Show Log button appears after 3s if loading takes too long - Log viewer with Copy to Clipboard and Open Log File options - Detailed logging throughout LoadMonitors for debugging 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
77
DellMonitorControl/DebugLogger.cs
Normal file
77
DellMonitorControl/DebugLogger.cs
Normal file
@@ -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<string> _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;
|
||||
}
|
||||
Reference in New Issue
Block a user