Refactor the ASP Logger to take advantage of new .net6 features

This commit is contained in:
Daan Boerlage 2021-11-07 00:15:15 +01:00
parent e01a066920
commit 47299dd1de
Signed by: daan
GPG key ID: FCE070E1E4956606
2 changed files with 77 additions and 82 deletions

View file

@ -1,28 +1,26 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using Geekbot.Core.Logger; using Geekbot.Core.Logger;
using Microsoft.Extensions.Logging;
namespace Geekbot.Web.Logging namespace Geekbot.Web.Logging;
public class AspLogProvider : ILoggerProvider
{ {
public class AspLogProvider : ILoggerProvider private readonly IGeekbotLogger _geekbotLogger;
private readonly ConcurrentDictionary<string, AspLogger> _loggers = new ConcurrentDictionary<string, AspLogger>();
public AspLogProvider(IGeekbotLogger geekbotLogger)
{ {
private readonly IGeekbotLogger _geekbotLogger; _geekbotLogger = geekbotLogger;
}
private readonly ConcurrentDictionary<string, AspLogger> _loggers = new ConcurrentDictionary<string, AspLogger>(); public void Dispose()
{
_loggers.Clear();
}
public AspLogProvider(IGeekbotLogger geekbotLogger) public ILogger CreateLogger(string categoryName)
{ {
_geekbotLogger = geekbotLogger; return _loggers.GetOrAdd(categoryName, name => new AspLogger(categoryName, _geekbotLogger));
}
public void Dispose()
{
_loggers.Clear();
}
public ILogger CreateLogger(string categoryName)
{
return _loggers.GetOrAdd(categoryName, name => new AspLogger(categoryName, _geekbotLogger));
}
} }
} }

View file

@ -1,71 +1,68 @@
using System; using Geekbot.Core.Logger;
using Geekbot.Core.Logger;
using Microsoft.Extensions.Logging;
namespace Geekbot.Web.Logging namespace Geekbot.Web.Logging;
public class AspLogger : ILogger
{ {
public class AspLogger : ILogger private readonly string _categoryName;
private readonly IGeekbotLogger _geekbotLogger;
public AspLogger(string categoryName, IGeekbotLogger geekbotLogger)
{ {
private readonly string _categoryName; geekbotLogger.Trace(LogSource.Api, $"Adding {categoryName}");
private readonly IGeekbotLogger _geekbotLogger; _categoryName = categoryName;
_geekbotLogger = geekbotLogger;
}
public AspLogger(string categoryName, IGeekbotLogger geekbotLogger) public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
switch (logLevel)
{ {
geekbotLogger.Trace(LogSource.Api, $"Adding {categoryName}"); case LogLevel.Trace:
_categoryName = categoryName; _geekbotLogger.Trace(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
_geekbotLogger = geekbotLogger; break;
} case LogLevel.Debug:
_geekbotLogger.Debug(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) break;
{ case LogLevel.Information:
switch (logLevel) _geekbotLogger.Information(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
{ break;
case LogLevel.Trace: case LogLevel.Warning:
_geekbotLogger.Trace(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}"); _geekbotLogger.Warning(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception);
break; break;
case LogLevel.Debug: case LogLevel.Error:
_geekbotLogger.Debug(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}"); case LogLevel.Critical:
break; _geekbotLogger.Error(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception);
case LogLevel.Information: break;
_geekbotLogger.Information(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}"); case LogLevel.None:
break; break;
case LogLevel.Warning: default:
_geekbotLogger.Warning(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception); throw new ArgumentOutOfRangeException(nameof(logLevel));
break;
case LogLevel.Error:
case LogLevel.Critical:
_geekbotLogger.Error(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception);
break;
case LogLevel.None:
break;
default:
throw new ArgumentOutOfRangeException(nameof(logLevel));
}
}
public bool IsEnabled(LogLevel logLevel)
{
return !_geekbotLogger.LogAsJson() && _geekbotLogger.GetNLogger().IsEnabled(ToGeekbotLogLevel(logLevel));
}
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
private static NLog.LogLevel ToGeekbotLogLevel(LogLevel level)
{
return level switch
{
LogLevel.Trace => NLog.LogLevel.Trace,
LogLevel.Debug => NLog.LogLevel.Debug,
LogLevel.Information => NLog.LogLevel.Info,
LogLevel.Warning => NLog.LogLevel.Warn,
LogLevel.Error => NLog.LogLevel.Error,
LogLevel.Critical => NLog.LogLevel.Fatal,
LogLevel.None => NLog.LogLevel.Off,
_ => throw new ArgumentOutOfRangeException(nameof(level))
};
} }
} }
public bool IsEnabled(LogLevel logLevel)
{
return !_geekbotLogger.LogAsJson() && _geekbotLogger.GetNLogger().IsEnabled(ToGeekbotLogLevel(logLevel));
}
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
private static NLog.LogLevel ToGeekbotLogLevel(LogLevel level)
{
return level switch
{
LogLevel.Trace => NLog.LogLevel.Trace,
LogLevel.Debug => NLog.LogLevel.Debug,
LogLevel.Information => NLog.LogLevel.Info,
LogLevel.Warning => NLog.LogLevel.Warn,
LogLevel.Error => NLog.LogLevel.Error,
LogLevel.Critical => NLog.LogLevel.Fatal,
LogLevel.None => NLog.LogLevel.Off,
_ => throw new ArgumentOutOfRangeException(nameof(level))
};
}
} }