Skip to content

Commit

Permalink
Added additional Logger setup methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JPVenson committed Jun 15, 2022
1 parent 095ed03 commit 7ba0984
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 97 deletions.
116 changes: 19 additions & 97 deletions Morestachio.Extensions.Logging/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Extensions.Logging;
using MsILogger = Microsoft.Extensions.Logging.ILogger;
using MoresILogger = Morestachio.Helper.Logging.ILogger;
using ILogger = Morestachio.Helper.Logging.ILogger;

namespace Morestachio.Extensions.Logging
{
Expand All @@ -11,6 +10,9 @@ namespace Morestachio.Extensions.Logging
/// </summary>
public static class LoggerExtensions
{
/// <summary>
/// Adds an Adapter for <see cref="MsILogger"/> to <see cref="ILogger"/>
/// </summary>
public static IParserOptionsBuilder WithMicrosoftLogger(this IParserOptionsBuilder builder,
MsILogger logger,
int eventId,
Expand All @@ -19,106 +21,26 @@ public static IParserOptionsBuilder WithMicrosoftLogger(this IParserOptionsBuild
{
return builder.WithLogger(() => new MicrosoftLoggerAdapter(logger, eventId, defaultLogLevel, format));
}
}

public class MicrosoftLoggerAdapter : MoresILogger
{
private readonly ILogger _logger;
private readonly int _eventId;
private readonly LogLevel _defaultLogLevel;
private readonly Func<LogEntry, string> _format;

public MicrosoftLoggerAdapter(
ILogger logger,
int eventId,
LogLevel defaultLogLevel,
Func<LogEntry, string> format
)
{
_logger = logger;
_eventId = eventId;
_defaultLogLevel = defaultLogLevel;
_format = format;
Enabled = true;
}

/// <inheritdoc />
public bool Enabled { get; set; }

public class LogEntry
{
public string Message { get; }
public IDictionary<string, object> Data { get; }

public LogEntry(string message, IDictionary<string, object> data)
{
Message = message;
Data = data;
}
}

/// <inheritdoc />
public void Log(
string logLevel,
string eventId,
string message,
IDictionary<string, object> data
)
{
if (!Enabled)
{
return;
}

_logger.Log(ParseLogLevel(logLevel), new EventId(_eventId, eventId), new LogEntry(message, data), null, FormatMessage);
}

/// <inheritdoc />
public void Log(string logLevel, string eventId, string message)
{
if (!Enabled)
{
return;
}

_logger.Log(ParseLogLevel(logLevel), new EventId(_eventId, eventId), new LogEntry(message, null), null, FormatMessage);
}

private string FormatMessage(LogEntry entry, Exception? arg2)
/// <summary>
/// Adds an Adapter for <see cref="MsILogger"/> to <see cref="ILogger"/>
/// </summary>
public static IParserOptionsBuilder WithMicrosoftLogger(this IParserOptionsBuilder builder,
MsILogger logger,
int eventId,
LogLevel defaultLogLevel)
{
if (_format != null)
{
return _format(entry);
}

if (entry.Data?.Count is null or 0)
{
return entry.Message;
}

var sb = new StringBuilder(entry.Message);
sb.AppendLine();

foreach (var data in entry.Data)
{
sb.AppendLine($"{data.Key} - {(data.Value?.ToString() ?? "{NULL}")}");
}

return sb.ToString();
return builder.WithMicrosoftLogger(logger, eventId, defaultLogLevel, null);
}

private LogLevel ParseLogLevel(string logLevel)
/// <summary>
/// Adds an Adapter for <see cref="MsILogger"/> to <see cref="ILogger"/>
/// </summary>
public static IParserOptionsBuilder WithMicrosoftLogger(this IParserOptionsBuilder builder,
MsILogger logger,
int eventId)
{
return logLevel.ToUpper() switch
{
"Warning" => LogLevel.Warning,
"Critical" => LogLevel.Critical,
"Debug" => LogLevel.Debug,
"Error" => LogLevel.Error,
"Info" => LogLevel.Information,
"Trace" => LogLevel.Trace,
_ => _defaultLogLevel
};
return builder.WithMicrosoftLogger(logger, eventId, LogLevel.Debug);
}
}
}
109 changes: 109 additions & 0 deletions Morestachio.Extensions.Logging/MicrosoftLoggerAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System.Text;
using Microsoft.Extensions.Logging;
using ILogger = Morestachio.Helper.Logging.ILogger;

namespace Morestachio.Extensions.Logging;

public class MicrosoftLoggerAdapter : ILogger
{
private readonly Microsoft.Extensions.Logging.ILogger _logger;
private readonly int _eventId;
private readonly LogLevel _defaultLogLevel;
private readonly Func<LogEntry, string> _format;

public MicrosoftLoggerAdapter(
Microsoft.Extensions.Logging.ILogger logger,
int eventId,
LogLevel defaultLogLevel,
Func<LogEntry, string> format
)
{
_logger = logger;
_eventId = eventId;
_defaultLogLevel = defaultLogLevel;
_format = format;
Enabled = true;
}

/// <inheritdoc />
public bool Enabled { get; set; }

/// <summary>
/// Defines an log entry
/// </summary>
public class LogEntry
{
public string Message { get; }
public IDictionary<string, object> Data { get; }

public LogEntry(string message, IDictionary<string, object> data)
{
Message = message;
Data = data;
}
}

/// <inheritdoc />
public void Log(
string logLevel,
string eventId,
string message,
IDictionary<string, object> data
)
{
if (!Enabled)
{
return;
}

_logger.Log(ParseLogLevel(logLevel), new EventId(_eventId, eventId), new LogEntry(message, data), null, FormatMessage);
}

/// <inheritdoc />
public void Log(string logLevel, string eventId, string message)
{
if (!Enabled)
{
return;
}

_logger.Log(ParseLogLevel(logLevel), new EventId(_eventId, eventId), new LogEntry(message, null), null, FormatMessage);
}

private string FormatMessage(LogEntry entry, Exception? arg2)
{
if (_format != null)
{
return _format(entry);
}

if (entry.Data?.Count is null or 0)
{
return entry.Message;
}

var sb = new StringBuilder(entry.Message);
sb.AppendLine();

foreach (var data in entry.Data)
{
sb.AppendLine($"{data.Key} - {(data.Value?.ToString() ?? "{NULL}")}");
}

return sb.ToString();
}

private LogLevel ParseLogLevel(string logLevel)
{
return logLevel.ToUpper() switch
{
"Warning" => LogLevel.Warning,
"Critical" => LogLevel.Critical,
"Debug" => LogLevel.Debug,
"Error" => LogLevel.Error,
"Info" => LogLevel.Information,
"Trace" => LogLevel.Trace,
_ => _defaultLogLevel
};
}
}

0 comments on commit 7ba0984

Please sign in to comment.