From b82241bbd31731a916023ea204511c724b7ae138 Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Sun, 30 Apr 2023 00:32:09 +0100 Subject: [PATCH] Auto clean up log files older than a week --- Bloxstrap/App.xaml.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Bloxstrap/App.xaml.cs b/Bloxstrap/App.xaml.cs index b9b31c2..3b11552 100644 --- a/Bloxstrap/App.xaml.cs +++ b/Bloxstrap/App.xaml.cs @@ -74,9 +74,24 @@ namespace Bloxstrap // if we're running for the first time or uninstalling, log to temp folder // else, log to bloxstrap folder - string logdir = IsFirstRun || IsUninstall ? Path.Combine(Directories.LocalAppData, "Temp") : Path.Combine(Directories.Base, "Logs"); + bool isUsingTempDir = IsFirstRun || IsUninstall; + string logdir = isUsingTempDir ? Path.Combine(Directories.LocalAppData, "Temp") : Path.Combine(Directories.Base, "Logs"); string timestamp = DateTime.UtcNow.ToString("yyyyMMdd'T'HHmmss'Z'"); + Logger.Initialize(Path.Combine(logdir, $"{ProjectName}_{timestamp}.log")); + + // clean up any logs older than a week + if (!isUsingTempDir) + { + foreach (FileInfo log in new DirectoryInfo(logdir).GetFiles()) + { + if (log.LastWriteTimeUtc.AddDays(7) > DateTime.UtcNow) + continue; + + Logger.WriteLine($"[App::InitLog] Cleaning up old log file '{log.Name}'"); + log.Delete(); + } + } } void GlobalExceptionHandler(object sender, DispatcherUnhandledExceptionEventArgs e)