From ead2c9c3f1a9e45a373243d677e747ffc6589427 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Mon, 20 Nov 2023 18:19:31 -0600 Subject: [PATCH] Implement ILogger abstraction for Playnite --- .../LANCommander.PlaynitePlugin.csproj | 4 ++ .../LANCommanderLibraryPlugin.cs | 2 +- .../PlayniteLogger.cs | 55 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 LANCommander.Playnite.Extension/PlayniteLogger.cs diff --git a/LANCommander.Playnite.Extension/LANCommander.PlaynitePlugin.csproj b/LANCommander.Playnite.Extension/LANCommander.PlaynitePlugin.csproj index 2f9178b..4c55555 100644 --- a/LANCommander.Playnite.Extension/LANCommander.PlaynitePlugin.csproj +++ b/LANCommander.Playnite.Extension/LANCommander.PlaynitePlugin.csproj @@ -41,6 +41,9 @@ ..\packages\Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll + + ..\packages\Microsoft.Extensions.Logging.Abstractions.7.0.0\lib\net462\Microsoft.Extensions.Logging.Abstractions.dll + ..\packages\PlayniteSDK.6.10.0\lib\net462\Playnite.SDK.dll @@ -104,6 +107,7 @@ + diff --git a/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs b/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs index 5c12d2c..99206df 100644 --- a/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs +++ b/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs @@ -39,7 +39,7 @@ namespace LANCommander.PlaynitePlugin Settings = new LANCommanderSettingsViewModel(this); - LANCommanderClient = new SDK.Client(Settings.ServerAddress); + LANCommanderClient = new SDK.Client(Settings.ServerAddress, new PlayniteLogger(Logger)); LANCommanderClient.UseToken(new SDK.Models.AuthToken() { AccessToken = Settings.AccessToken, diff --git a/LANCommander.Playnite.Extension/PlayniteLogger.cs b/LANCommander.Playnite.Extension/PlayniteLogger.cs new file mode 100644 index 0000000..a570720 --- /dev/null +++ b/LANCommander.Playnite.Extension/PlayniteLogger.cs @@ -0,0 +1,55 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LANCommander.PlaynitePlugin +{ + public sealed class PlayniteLogger : ILogger + { + private readonly Playnite.SDK.ILogger Logger; + + public PlayniteLogger(Playnite.SDK.ILogger logger) { + Logger = logger; + } + + public IDisposable BeginScope(TState state) + { + return default; + } + + public bool IsEnabled(LogLevel logLevel) + { + return true; + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + { + switch (logLevel) + { + case LogLevel.Trace: + Logger?.Trace(formatter.Invoke(state, exception)); + break; + + case LogLevel.Debug: + Logger?.Debug(formatter.Invoke(state, exception)); + break; + + case LogLevel.Information: + Logger.Info(formatter.Invoke(state, exception)); + break; + + case LogLevel.Warning: + Logger.Warn(formatter.Invoke(state, exception)); + break; + + case LogLevel.Error: + case LogLevel.Critical: + Logger.Error(formatter.Invoke(state, exception)); + break; + } + } + } +}