From 69992babeb7c1cde7b72c58a4ca0f36010e6e26e Mon Sep 17 00:00:00 2001 From: Sparronator9999 <86388887+Sparronator9999@users.noreply.github.com> Date: Sat, 7 Dec 2024 06:15:15 +1100 Subject: [PATCH] Minor log library optimisation --- YAMDCC.Logs/Logger.cs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/YAMDCC.Logs/Logger.cs b/YAMDCC.Logs/Logger.cs index 1355ed7..ba48160 100644 --- a/YAMDCC.Logs/Logger.cs +++ b/YAMDCC.Logs/Logger.cs @@ -34,11 +34,9 @@ public sealed class Logger : IDisposable /// /// Used with to prevent more - /// than one thread writing to the file at once. + /// than one thread writing to the console at once. /// - private readonly object - fileLock = new(), - consoleLock = new(); + private readonly object consoleLock = new(); /// /// The newline characters to split provided log message lines by. @@ -75,12 +73,12 @@ private static string LogString(string text, LogLevel level, bool showDate) => /// /// Should the log time be shown in console logs? /// - public bool ShowTimeInConsole { get; set; } + public bool LogTimeToConsole { get; set; } /// /// Should the log time be shown in logs written to disk? /// - public bool ShowTimeInFile { get; set; } = true; + public bool LogTimeToFile { get; set; } = true; /// /// Writes a Debug event to the . @@ -263,16 +261,16 @@ public void DeleteArchivedLogs() private void WriteFile(string message, LogLevel level) { - lock (fileLock) + if (LogWriter is null) { - if (LogWriter is null || LogWriter == null || LogWriter.Equals(null)) - { - InitLogFile(); - } + InitLogFile(); + } + lock (LogWriter) + { foreach (string str in message.Split(NewlineChars, StringSplitOptions.RemoveEmptyEntries)) { - LogWriter.WriteLine(LogString(str, level, ShowTimeInFile)); + LogWriter.WriteLine(LogString(str, level, LogTimeToFile)); } } } @@ -288,7 +286,7 @@ private void WriteConsole(string message, LogLevel level) { case LogLevel.Fatal: Console.BackgroundColor = ConsoleColor.Yellow; - Console.ForegroundColor = ConsoleColor.DarkBlue; + Console.ForegroundColor = ConsoleColor.DarkRed; break; case LogLevel.Error: Console.ForegroundColor = ConsoleColor.Red; @@ -306,7 +304,7 @@ private void WriteConsole(string message, LogLevel level) foreach (string str in message.Split(NewlineChars, StringSplitOptions.RemoveEmptyEntries)) { - Console.WriteLine(LogString(str, level, ShowTimeInConsole)); + Console.WriteLine(LogString(str, level, LogTimeToConsole)); } Console.BackgroundColor = bgColour;