using System; using System.Globalization; using System.IO; using HC_APTBS.Services; namespace HC_APTBS.Infrastructure.Logging { /// /// File-based application logger. /// Log files are written to %UserProfile%\.HC_APTBS\log\ with /// daily rotation using the filename pattern LOG_yyyy_MM_dd.txt. /// Entries are appended with a timestamp prefix and a severity tag. /// 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 ──────────────────────────────────────────────────────────── /// public void Error(string source, string message) => Write(source, message, TagError); /// public void Warning(string source, string message) => Write(source, message, TagWarning); /// public void Info(string source, string message) => Write(source, message, TagMessage); /// 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}"); } } /// Returns the current time formatted as [HH:mm:ss] . private static string Timestamp() => DateTime.Now.ToString("[HH:mm:ss] ", DateTimeFormatInfo.InvariantInfo); /// /// Writes the application start banner to today's log file. /// Call once from App.OnStartup. /// 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 */ } } /// /// Writes the application shutdown footer to today's log file. /// Call once from App.OnExit. /// 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 */ } } } }