From 21f2e5bb69b721dc54583dba8f1bb98542287f48 Mon Sep 17 00:00:00 2001 From: Liryna Date: Fri, 29 Dec 2023 22:20:32 -0500 Subject: [PATCH] Library - Allow passing a date time format info for console and debug logger Fixes #348 --- DokanNet/Logging/ConsoleLogger.cs | 8 ++++++-- DokanNet/Logging/DebugViewLogger.cs | 10 +++++++--- DokanNet/Logging/LoggerExtensions.cs | 12 ++++++++---- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/DokanNet/Logging/ConsoleLogger.cs b/DokanNet/Logging/ConsoleLogger.cs index 2021de5c..ecec18ca 100644 --- a/DokanNet/Logging/ConsoleLogger.cs +++ b/DokanNet/Logging/ConsoleLogger.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.Threading.Tasks; namespace DokanNet.Logging @@ -9,6 +10,7 @@ namespace DokanNet.Logging public class ConsoleLogger : ILogger, IDisposable { private readonly string _loggerName; + private readonly DateTimeFormatInfo _dateTimeFormatInfo; private readonly System.Collections.Concurrent.BlockingCollection> _PendingLogs = new System.Collections.Concurrent.BlockingCollection>(); @@ -19,9 +21,11 @@ private readonly System.Collections.Concurrent.BlockingCollection class. /// /// Optional name to be added to each log line. - public ConsoleLogger(string loggerName = "") + /// An object that supplies format information for DateTime. + public ConsoleLogger(string loggerName = "", DateTimeFormatInfo dateTimeFormatInfo = null) { _loggerName = loggerName; + _dateTimeFormatInfo = dateTimeFormatInfo; _WriterTask = Task.Factory.StartNew(() => { foreach (var tuple in _PendingLogs.GetConsumingEnumerable()) @@ -78,7 +82,7 @@ private void WriteMessage(string message, ConsoleColor newColor) { var origForegroundColor = Console.ForegroundColor; Console.ForegroundColor = newColor; - Console.WriteLine(message.FormatMessageForLogging(true, loggerName: _loggerName)); + Console.WriteLine(message.FormatMessageForLogging(true, loggerName: _loggerName, dateTimeFormatInfo: _dateTimeFormatInfo)); Console.ForegroundColor = origForegroundColor; } } diff --git a/DokanNet/Logging/DebugViewLogger.cs b/DokanNet/Logging/DebugViewLogger.cs index e2cdd0ab..63d3f280 100644 --- a/DokanNet/Logging/DebugViewLogger.cs +++ b/DokanNet/Logging/DebugViewLogger.cs @@ -1,4 +1,5 @@ -using System.Runtime.InteropServices; +using System.Globalization; +using System.Runtime.InteropServices; namespace DokanNet.Logging { @@ -12,14 +13,17 @@ namespace DokanNet.Logging public class DebugViewLogger : ILogger { private readonly string _loggerName; + private readonly DateTimeFormatInfo _dateTimeFormatInfo; /// /// Initializes a new instance of the class. /// /// Optional name to be added to each log line. - public DebugViewLogger(string loggerName = "") + /// An object that supplies format information for DateTime. + public DebugViewLogger(string loggerName = "", DateTimeFormatInfo dateTimeFormatInfo = null) { _loggerName = loggerName; + _dateTimeFormatInfo = dateTimeFormatInfo; } /// @@ -62,7 +66,7 @@ private void WriteMessageToDebugView(string category, string message, params obj { if (args?.Length > 0) message = string.Format(message, args); - OutputDebugString(message.FormatMessageForLogging(category, _loggerName)); + OutputDebugString(message.FormatMessageForLogging(category, _loggerName, _dateTimeFormatInfo)); } } } \ No newline at end of file diff --git a/DokanNet/Logging/LoggerExtensions.cs b/DokanNet/Logging/LoggerExtensions.cs index e835a954..eb300175 100644 --- a/DokanNet/Logging/LoggerExtensions.cs +++ b/DokanNet/Logging/LoggerExtensions.cs @@ -15,13 +15,15 @@ public static class LoggerExtensions /// Message to format. /// Optional category to add to the log message. /// Optional log name to at to the log message. + /// An object that supplies format information for DateTime. /// A formated log message. public static string FormatMessageForLogging( this string message, string category = null, - string loggerName = "") + string loggerName = "", + DateTimeFormatInfo dateTimeFormatInfo = null) { - return message.FormatMessageForLogging(false, category, loggerName); + return message.FormatMessageForLogging(false, category, loggerName, dateTimeFormatInfo); } /// @@ -31,17 +33,19 @@ public static string FormatMessageForLogging( /// If date and time shout be added to the log message. /// Optional category to add to the log message. /// Optional log name to at to the log message. + /// An object that supplies format information for DateTime. /// A formated log message. public static string FormatMessageForLogging( this string message, bool addDateTime = false, string category = null, - string loggerName = "") + string loggerName = "", + DateTimeFormatInfo dateTimeFormatInfo = null) { var stringBuilder = new StringBuilder(); if (addDateTime) { - stringBuilder.AppendFormat("{0} - ", DateTime.Now.ToString(CultureInfo.InvariantCulture)); + stringBuilder.AppendFormat("{0} - ", DateTime.Now.ToString((IFormatProvider)dateTimeFormatInfo ?? CultureInfo.InvariantCulture)); } if (!string.IsNullOrEmpty(loggerName))