From 8b537f58c056001d1598fdb379c6b7ee96cd60c2 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sat, 14 Oct 2017 15:17:27 -0400 Subject: [PATCH 1/3] Added MicrosoftLogging project + LogAdaptor The Log event needed to be added to IDiscordClient to allow MicrosoftLogging to compile implementation-agnostic. This has no major effect on any of our libraries, and should have minimal effect on any downstream libraries. --- Discord.Net.sln | 17 ++++++++++- src/Discord.Net.Core/IDiscordClient.cs | 2 ++ .../Discord.Net.MicrosoftLogging.csproj | 15 ++++++++++ .../Extensions.cs | 14 +++++++++ .../LogAdaptor.cs | 29 +++++++++++++++++++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/Discord.Net.MicrosoftLogging/Discord.Net.MicrosoftLogging.csproj create mode 100644 src/Discord.Net.MicrosoftLogging/Extensions.cs create mode 100644 src/Discord.Net.MicrosoftLogging/LogAdaptor.cs diff --git a/Discord.Net.sln b/Discord.Net.sln index 58bfcad86..6686004bc 100644 --- a/Discord.Net.sln +++ b/Discord.Net.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26730.12 +VisualStudioVersion = 15.0.26730.3 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Discord.Net.Core", "src\Discord.Net.Core\Discord.Net.Core.csproj", "{91E9E7BD-75C9-4E98-84AA-2C271922E5C2}" EndProject @@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Discord.Net.Tests", "test\D EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Discord.Net.Webhook", "src\Discord.Net.Webhook\Discord.Net.Webhook.csproj", "{9AFAB80E-D2D3-4EDB-B58C-BACA78D1EA30}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Discord.Net.MicrosoftLogging", "src\Discord.Net.MicrosoftLogging\Discord.Net.MicrosoftLogging.csproj", "{3FDEFF51-286A-4D2F-AD73-3679A76F09B5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -130,6 +132,18 @@ Global {9AFAB80E-D2D3-4EDB-B58C-BACA78D1EA30}.Release|x64.Build.0 = Release|Any CPU {9AFAB80E-D2D3-4EDB-B58C-BACA78D1EA30}.Release|x86.ActiveCfg = Release|Any CPU {9AFAB80E-D2D3-4EDB-B58C-BACA78D1EA30}.Release|x86.Build.0 = Release|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Debug|x64.ActiveCfg = Debug|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Debug|x64.Build.0 = Debug|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Debug|x86.ActiveCfg = Debug|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Debug|x86.Build.0 = Debug|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Release|Any CPU.Build.0 = Release|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Release|x64.ActiveCfg = Release|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Release|x64.Build.0 = Release|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Release|x86.ActiveCfg = Release|Any CPU + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -141,6 +155,7 @@ Global {688FD1D8-7F01-4539-B2E9-F473C5D699C7} = {288C363D-A636-4EAE-9AC1-4698B641B26E} {6BDEEC08-417B-459F-9CA3-FF8BAB18CAC7} = {B0657AAE-DCC5-4FBF-8E5D-1FB578CF3012} {9AFAB80E-D2D3-4EDB-B58C-BACA78D1EA30} = {CC3D4B1C-9DE0-448B-8AE7-F3F1F3EC5C3A} + {3FDEFF51-286A-4D2F-AD73-3679A76F09B5} = {CC3D4B1C-9DE0-448B-8AE7-F3F1F3EC5C3A} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {D2404771-EEC8-45F2-9D71-F3373F6C1495} diff --git a/src/Discord.Net.Core/IDiscordClient.cs b/src/Discord.Net.Core/IDiscordClient.cs index 23e8e9c5b..ce888c3b3 100644 --- a/src/Discord.Net.Core/IDiscordClient.cs +++ b/src/Discord.Net.Core/IDiscordClient.cs @@ -7,6 +7,8 @@ namespace Discord { public interface IDiscordClient : IDisposable { + event Func Log; + ConnectionState ConnectionState { get; } ISelfUser CurrentUser { get; } TokenType TokenType { get; } diff --git a/src/Discord.Net.MicrosoftLogging/Discord.Net.MicrosoftLogging.csproj b/src/Discord.Net.MicrosoftLogging/Discord.Net.MicrosoftLogging.csproj new file mode 100644 index 000000000..aa582ae24 --- /dev/null +++ b/src/Discord.Net.MicrosoftLogging/Discord.Net.MicrosoftLogging.csproj @@ -0,0 +1,15 @@ + + + + Discord.Net.MicrosoftLogging + Discord.MicrosoftLogging + A Discord.Net extension adding support for Microsoft's logging abstractions. + netstandard1.1 + + + + + + + + \ No newline at end of file diff --git a/src/Discord.Net.MicrosoftLogging/Extensions.cs b/src/Discord.Net.MicrosoftLogging/Extensions.cs new file mode 100644 index 000000000..c654e78b2 --- /dev/null +++ b/src/Discord.Net.MicrosoftLogging/Extensions.cs @@ -0,0 +1,14 @@ +using System; +using Microsoft.Extensions.Logging; + +namespace Discord.MicrosoftLogging +{ + public static class Extensions + { + public static void UseMicrosoftLogging(this IDiscordClient client, ILogger logger, Func formatter = null) + { + var adaptor = new LogAdaptor(logger, formatter); + client.Log += adaptor.Log; + } + } +} diff --git a/src/Discord.Net.MicrosoftLogging/LogAdaptor.cs b/src/Discord.Net.MicrosoftLogging/LogAdaptor.cs new file mode 100644 index 000000000..e0dc02044 --- /dev/null +++ b/src/Discord.Net.MicrosoftLogging/LogAdaptor.cs @@ -0,0 +1,29 @@ +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)); + } +} From cbcfbdeef8163651214a25a1f403d318453c6999 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sat, 14 Oct 2017 15:24:10 -0400 Subject: [PATCH 2/3] Added MicrosoftLogging project to appveyor config --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index d94e2ad68..54d0866cd 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -28,6 +28,7 @@ after_build: - ps: dotnet pack "src\Discord.Net.WebSocket\Discord.Net.WebSocket.csproj" -c "Release" -o "../../artifacts" --no-build /p:BuildNumber="$Env:BUILD" /p:IsTagBuild="$Env:APPVEYOR_REPO_TAG" - ps: dotnet pack "src\Discord.Net.Rpc\Discord.Net.Rpc.csproj" -c "Release" -o "../../artifacts" --no-build /p:BuildNumber="$Env:BUILD" /p:IsTagBuild="$Env:APPVEYOR_REPO_TAG" - ps: dotnet pack "src\Discord.Net.Commands\Discord.Net.Commands.csproj" -c "Release" -o "../../artifacts" --no-build /p:BuildNumber="$Env:BUILD" /p:IsTagBuild="$Env:APPVEYOR_REPO_TAG" +- ps: dotnet pack "src\Discord.Net.MicrosoftLogging\Discord.Net.MicrosoftLogging.csproj" -c "Release" -o "../../artifacts" --no-build /p:BuildNumber="$Env:BUILD" /p:IsTagBuild="$Env:APPVEYOR_REPO_TAG" - ps: dotnet pack "src\Discord.Net.Webhook\Discord.Net.Webhook.csproj" -c "Release" -o "../../artifacts" --no-build /p:BuildNumber="$Env:BUILD" /p:IsTagBuild="$Env:APPVEYOR_REPO_TAG" - ps: dotnet pack "src\Discord.Net.Providers.WS4Net\Discord.Net.Providers.WS4Net.csproj" -c "Release" -o "../../artifacts" --no-build /p:BuildNumber="$Env:BUILD" /p:IsTagBuild="$Env:APPVEYOR_REPO_TAG" - ps: >- From 393cbbeb23170016d6e1256be0fcafc92986717e Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sat, 14 Oct 2017 15:52:25 -0400 Subject: [PATCH 3/3] 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)); - } -}