From 346a5e4bbe5b7d530bb07dd7db48cc0e8faf1126 Mon Sep 17 00:00:00 2001 From: Waterball Date: Mon, 1 Feb 2021 18:04:14 +0000 Subject: [PATCH] Fix logging in commands --- .../Builders/ModuleClassBuilder.cs | 5 ++-- src/Discord.Net.Commands/CommandService.cs | 24 ++++++++++--------- src/Discord.Net.Commands/Info/CommandInfo.cs | 7 +++--- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs index 28037b0fa..b6a254fec 100644 --- a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs +++ b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs @@ -5,6 +5,7 @@ using System.Reflection; using System.Threading.Tasks; using Discord.Commands.Builders; +using Microsoft.Extensions.Logging; namespace Discord.Commands { @@ -34,7 +35,7 @@ namespace Discord.Commands } else if (IsLoadableModule(typeInfo)) { - await service._cmdLogger.WarningAsync($"Class {typeInfo.FullName} is not public and cannot be loaded. To suppress this message, mark the class with {nameof(DontAutoLoadAttribute)}.").ConfigureAwait(false); + service._cmdLogger.LogWarning($"Class {typeInfo.FullName} is not public and cannot be loaded. To suppress this message, mark the class with {nameof(DontAutoLoadAttribute)}."); } } @@ -69,7 +70,7 @@ namespace Discord.Commands result[typeInfo.AsType()] = module.Build(service, services); } - await service._cmdLogger.DebugAsync($"Successfully built {builtTypes.Count} modules.").ConfigureAwait(false); + service._cmdLogger.LogDebug($"Successfully built {builtTypes.Count} modules."); return result; } diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index 1d4b0e15a..7f3c10c76 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Threading.Tasks; using Discord.Commands.Builders; using Discord.Logging; +using Microsoft.Extensions.Logging; namespace Discord.Commands { @@ -29,12 +30,6 @@ namespace Discord.Commands /// public class CommandService : IDisposable { - /// - /// Occurs when a command-related information is received. - /// - public event Func Log { add { _logEvent.Add(value); } remove { _logEvent.Remove(value); } } - internal readonly AsyncEvent> _logEvent = new AsyncEvent>(); - /// /// Occurs when a command is executed. /// @@ -56,8 +51,8 @@ namespace Discord.Commands internal readonly bool _caseSensitive, _throwOnError, _ignoreExtraArgs; internal readonly char _separatorChar; internal readonly RunMode _defaultRunMode; - internal readonly Logger _cmdLogger; - internal readonly LogManager _logManager; + internal readonly ILogger _cmdLogger; + internal readonly ILoggerFactory _logManager; internal readonly IReadOnlyDictionary _quotationMarkAliasMap; internal bool _isDisposed; @@ -100,8 +95,14 @@ namespace Discord.Commands if (_defaultRunMode == RunMode.Default) throw new InvalidOperationException("The default run mode cannot be set to Default."); - _logManager = new LogManager(config.LogLevel); - _logManager.Message += async msg => await _logEvent.InvokeAsync(msg).ConfigureAwait(false); + _logManager = config.LoggerFactory; + + if (_logManager == null) + { + _logManager = new DefaultLoggerFactory(); + _logManager.AddProvider(new DefaultLoggerProvider()); + } + _cmdLogger = _logManager.CreateLogger("Command"); _moduleLock = new SemaphoreSlim(1, 1); @@ -349,7 +350,8 @@ namespace Discord.Commands public void AddTypeReader(Type type, TypeReader reader) { if (_defaultTypeReaders.ContainsKey(type)) - _ = _cmdLogger.WarningAsync($"The default TypeReader for {type.FullName} was replaced by {reader.GetType().FullName}." + + _cmdLogger.LogWarning( + $"The default TypeReader for {type.FullName} was replaced by {reader.GetType().FullName}." + "To suppress this message, use AddTypeReader(reader, true)."); AddTypeReader(type, reader, true); } diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index 3bcef9831..890b956ba 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -1,4 +1,5 @@ using Discord.Commands.Builders; +using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Collections.Immutable; @@ -240,7 +241,7 @@ namespace Discord.Commands private async Task ExecuteInternalAsync(ICommandContext context, object[] args, IServiceProvider services) { - await Module.Service._cmdLogger.DebugAsync($"Executing {GetLogText(context)}").ConfigureAwait(false); + Module.Service._cmdLogger.LogDebug($"Executing {GetLogText(context)}"); try { var task = _action(context, args, services, this); @@ -274,7 +275,7 @@ namespace Discord.Commands ex = ex.InnerException; var wrappedEx = new CommandException(this, context, ex); - await Module.Service._cmdLogger.ErrorAsync(wrappedEx).ConfigureAwait(false); + Module.Service._cmdLogger.LogError(wrappedEx, wrappedEx.Message); var result = ExecuteResult.FromError(ex); await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false); @@ -291,7 +292,7 @@ namespace Discord.Commands } finally { - await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false); + Module.Service._cmdLogger.LogTrace($"Executed {GetLogText(context)}"); } }