From cbdcf580633afd3ef06085094f8bedb689495465 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Sat, 25 Feb 2023 15:32:26 +0100 Subject: [PATCH] Move logging into nuget package --- .../Configuration/AccountConfigurationV7.cs | 2 - .../Logging/DalamudLogger.cs | 90 ------------------- .../Logging/DalamudLoggerProvider.cs | 31 ------- Pal.Client/DependencyInjectionContext.cs | 6 +- Pal.Client/Pal.Client.csproj | 1 + 5 files changed, 4 insertions(+), 126 deletions(-) delete mode 100644 Pal.Client/DependencyInjection/Logging/DalamudLogger.cs delete mode 100644 Pal.Client/DependencyInjection/Logging/DalamudLoggerProvider.cs diff --git a/Pal.Client/Configuration/AccountConfigurationV7.cs b/Pal.Client/Configuration/AccountConfigurationV7.cs index e02769a..97ea445 100644 --- a/Pal.Client/Configuration/AccountConfigurationV7.cs +++ b/Pal.Client/Configuration/AccountConfigurationV7.cs @@ -2,9 +2,7 @@ using System.Collections.Generic; using System.Security.Cryptography; using System.Text.Json.Serialization; -using Dalamud.Logging; using Microsoft.Extensions.Logging; -using Pal.Client.DependencyInjection.Logging; namespace Pal.Client.Configuration { diff --git a/Pal.Client/DependencyInjection/Logging/DalamudLogger.cs b/Pal.Client/DependencyInjection/Logging/DalamudLogger.cs deleted file mode 100644 index 692c311..0000000 --- a/Pal.Client/DependencyInjection/Logging/DalamudLogger.cs +++ /dev/null @@ -1,90 +0,0 @@ -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Text; -using Serilog.Events; - -namespace Pal.Client.DependencyInjection.Logging -{ - internal sealed class DalamudLogger : ILogger - { - private static readonly string AssemblyName = typeof(Plugin).Assembly.GetName().Name!; - private static readonly Serilog.ILogger PluginLogDelegate = Serilog.Log.ForContext("SourceContext", AssemblyName); - private readonly string _name; - private readonly IExternalScopeProvider? _scopeProvider; - - public DalamudLogger(string name, IExternalScopeProvider? scopeProvider) - { - _name = name; - _scopeProvider = scopeProvider; - } - - public IDisposable BeginScope(TState state) - where TState : notnull - => _scopeProvider?.Push(state) ?? NullScope.Instance; - - public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None && IsEnabled(ToSerilogLevel(logLevel)); - - private bool IsEnabled(LogEventLevel logEventLevel) => PluginLogDelegate.IsEnabled(logEventLevel); - - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, - Func formatter) - { - if (logLevel == LogLevel.None) - return; - - LogEventLevel logEventLevel = ToSerilogLevel(logLevel); - if (!IsEnabled(logEventLevel)) - return; - - if (formatter == null) - throw new ArgumentNullException(nameof(formatter)); - - StringBuilder sb = new StringBuilder(); - sb.Append('[').Append(AssemblyName).Append("] "); - _scopeProvider?.ForEachScope((scope, builder) => - { - if (scope is IEnumerable> properties) - { - foreach (KeyValuePair pair in properties) - { - builder.Append('<').Append(pair.Key).Append('=').Append(pair.Value) - .Append("> "); - } - } - else if (scope != null) - builder.Append('<').Append(scope).Append("> "); - }, - sb); - sb.Append(_name).Append(": ").Append(formatter(state, null)); - PluginLogDelegate.Write(logEventLevel, exception, sb.ToString()); - } - - private LogEventLevel ToSerilogLevel(LogLevel logLevel) - { - return logLevel switch - { - LogLevel.Critical => LogEventLevel.Fatal, - LogLevel.Error => LogEventLevel.Error, - LogLevel.Warning => LogEventLevel.Warning, - LogLevel.Information => LogEventLevel.Information, - LogLevel.Debug => LogEventLevel.Debug, - LogLevel.Trace => LogEventLevel.Verbose, - _ => throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null) - }; - } - - private sealed class NullScope : IDisposable - { - public static NullScope Instance { get; } = new(); - - private NullScope() - { - } - - public void Dispose() - { - } - } - } -} diff --git a/Pal.Client/DependencyInjection/Logging/DalamudLoggerProvider.cs b/Pal.Client/DependencyInjection/Logging/DalamudLoggerProvider.cs deleted file mode 100644 index 287239b..0000000 --- a/Pal.Client/DependencyInjection/Logging/DalamudLoggerProvider.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Microsoft.Extensions.Logging; -using System; - -namespace Pal.Client.DependencyInjection.Logging -{ - internal sealed class DalamudLoggerProvider : ILoggerProvider, ISupportExternalScope - { - private IExternalScopeProvider? _scopeProvider; - - public ILogger CreateLogger(string categoryName) => new DalamudLogger(categoryName, _scopeProvider); - - /// - /// Manual logger creation, doesn't handle scopes. - /// - public ILogger CreateLogger(Type type) => CreateLogger(type.FullName ?? type.ToString()); - - /// - /// Manual logger creation, doesn't handle scopes. - /// - public ILogger CreateLogger() => CreateLogger(typeof(T)); - - public void SetScopeProvider(IExternalScopeProvider scopeProvider) - { - _scopeProvider = scopeProvider; - } - - public void Dispose() - { - } - } -} diff --git a/Pal.Client/DependencyInjectionContext.cs b/Pal.Client/DependencyInjectionContext.cs index 7ae62e4..539ba05 100644 --- a/Pal.Client/DependencyInjectionContext.cs +++ b/Pal.Client/DependencyInjectionContext.cs @@ -1,6 +1,7 @@ using System; using System.IO; using Dalamud.Data; +using Dalamud.Extensions.MicrosoftLogging; using Dalamud.Game; using Dalamud.Game.ClientState; using Dalamud.Game.ClientState.Conditions; @@ -18,7 +19,6 @@ using Pal.Client.Configuration; using Pal.Client.Configuration.Legacy; using Pal.Client.Database; using Pal.Client.DependencyInjection; -using Pal.Client.DependencyInjection.Logging; using Pal.Client.Floors; using Pal.Client.Net; using Pal.Client.Rendering; @@ -33,7 +33,7 @@ namespace Pal.Client internal sealed class DependencyInjectionContext : IDisposable { public const string DatabaseFileName = "palace-pal.data.sqlite3"; - public static DalamudLoggerProvider LoggerProvider { get; } = new(); + public static DalamudLoggerProvider LoggerProvider { get; } = new(typeof(Plugin).Assembly); /// /// Initialized as temporary logger, will be overriden once context is ready with a logger that supports scopes. @@ -70,7 +70,7 @@ namespace Pal.Client .AddFilter("Microsoft.EntityFrameworkCore.Database", LogLevel.Warning) .AddFilter("Grpc", LogLevel.Debug) .ClearProviders() - .AddProvider(LoggerProvider)); + .AddDalamudLogger(plugin)); // dalamud _serviceCollection.AddSingleton(plugin); diff --git a/Pal.Client/Pal.Client.csproj b/Pal.Client/Pal.Client.csproj index 10baf2e..1564690 100644 --- a/Pal.Client/Pal.Client.csproj +++ b/Pal.Client/Pal.Client.csproj @@ -37,6 +37,7 @@ +