From 393cbbeb23170016d6e1256be0fcafc92986717e Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sat, 14 Oct 2017 15:52:25 -0400 Subject: [PATCH] Fixed spelling of LogAdapter, added docstrings --- .../Extensions.cs | 11 ++++- .../LogAdapter.cs | 44 +++++++++++++++++++ .../LogAdaptor.cs | 29 ------------ 3 files changed, 54 insertions(+), 30 deletions(-) create mode 100644 src/Discord.Net.MicrosoftLogging/LogAdapter.cs delete mode 100644 src/Discord.Net.MicrosoftLogging/LogAdaptor.cs diff --git a/src/Discord.Net.MicrosoftLogging/Extensions.cs b/src/Discord.Net.MicrosoftLogging/Extensions.cs index c654e78b2..1232a8a7f 100644 --- a/src/Discord.Net.MicrosoftLogging/Extensions.cs +++ b/src/Discord.Net.MicrosoftLogging/Extensions.cs @@ -5,9 +5,18 @@ namespace Discord.MicrosoftLogging { public static class Extensions { + /// + /// Configure this Discord client with support for Microsoft's logging abstractions + /// + /// The Discord client to hook into + /// A logger created for logging to + /// + /// A custom message formatter, should the default one not suffice. + /// + /// See for more information. public static void UseMicrosoftLogging(this IDiscordClient client, ILogger logger, Func formatter = null) { - var adaptor = new LogAdaptor(logger, formatter); + var adaptor = new LogAdapter(logger, formatter); client.Log += adaptor.Log; } } diff --git a/src/Discord.Net.MicrosoftLogging/LogAdapter.cs b/src/Discord.Net.MicrosoftLogging/LogAdapter.cs new file mode 100644 index 000000000..18cf75aaa --- /dev/null +++ b/src/Discord.Net.MicrosoftLogging/LogAdapter.cs @@ -0,0 +1,44 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; + +namespace Discord.MicrosoftLogging +{ + public class LogAdapter + { + private readonly ILogger _logger; + private readonly Func _formatter; + + /// + /// Creates a LogAdapter to be used with a Discord client + /// + /// The logger implementation that messages will be written to + /// + /// A custom message formatter, should the default be inadequate. + /// + /// The default message formatter simply returns , which + /// in most cases should not be a problem. + /// + public LogAdapter(ILogger logger, Func formatter = null) + { + _logger = logger; + _formatter = formatter ?? DefaultFormatter; + } + + /// + /// Convert a Discord.Net log event to an abstract log event + /// + /// The log event to be converted + /// A task for compatibility with Discord.Net's async events + public Task Log(LogMessage message) + { + _logger.Log(GetLogLevel(message.Severity), default(EventId), message, message.Exception, _formatter); + return Task.Delay(0); + } + + private string DefaultFormatter(LogMessage message, Exception _) + => message.ToString(); + private static LogLevel GetLogLevel(LogSeverity severity) + => (LogLevel)(Math.Abs((int)severity - 5)); + } +} diff --git a/src/Discord.Net.MicrosoftLogging/LogAdaptor.cs b/src/Discord.Net.MicrosoftLogging/LogAdaptor.cs deleted file mode 100644 index e0dc02044..000000000 --- a/src/Discord.Net.MicrosoftLogging/LogAdaptor.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; - -namespace Discord.MicrosoftLogging -{ - public class LogAdaptor - { - private readonly ILogger _logger; - private readonly Func _formatter; - - public LogAdaptor(ILogger logger, Func formatter = null) - { - _logger = logger; - _formatter = formatter ?? DefaultFormatter; - } - - public Task Log(LogMessage message) - { - _logger.Log(GetLogLevel(message.Severity), default(EventId), message, message.Exception, _formatter); - return Task.Delay(0); - } - - private string DefaultFormatter(LogMessage message, Exception _) - => message.ToString(); - private static LogLevel GetLogLevel(LogSeverity severity) - => (LogLevel)(Math.Abs((int)severity - 5)); - } -}