-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. required change: Use fully qualified naming instead of aliases for ambiguities. |
||
| #endif | ||
|
|
||
| namespace DiscordRPC | ||
| { | ||
|
|
@@ -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> | ||
|
|
@@ -249,7 +262,7 @@ public DiscordRpcClient(string applicationID, int pipe = -1, ILogger logger = nu | |
| }; | ||
| } | ||
|
|
||
| #endregion | ||
| #endregion | ||
|
|
||
| #region Message Handling | ||
| /// <summary> | ||
|
|
||
| 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; | ||
|
Comment on lines
+4
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. required change: Use fully qualified naming instead of aliases for ambiguities. |
||
|
|
||
| namespace Hi3Helper.SharpDiscordRPC.DiscordRPC.Logging | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. required change: must be the same namespace as the rest of the project. |
||
| { | ||
| /// <summary> | ||
| /// Wraps a <see cref="ILogger"/> to a <see cref="ILoggerRpc"/> | ||
| /// </summary> | ||
| public class MsILoggerWrapper : ILoggerRpc | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit-pick: Rename to MsLogger |
||
| { | ||
| 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 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
required change: Use the same namespace from the project