82 lines
3.3 KiB
Markdown
82 lines
3.3 KiB
Markdown
# 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 `DEBUG` message 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-dd` format (e.g., `...\FrymasterBadgeApp_2026-04-13.log`).
|
|
|
|
### `Info(string message)`
|
|
- **Access:** `public static`
|
|
- **Parameters:**
|
|
| Name | Type | Description |
|
|
|------|------|-------------|
|
|
| `message` | `string` | The 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 |
|
|
|------|------|-------------|
|
|
| `message` | `string` | Primary error description. |
|
|
| `ex` | `Exception?` | Associated exception (optional). |
|
|
- **Behavior:** Appends `\nException: {ex}` if `ex` is 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 |
|
|
|------|------|-------------|
|
|
| `level` | `string` | Log level string (`INFO`, `ERROR`, etc.) |
|
|
| `message` | `string` | Log content. |
|
|
- **Behavior:**
|
|
1. Formats timestamp (`yyyy-MM-dd HH:mm:ss.fff`).
|
|
2. Acquires `_lock` for thread safety.
|
|
3. Checks if current log exceeds `MaxFileSizeBytes`. If so, renames to `.old` (deleting existing `.old` first).
|
|
4. Appends formatted line to file. Catches & logs `Console.WriteLine` if file I/O fails.
|
|
5. Writes to `Console` and `System.Diagnostics.Debug`.
|
|
|
|
### `CleanupOldLogs(int daysToKeep = 30)`
|
|
- **Access:** `public static`
|
|
- **Parameters:** `daysToKeep` (`int`, default: `30`)
|
|
- **Behavior:** Scans `LogDirectory` for `FrymasterBadgeApp_*.log`, checks `File.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. |