From 66ba0cf2875b0593af123d9f0f652efe23f022bd Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 6 Apr 2018 00:07:23 +0200 Subject: [PATCH] Fixed default logger behaviour on closing --- SweetLib/Utils/Logger/Logger.cs | 2 +- .../Memory/ArchivableConsoleLogMemory.cs | 65 +++++++++++-------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/SweetLib/Utils/Logger/Logger.cs b/SweetLib/Utils/Logger/Logger.cs index daa682d..b358939 100644 --- a/SweetLib/Utils/Logger/Logger.cs +++ b/SweetLib/Utils/Logger/Logger.cs @@ -37,7 +37,7 @@ namespace SweetLib.Utils.Logger /// /// The default which will be used for any logging action, if no custom is set as parameter. /// - public static ILogMemory DefaultLogMemory = new ArchivableConsoleLogMemory(); + public static ILogMemory DefaultLogMemory { get; set; } = new ArchivableConsoleLogMemory(); /// /// Will log a message into the global . diff --git a/SweetLib/Utils/Logger/Memory/ArchivableConsoleLogMemory.cs b/SweetLib/Utils/Logger/Memory/ArchivableConsoleLogMemory.cs index b0c64bf..261a582 100644 --- a/SweetLib/Utils/Logger/Memory/ArchivableConsoleLogMemory.cs +++ b/SweetLib/Utils/Logger/Memory/ArchivableConsoleLogMemory.cs @@ -8,7 +8,7 @@ using SweetLib.Utils.Logger.Message; namespace SweetLib.Utils.Logger.Memory { - public class ArchivableConsoleLogMemory : ILogMemory + public class ArchivableConsoleLogMemory : ILogMemory, IDisposable { private string TempFile { get; } = Path.GetTempFileName(); @@ -26,40 +26,54 @@ namespace SweetLib.Utils.Logger.Memory { ArchiveFile = archiveFile; - QueueTimer = new Timer(e => ProcessQueue(), null, TimeSpan.Zero, TimeSpan.FromSeconds(30)); + QueueTimer = new Timer(e => + { + if (Monitor.TryEnter(this)) + { + try + { + ProcessQueue(); + } + finally + { + Monitor.Exit(this); + } + } + }, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(500)); } ~ArchivableConsoleLogMemory() { - Dispose(false); + Dispose(true); + GC.SuppressFinalize(this); } - private void ProcessQueue(bool isDisposing = false) + private void ProcessQueue(bool disposing = false) { + // quick exit if (LogQueue.IsEmpty) return; - // if we are disposing no need to lock. This might cause issues! - if (!isDisposing) + if (!disposing) { lock (this) { - if (LogQueue.IsEmpty) - return; - - LogMessage message; - if (LogQueue.TryDequeue(out message)) - File.AppendAllText(TempFile, message.ToString()); + while (!LogQueue.IsEmpty) + { + LogMessage message; + if (LogQueue.TryDequeue(out message)) + File.AppendAllText(TempFile, message.ToString() + Environment.NewLine); + } } } else { - if (LogQueue.IsEmpty) - return; - - LogMessage message; - if (LogQueue.TryDequeue(out message)) - File.AppendAllText(TempFile, message.ToString()); + while (!LogQueue.IsEmpty) + { + LogMessage message; + if (LogQueue.TryDequeue(out message)) + File.AppendAllText(TempFile, message.ToString() + Environment.NewLine); + } } } @@ -96,17 +110,14 @@ namespace SweetLib.Utils.Logger.Memory public void Forget(LogMessage message) { - lock (this) + var lines = File.ReadAllLines(TempFile).ToList(); + foreach (var line in lines) { - var lines = File.ReadAllLines(TempFile).ToList(); - foreach (var line in lines) - { - if (line == message.ToString()) - lines.Remove(line); - } - - File.WriteAllLines(TempFile, lines); + if (line == message.ToString()) + lines.Remove(line); } + + File.WriteAllLines(TempFile, lines); } public void Archive(string fullFileName = null)