Skip to content

Commit

Permalink
Library - Allow passing a date time format info for console and debug…
Browse files Browse the repository at this point in the history
… logger

Fixes #348
  • Loading branch information
Liryna committed Dec 30, 2023
1 parent 3eea673 commit 21f2e5b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
8 changes: 6 additions & 2 deletions DokanNet/Logging/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Threading.Tasks;

namespace DokanNet.Logging
Expand All @@ -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<Tuple<String, ConsoleColor>> _PendingLogs
= new System.Collections.Concurrent.BlockingCollection<Tuple<String, ConsoleColor>>();

Expand All @@ -19,9 +21,11 @@ private readonly System.Collections.Concurrent.BlockingCollection<Tuple<String,
/// Initializes a new instance of the <see cref="ConsoleLogger"/> class.
/// </summary>
/// <param name="loggerName">Optional name to be added to each log line.</param>
public ConsoleLogger(string loggerName = "")
/// <param name="dateTimeFormatInfo">An object that supplies format information for DateTime.</param>
public ConsoleLogger(string loggerName = "", DateTimeFormatInfo dateTimeFormatInfo = null)
{
_loggerName = loggerName;
_dateTimeFormatInfo = dateTimeFormatInfo;
_WriterTask = Task.Factory.StartNew(() =>
{
foreach (var tuple in _PendingLogs.GetConsumingEnumerable())
Expand Down Expand Up @@ -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;
}
}
Expand Down
10 changes: 7 additions & 3 deletions DokanNet/Logging/DebugViewLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Globalization;
using System.Runtime.InteropServices;

namespace DokanNet.Logging
{
Expand All @@ -12,14 +13,17 @@ namespace DokanNet.Logging
public class DebugViewLogger : ILogger
{
private readonly string _loggerName;
private readonly DateTimeFormatInfo _dateTimeFormatInfo;

/// <summary>
/// Initializes a new instance of the <see cref="DebugViewLogger"/> class.
/// </summary>
/// <param name="loggerName">Optional name to be added to each log line.</param>
public DebugViewLogger(string loggerName = "")
/// <param name="dateTimeFormatInfo">An object that supplies format information for DateTime.</param>
public DebugViewLogger(string loggerName = "", DateTimeFormatInfo dateTimeFormatInfo = null)
{
_loggerName = loggerName;
_dateTimeFormatInfo = dateTimeFormatInfo;
}

/// <inheritdoc />
Expand Down Expand Up @@ -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));
}
}
}
12 changes: 8 additions & 4 deletions DokanNet/Logging/LoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ public static class LoggerExtensions
/// <param name="message">Message to format.</param>
/// <param name="category">Optional category to add to the log message.</param>
/// <param name="loggerName">Optional log name to at to the log message.</param>
/// <param name="dateTimeFormatInfo">An object that supplies format information for DateTime.</param>
/// <returns>A formated log message.</returns>
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);
}

/// <summary>
Expand All @@ -31,17 +33,19 @@ public static string FormatMessageForLogging(
/// <param name="addDateTime">If date and time shout be added to the log message.</param>
/// <param name="category">Optional category to add to the log message.</param>
/// <param name="loggerName">Optional log name to at to the log message.</param>
/// <param name="dateTimeFormatInfo">An object that supplies format information for DateTime.</param>
/// <returns>A formated log message.</returns>
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))
Expand Down

0 comments on commit 21f2e5b

Please sign in to comment.