initial commit
This commit is contained in:
112
Infrastructure/Logging/AppLogger.cs
Normal file
112
Infrastructure/Logging/AppLogger.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using HC_APTBS.Services;
|
||||
|
||||
namespace HC_APTBS.Infrastructure.Logging
|
||||
{
|
||||
/// <summary>
|
||||
/// File-based application logger.
|
||||
/// Log files are written to <c>%UserProfile%\.HC_APTBS\log\</c> with
|
||||
/// daily rotation using the filename pattern <c>LOG_yyyy_MM_dd.txt</c>.
|
||||
/// Entries are appended with a timestamp prefix and a severity tag.
|
||||
/// </summary>
|
||||
public sealed class AppLogger : IAppLogger
|
||||
{
|
||||
// ── Constants ────────────────────────────────────────────────────────────
|
||||
|
||||
private const string TagError = "ERR";
|
||||
private const string TagWarning = "WAR";
|
||||
private const string TagMessage = "MSG";
|
||||
private const string TagDebug = "DBG";
|
||||
|
||||
private static readonly string LogFolder =
|
||||
Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
|
||||
".HC_APTBS", "log");
|
||||
|
||||
// ── IAppLogger ────────────────────────────────────────────────────────────
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Error(string source, string message) => Write(source, message, TagError);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Warning(string source, string message) => Write(source, message, TagWarning);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Info(string source, string message) => Write(source, message, TagMessage);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Debug(string source, string message) => Write(source, message, TagDebug);
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
private static void Write(string source, string message, string tag)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(LogFolder);
|
||||
string path = Path.Combine(LogFolder,
|
||||
$"LOG_{DateTime.Now:yyyy_MM_dd}.txt");
|
||||
|
||||
string line = $"{Timestamp()}[{tag}] {source.ToUpperInvariant()}-> {message}{Environment.NewLine}";
|
||||
File.AppendAllText(path, line);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// If logging fails (disk full, permissions) we swallow the error
|
||||
// rather than crashing the application.
|
||||
Console.WriteLine($"[{tag}] {source}: {message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Returns the current time formatted as <c>[HH:mm:ss] </c>.</summary>
|
||||
private static string Timestamp()
|
||||
=> DateTime.Now.ToString("[HH:mm:ss] ", DateTimeFormatInfo.InvariantInfo);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the application start banner to today's log file.
|
||||
/// Call once from <c>App.OnStartup</c>.
|
||||
/// </summary>
|
||||
public void WriteStartupBanner(string version)
|
||||
{
|
||||
string sep = "******************************************";
|
||||
string banner =
|
||||
sep + Environment.NewLine +
|
||||
sep + Environment.NewLine +
|
||||
$"** STARTED {version} {DateTime.Now:dd/MM/yyyy HH:mm:ss} **" + Environment.NewLine +
|
||||
sep + Environment.NewLine +
|
||||
sep + Environment.NewLine;
|
||||
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(LogFolder);
|
||||
File.AppendAllText(
|
||||
Path.Combine(LogFolder, $"LOG_{DateTime.Now:yyyy_MM_dd}.txt"),
|
||||
banner);
|
||||
}
|
||||
catch { /* swallow */ }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the application shutdown footer to today's log file.
|
||||
/// Call once from <c>App.OnExit</c>.
|
||||
/// </summary>
|
||||
public void WriteShutdownFooter()
|
||||
{
|
||||
string footer =
|
||||
"-----------------------------------------------" + Environment.NewLine +
|
||||
$"-- STOPPED {DateTime.Now:dd/MM/yyyy HH:mm:ss} --" + Environment.NewLine +
|
||||
"-----------------------------------------------" + Environment.NewLine +
|
||||
Environment.NewLine;
|
||||
|
||||
try
|
||||
{
|
||||
File.AppendAllText(
|
||||
Path.Combine(LogFolder, $"LOG_{DateTime.Now:yyyy_MM_dd}.txt"),
|
||||
footer);
|
||||
}
|
||||
catch { /* swallow */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user