Move the asp logger to core and and make it a generic ilogger adapter
This commit is contained in:
parent
a3e10b15c1
commit
dc800d144d
6 changed files with 51 additions and 41 deletions
|
@ -8,6 +8,7 @@ using Geekbot.Core.Database;
|
|||
using Geekbot.Core.GlobalSettings;
|
||||
using Geekbot.Core.GuildSettingsManager;
|
||||
using Geekbot.Core.Logger;
|
||||
using Geekbot.Core.Logger.Adapters;
|
||||
using Geekbot.Core.ReactionListener;
|
||||
using Geekbot.Core.UserRepository;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
|
||||
namespace Geekbot.Core.Logger
|
||||
namespace Geekbot.Core.Logger.Adapters
|
||||
{
|
||||
public class DiscordLogger : IDiscordLogger
|
||||
{
|
|
@ -1,16 +1,17 @@
|
|||
using Geekbot.Core.Logger;
|
||||
using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Geekbot.Web.Logging;
|
||||
namespace Geekbot.Core.Logger.Adapters;
|
||||
|
||||
public class AspLogger : ILogger
|
||||
public class ILoggerAdapter : ILogger
|
||||
{
|
||||
private readonly string _categoryName;
|
||||
private readonly LogSource _logSource;
|
||||
private readonly IGeekbotLogger _geekbotLogger;
|
||||
|
||||
public AspLogger(string categoryName, IGeekbotLogger geekbotLogger)
|
||||
public ILoggerAdapter(string categoryName, LogSource logSource, IGeekbotLogger geekbotLogger)
|
||||
{
|
||||
geekbotLogger.Trace(LogSource.Api, $"Adding {categoryName}");
|
||||
_categoryName = categoryName;
|
||||
_logSource = logSource;
|
||||
_geekbotLogger = geekbotLogger;
|
||||
}
|
||||
|
||||
|
@ -19,20 +20,20 @@ public class AspLogger : ILogger
|
|||
switch (logLevel)
|
||||
{
|
||||
case LogLevel.Trace:
|
||||
_geekbotLogger.Trace(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
_geekbotLogger.Trace(_logSource, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Debug:
|
||||
_geekbotLogger.Debug(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
_geekbotLogger.Debug(_logSource, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Information:
|
||||
_geekbotLogger.Information(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
_geekbotLogger.Information(_logSource, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Warning:
|
||||
_geekbotLogger.Warning(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception);
|
||||
_geekbotLogger.Warning(_logSource, $"{eventId.Id} - {_categoryName} - {state}", exception);
|
||||
break;
|
||||
case LogLevel.Error:
|
||||
case LogLevel.Critical:
|
||||
_geekbotLogger.Error(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception);
|
||||
_geekbotLogger.Error(_logSource, $"{eventId.Id} - {_categoryName} - {state}", exception);
|
||||
break;
|
||||
case LogLevel.None:
|
||||
break;
|
||||
|
@ -43,7 +44,8 @@ public class AspLogger : ILogger
|
|||
|
||||
public bool IsEnabled(LogLevel logLevel)
|
||||
{
|
||||
return !_geekbotLogger.LogAsJson() && _geekbotLogger.GetNLogger().IsEnabled(ToGeekbotLogLevel(logLevel));
|
||||
return _geekbotLogger.GetNLogger().IsEnabled(ToGeekbotLogLevel(logLevel));
|
||||
// return !_geekbotLogger.LogAsJson() && _geekbotLogger.GetNLogger().IsEnabled(ToGeekbotLogLevel(logLevel));
|
||||
}
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
33
src/Core/Logger/Adapters/ILoggerProviderProvider.cs
Normal file
33
src/Core/Logger/Adapters/ILoggerProviderProvider.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
using System.Collections.Concurrent;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Geekbot.Core.Logger.Adapters;
|
||||
|
||||
public class ILoggerProviderProvider : ILoggerProvider, ILoggerFactory
|
||||
{
|
||||
private readonly IGeekbotLogger _geekbotLogger;
|
||||
private readonly LogSource _logSource;
|
||||
|
||||
private readonly ConcurrentDictionary<string, ILoggerAdapter> _loggers = new();
|
||||
|
||||
public ILoggerProviderProvider(IGeekbotLogger geekbotLogger, LogSource logSource)
|
||||
{
|
||||
_geekbotLogger = geekbotLogger;
|
||||
_logSource = logSource;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_loggers.Clear();
|
||||
}
|
||||
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
{
|
||||
return _loggers.GetOrAdd(categoryName, name => new ILoggerAdapter(categoryName, _logSource, _geekbotLogger));
|
||||
}
|
||||
|
||||
public void AddProvider(ILoggerProvider provider)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
using System.Collections.Concurrent;
|
||||
using Geekbot.Core.Logger;
|
||||
|
||||
namespace Geekbot.Web.Logging;
|
||||
|
||||
public class AspLogProvider : ILoggerProvider
|
||||
{
|
||||
private readonly IGeekbotLogger _geekbotLogger;
|
||||
|
||||
private readonly ConcurrentDictionary<string, AspLogger> _loggers = new ConcurrentDictionary<string, AspLogger>();
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
|
@ -7,9 +7,9 @@ using Geekbot.Core.GlobalSettings;
|
|||
using Geekbot.Core.GuildSettingsManager;
|
||||
using Geekbot.Core.Highscores;
|
||||
using Geekbot.Core.Logger;
|
||||
using Geekbot.Core.Logger.Adapters;
|
||||
using Geekbot.Core.UserRepository;
|
||||
using Geekbot.Interactions;
|
||||
using Geekbot.Web.Logging;
|
||||
|
||||
namespace Geekbot.Web;
|
||||
|
||||
|
@ -40,7 +40,7 @@ public static class WebApiStartup
|
|||
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.SetMinimumLevel(LogLevel.Debug);
|
||||
builder.Logging.AddProvider(new AspLogProvider(logger));
|
||||
builder.Logging.AddProvider(new ILoggerProviderProvider(logger, LogSource.Api));
|
||||
|
||||
var app = builder.Build();
|
||||
app.UseCors(cors => cors.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().Build());
|
||||
|
|
Loading…
Reference in a new issue