3.3 KiB
3.3 KiB
AppLogger
Namespace: FrymasterBadgeApp
Type: static class
Purpose: Centralized, thread-safe logging utility that writes timestamped messages to daily rotating log files, console, and debug output. Includes automatic file rotation and scheduled cleanup.
🔑 Fields
| Name | Type | Access | Description |
|---|---|---|---|
LogDirectory |
string |
private static readonly |
Path to %LOCALAPPDATA% where logs are stored. |
BaseLogName |
string |
private static readonly |
Base filename prefix (FrymasterBadgeApp). |
MaxFileSizeBytes |
long |
private static readonly |
Maximum log file size before rotation (5 MB). |
_lock |
object |
private static readonly |
Synchronization object for thread-safe file writes. |
🛠 Constructor
static AppLogger()
- Access:
static - Behavior: Automatically logs a
DEBUGmessage upon first access to confirm initialization.
📦 Methods
GetCurrentLogPath()
- Access:
private static - Returns:
string - Behavior: Constructs a log file path using the current date in
yyyy-MM-ddformat (e.g.,...\FrymasterBadgeApp_2026-04-13.log).
Info(string message)
- Access:
public static - Parameters:
Name Type Description messagestringThe informational text to log. - Behavior: Wrapper that calls
Log("INFO ", message).
Warn(string message)
- Access:
public static - Parameters:
message(string) - Behavior: Logs a warning-level message (
"WARN ").
Warning(string message)
- Access:
public static - Parameters:
message(string) - Behavior: Alias for
Warn, logs"WARNING "level.
Error(string message, Exception? ex = null)
- Access:
public static - Parameters:
Name Type Description messagestringPrimary error description. exException?Associated exception (optional). - Behavior: Appends
\nException: {ex}ifexis provided, then logs at"ERROR "level.
Debug(string message)
- Access:
public static - Parameters:
message(string) - Behavior: Logs at
"DEBUG"level. Wrapped in#if DEBUG, so calls are compiled out in Release builds.
Log(string level, string message)
- Access:
private static - Parameters:
Name Type Description levelstringLog level string ( INFO,ERROR, etc.)messagestringLog content. - Behavior:
- Formats timestamp (
yyyy-MM-dd HH:mm:ss.fff). - Acquires
_lockfor thread safety. - Checks if current log exceeds
MaxFileSizeBytes. If so, renames to.old(deleting existing.oldfirst). - Appends formatted line to file. Catches & logs
Console.WriteLineif file I/O fails. - Writes to
ConsoleandSystem.Diagnostics.Debug.
- Formats timestamp (
CleanupOldLogs(int daysToKeep = 30)
- Access:
public static - Parameters:
daysToKeep(int, default:30) - Behavior: Scans
LogDirectoryforFrymasterBadgeApp_*.log, checksFile.GetCreationTime, and deletes expired files. Exceptions during deletion are silently caught.
Notes:
- Thread-safe via
lock (_lock). - Log rotation prevents unbounded disk growth.
- Safe to call from any thread.