Browse Source

Fix logging in commands

pull/1765/head
Waterball 4 years ago
parent
commit
346a5e4bbe
3 changed files with 20 additions and 16 deletions
  1. +3
    -2
      src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
  2. +13
    -11
      src/Discord.Net.Commands/CommandService.cs
  3. +4
    -3
      src/Discord.Net.Commands/Info/CommandInfo.cs

+ 3
- 2
src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs View File

@@ -5,6 +5,7 @@ using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;


using Discord.Commands.Builders; using Discord.Commands.Builders;
using Microsoft.Extensions.Logging;


namespace Discord.Commands namespace Discord.Commands
{ {
@@ -34,7 +35,7 @@ namespace Discord.Commands
} }
else if (IsLoadableModule(typeInfo)) 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); 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; return result;
} }


+ 13
- 11
src/Discord.Net.Commands/CommandService.cs View File

@@ -8,6 +8,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Commands.Builders; using Discord.Commands.Builders;
using Discord.Logging; using Discord.Logging;
using Microsoft.Extensions.Logging;


namespace Discord.Commands namespace Discord.Commands
{ {
@@ -29,12 +30,6 @@ namespace Discord.Commands
/// </remarks> /// </remarks>
public class CommandService : IDisposable public class CommandService : IDisposable
{ {
/// <summary>
/// Occurs when a command-related information is received.
/// </summary>
public event Func<LogMessage, Task> Log { add { _logEvent.Add(value); } remove { _logEvent.Remove(value); } }
internal readonly AsyncEvent<Func<LogMessage, Task>> _logEvent = new AsyncEvent<Func<LogMessage, Task>>();

/// <summary> /// <summary>
/// Occurs when a command is executed. /// Occurs when a command is executed.
/// </summary> /// </summary>
@@ -56,8 +51,8 @@ namespace Discord.Commands
internal readonly bool _caseSensitive, _throwOnError, _ignoreExtraArgs; internal readonly bool _caseSensitive, _throwOnError, _ignoreExtraArgs;
internal readonly char _separatorChar; internal readonly char _separatorChar;
internal readonly RunMode _defaultRunMode; internal readonly RunMode _defaultRunMode;
internal readonly Logger _cmdLogger;
internal readonly LogManager _logManager;
internal readonly ILogger _cmdLogger;
internal readonly ILoggerFactory _logManager;
internal readonly IReadOnlyDictionary<char, char> _quotationMarkAliasMap; internal readonly IReadOnlyDictionary<char, char> _quotationMarkAliasMap;


internal bool _isDisposed; internal bool _isDisposed;
@@ -100,8 +95,14 @@ namespace Discord.Commands
if (_defaultRunMode == RunMode.Default) if (_defaultRunMode == RunMode.Default)
throw new InvalidOperationException("The default run mode cannot be set to 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"); _cmdLogger = _logManager.CreateLogger("Command");


_moduleLock = new SemaphoreSlim(1, 1); _moduleLock = new SemaphoreSlim(1, 1);
@@ -349,7 +350,8 @@ namespace Discord.Commands
public void AddTypeReader(Type type, TypeReader reader) public void AddTypeReader(Type type, TypeReader reader)
{ {
if (_defaultTypeReaders.ContainsKey(type)) 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<T>(reader, true)."); "To suppress this message, use AddTypeReader<T>(reader, true).");
AddTypeReader(type, reader, true); AddTypeReader(type, reader, true);
} }


+ 4
- 3
src/Discord.Net.Commands/Info/CommandInfo.cs View File

@@ -1,4 +1,5 @@
using Discord.Commands.Builders; using Discord.Commands.Builders;
using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
@@ -240,7 +241,7 @@ namespace Discord.Commands


private async Task<IResult> ExecuteInternalAsync(ICommandContext context, object[] args, IServiceProvider services) private async Task<IResult> 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 try
{ {
var task = _action(context, args, services, this); var task = _action(context, args, services, this);
@@ -274,7 +275,7 @@ namespace Discord.Commands
ex = ex.InnerException; ex = ex.InnerException;


var wrappedEx = new CommandException(this, context, ex); 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); var result = ExecuteResult.FromError(ex);
await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false); await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);
@@ -291,7 +292,7 @@ namespace Discord.Commands
} }
finally finally
{ {
await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false);
Module.Service._cmdLogger.LogTrace($"Executed {GetLogText(context)}");
} }
} }




Loading…
Cancel
Save