@@ -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} | |||
@@ -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: >- | |||
@@ -7,6 +7,8 @@ namespace Discord | |||
{ | |||
public interface IDiscordClient : IDisposable | |||
{ | |||
event Func<LogMessage, Task> Log; | |||
ConnectionState ConnectionState { get; } | |||
ISelfUser CurrentUser { get; } | |||
TokenType TokenType { get; } | |||
@@ -0,0 +1,15 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<Import Project="../../Discord.Net.targets" /> | |||
<PropertyGroup> | |||
<AssemblyName>Discord.Net.MicrosoftLogging</AssemblyName> | |||
<RootNamespace>Discord.MicrosoftLogging</RootNamespace> | |||
<Description>A Discord.Net extension adding support for Microsoft's logging abstractions.</Description> | |||
<TargetFrameworks>netstandard1.1</TargetFrameworks> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.1" /> | |||
</ItemGroup> | |||
</Project> |
@@ -0,0 +1,23 @@ | |||
using System; | |||
using Microsoft.Extensions.Logging; | |||
namespace Discord.MicrosoftLogging | |||
{ | |||
public static class Extensions | |||
{ | |||
/// <summary> | |||
/// Configure this Discord client with support for Microsoft's logging abstractions | |||
/// </summary> | |||
/// <param name="client">The Discord client to hook into</param> | |||
/// <param name="logger">A logger created for logging to</param> | |||
/// <param name="formatter"> | |||
/// A custom message formatter, should the default one not suffice. | |||
/// | |||
/// See <see cref="LogAdapter(ILogger, Func{LogMessage, Exception, string})"/> for more information.</param> | |||
public static void UseMicrosoftLogging(this IDiscordClient client, ILogger logger, Func<LogMessage, Exception, string> formatter = null) | |||
{ | |||
var adaptor = new LogAdapter(logger, formatter); | |||
client.Log += adaptor.Log; | |||
} | |||
} | |||
} |
@@ -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<LogMessage, Exception, string> _formatter; | |||
/// <summary> | |||
/// Creates a LogAdapter to be used with a Discord client | |||
/// </summary> | |||
/// <param name="logger">The logger implementation that messages will be written to</param> | |||
/// <param name="formatter"> | |||
/// A custom message formatter, should the default be inadequate. | |||
/// | |||
/// The default message formatter simply returns <see cref="LogMessage.ToString()"/>, which | |||
/// in most cases should not be a problem. | |||
/// </param> | |||
public LogAdapter(ILogger logger, Func<LogMessage, Exception, string> formatter = null) | |||
{ | |||
_logger = logger; | |||
_formatter = formatter ?? DefaultFormatter; | |||
} | |||
/// <summary> | |||
/// Convert a Discord.Net log event to an abstract log event | |||
/// </summary> | |||
/// <param name="message">The log event to be converted</param> | |||
/// <returns>A task for compatibility with Discord.Net's async events</returns> | |||
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)); | |||
} | |||
} |