From 7cdb58db91800fe221aefb8e6914fdf905e93e00 Mon Sep 17 00:00:00 2001 From: Arne Kiesewetter Date: Mon, 26 Aug 2024 17:20:37 +0200 Subject: [PATCH] Fix old log clearing --- MonkeyLoader/Entrypoint.cs | 36 +++++++++++---------------- MonkeyLoader/Logging/LoggingConfig.cs | 17 +++++++++---- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/MonkeyLoader/Entrypoint.cs b/MonkeyLoader/Entrypoint.cs index efdf12c..14292c4 100644 --- a/MonkeyLoader/Entrypoint.cs +++ b/MonkeyLoader/Entrypoint.cs @@ -1,5 +1,4 @@ using MonkeyLoader; -using MonkeyLoader.Logging; using System; using System.Diagnostics; using System.IO; @@ -13,34 +12,27 @@ internal static class Entrypoint { public static void Start() { - var log = new FileLoggingHandler("MonkeyLoader/MonkeyLog.log"); + var loader = new MonkeyLoader.MonkeyLoader(); + var log = loader.Logger; - foreach (var file in Directory.EnumerateFiles("./")) + try { - try - { - if (Path.GetFileName(file).StartsWith("doorstop", StringComparison.OrdinalIgnoreCase) - && Path.GetExtension(file).Equals(".log", StringComparison.OrdinalIgnoreCase)) - File.Delete(file); - } - catch + foreach (var file in Directory.EnumerateFiles("./")) { - log.Warn(() => $"Failed to delete doorstop logfile - probably the active one: {file}"); + try + { + if (Path.GetFileName(file).StartsWith("doorstop", StringComparison.OrdinalIgnoreCase) + && Path.GetExtension(file).Equals(".log", StringComparison.OrdinalIgnoreCase)) + File.Delete(file); + } + catch + { + log.Warn(() => $"Failed to delete doorstop logfile - probably the active one: {file}"); + } } - } - try - { AppDomain.CurrentDomain.UnhandledException += (sender, e) => log.Fatal(() => (e.ExceptionObject as Exception)?.Format("Unhandled Exception!") ?? "Unhandled Exception!"); - var loggingController = new LoggingController("MonkeyLoader") - { - Level = LoggingLevel.Trace, - Handler = log - }; - - var loader = new MonkeyLoader.MonkeyLoader(loggingController: loggingController); - AppDomain.CurrentDomain.ProcessExit += (_, _) => loader.Shutdown(); var type = Type.GetType("Mono.Runtime"); diff --git a/MonkeyLoader/Logging/LoggingConfig.cs b/MonkeyLoader/Logging/LoggingConfig.cs index 1e62375..9516f8f 100644 --- a/MonkeyLoader/Logging/LoggingConfig.cs +++ b/MonkeyLoader/Logging/LoggingConfig.cs @@ -14,11 +14,11 @@ public sealed class LoggingConfig : ConfigSection public const string FileExtension = ".log"; public const string FileNamePrefix = $"MonkeyLog_"; public const string FileSearchPattern = "*" + FileExtension; - public const string TimestampFormat = "yyyy-MM-ddTHH\\:mm\\:ss"; + public const string TimestampFormat = "yyyy-MM-ddTHH-mm-ss"; public readonly DefiningConfigKey DirectoryPathKey = new("DirectoryPath", "The directory to write log files to. Changes will only take effect on restart.", () => "./MonkeyLoader/Logs"); public readonly DefiningConfigKey FilesToPreserveKey = new("FilesToPreserve", "The number of recent log files to keep around. Set <1 to disable. Changes take effect on restart.", () => 16); - public readonly DefiningConfigKey LevelKey = new("Level", "The logging level used to filter logging requests. Changes take effect immediately.", () => LoggingLevel.Info); + public readonly DefiningConfigKey LevelKey = new("Level", "The logging level used to filter logging requests. May be ignored in the initial startup phase. Changes take effect immediately.", () => LoggingLevel.Info); private readonly Lazy _currentLogFilePath; private LoggingController _loggingController; @@ -71,13 +71,20 @@ public LoggingConfig() public static bool TryGetTimestamp(string logFile, [NotNullWhen(true)] out DateTime? timestamp) { - if (DateTime.TryParseExact(Path.GetFileNameWithoutExtension(logFile), $"{FileNamePrefix}{TimestampFormat}", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var result)) + timestamp = default; + var fileName = Path.GetFileNameWithoutExtension(logFile); + + if (!fileName.StartsWith(FileNamePrefix)) + return false; + + var timestampStr = fileName[FileNamePrefix.Length..]; + + if (DateTime.TryParseExact(timestampStr, TimestampFormat, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var result)) { timestamp = result; return true; } - timestamp = default; return false; } @@ -90,7 +97,7 @@ private void CleanLogDirectory() try { - var logFilesToDelete = Directory.EnumerateFiles(CurrentLogFilePath, FileSearchPattern) + var logFilesToDelete = Directory.EnumerateFiles(DirectoryPath, FileSearchPattern) .Select(logFile => (File: logFile, Success: TryGetTimestamp(logFile, out var timestamp), Created: timestamp)) .Where(logFile => logFile.Success) .OrderByDescending(logFile => logFile.Created)