Skip to content

Implement Microsoft.Extensions.Logger Wrapper #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DiscordRPC/DiscordRPC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
<NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net45'">
<DefineConstants>DISABLE_MSLOGGEREXTENSION</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
</ItemGroup>
</Project>
15 changes: 14 additions & 1 deletion DiscordRPC/DiscordRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
using DiscordRPC.RPC;
using DiscordRPC.RPC.Commands;
using System;
#if !DISABLE_MSLOGGEREXTENSION
using Hi3Helper.SharpDiscordRPC.DiscordRPC.Logging;
using ILoggerMs = Microsoft.Extensions.Logging.ILogger;
#endif

namespace DiscordRPC
{
Expand Down Expand Up @@ -206,6 +210,15 @@ public bool ShutdownOnly
/// <param name="applicationID">The ID of the application created at discord's developers portal.</param>
public DiscordRpcClient(string applicationID) : this(applicationID, -1) { }

#if !DISABLE_MSLOGGEREXTENSION
/// <summary>
/// Creates a new Discord RPC Client which can be used to send Rich Presence and receive Join / Spectate events.
/// </summary>
/// <param name="applicationID">The ID of the application created at discord's developers portal.</param>
/// <param name="logger">The logger used to report messages. Uses Microsoft's implementation of logging extension</param>
public DiscordRpcClient(string applicationID, ILoggerMs logger) : this(applicationID, -1, new MsILoggerWrapper(logger)) { }
#endif

/// <summary>
/// Creates a new Discord RPC Client which can be used to send Rich Presence and receive Join / Spectate events. This constructor exposes more advance features such as custom NamedPipeClients and Loggers.
/// </summary>
Expand Down Expand Up @@ -249,7 +262,7 @@ public DiscordRpcClient(string applicationID, int pipe = -1, ILogger logger = nu
};
}

#endregion
#endregion

#region Message Handling
/// <summary>
Expand Down
89 changes: 89 additions & 0 deletions DiscordRPC/Logging/MsILoggerWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#if !DISABLE_MSLOGGEREXTENSION
using System;
using Microsoft.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger;
using ILoggerRpc = DiscordRPC.Logging.ILogger;
using LogLevel = DiscordRPC.Logging.LogLevel;

namespace Hi3Helper.SharpDiscordRPC.DiscordRPC.Logging
{
/// <summary>
/// Wraps a <see cref="ILogger"/> to a <see cref="ILoggerRpc"/>
/// </summary>
public class MsILoggerWrapper : ILoggerRpc
{
private readonly ILogger _logger;

/// <summary>
/// Creates a new instance of the MsILoggerWrapper
/// <inheritdoc cref="MsILoggerWrapper"/>
/// </summary>
/// <param name="logger">Logger interface to be wrapped</param>
public MsILoggerWrapper(ILogger logger)
{
_logger = logger;
}

private static string FormatMessage(string message, params object[] args)
{
return string.Format(message, args);
}

/// <summary>
/// The level of logging to apply to this logger.
/// </summary>
public LogLevel Level { get; set; }

/// <summary>
/// Formats and writes a trace log message.
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Trace(string message, params object[] args)
{
_logger.LogTrace(FormatMessage(message), args);
}

/// <summary>
/// Informative log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Info(string message, params object[] args)
{
_logger.LogInformation(FormatMessage(message), args);
}

/// <summary>
/// Warning log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Warning(string message, params object[] args)
{
_logger.LogWarning(FormatMessage(message), args);
}

/// <summary>
/// Error log messages
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public void Error(string message, params object[] args)
{
_logger.LogError(FormatMessage(message), args);
}

/// <summary>
/// Error log messages
/// </summary>
/// <param name="ex"></param>
/// <param name="message"></param>
/// <param name="args"></param>
public void Error(Exception ex, string message, params object[] args)
{
_logger.LogError(ex, FormatMessage(message), args);
}
}
}
#endif