Refactor the ASP Logger to take advantage of new .net6 features
This commit is contained in:
parent
e01a066920
commit
47299dd1de
2 changed files with 77 additions and 82 deletions
|
@ -1,28 +1,26 @@
|
|||
using System.Collections.Concurrent;
|
||||
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)
|
||||
{
|
||||
_geekbotLogger = geekbotLogger;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_loggers.Clear();
|
||||
}
|
||||
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
{
|
||||
return _loggers.GetOrAdd(categoryName, name => new AspLogger(categoryName, _geekbotLogger));
|
||||
}
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
{
|
||||
return _loggers.GetOrAdd(categoryName, name => new AspLogger(categoryName, _geekbotLogger));
|
||||
}
|
||||
}
|
|
@ -1,71 +1,68 @@
|
|||
using System;
|
||||
using Geekbot.Core.Logger;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Geekbot.Core.Logger;
|
||||
|
||||
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;
|
||||
private readonly IGeekbotLogger _geekbotLogger;
|
||||
geekbotLogger.Trace(LogSource.Api, $"Adding {categoryName}");
|
||||
_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}");
|
||||
_categoryName = categoryName;
|
||||
_geekbotLogger = geekbotLogger;
|
||||
}
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
switch (logLevel)
|
||||
{
|
||||
case LogLevel.Trace:
|
||||
_geekbotLogger.Trace(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Debug:
|
||||
_geekbotLogger.Debug(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Information:
|
||||
_geekbotLogger.Information(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Warning:
|
||||
_geekbotLogger.Warning(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception);
|
||||
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))
|
||||
};
|
||||
case LogLevel.Trace:
|
||||
_geekbotLogger.Trace(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Debug:
|
||||
_geekbotLogger.Debug(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Information:
|
||||
_geekbotLogger.Information(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Warning:
|
||||
_geekbotLogger.Warning(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception);
|
||||
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))
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue