Move logging into nuget package
This commit is contained in:
parent
d25e21619d
commit
cbdcf58063
@ -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
|
||||
{
|
||||
|
@ -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>(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<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
|
||||
Func<TState, Exception?, string> 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<KeyValuePair<string, object>> properties)
|
||||
{
|
||||
foreach (KeyValuePair<string, object> 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()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
/// <summary>
|
||||
/// Manual logger creation, doesn't handle scopes.
|
||||
/// </summary>
|
||||
public ILogger CreateLogger(Type type) => CreateLogger(type.FullName ?? type.ToString());
|
||||
|
||||
/// <summary>
|
||||
/// Manual logger creation, doesn't handle scopes.
|
||||
/// </summary>
|
||||
public ILogger CreateLogger<T>() => CreateLogger(typeof(T));
|
||||
|
||||
public void SetScopeProvider(IExternalScopeProvider scopeProvider)
|
||||
{
|
||||
_scopeProvider = scopeProvider;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
/// <summary>
|
||||
/// 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<IDalamudPlugin>(plugin);
|
||||
|
@ -37,6 +37,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DalamudPackager" Version="2.1.10" />
|
||||
<PackageReference Include="Dalamud.Extensions.MicrosoftLogging" Version="1.0.0" />
|
||||
<PackageReference Include="Google.Protobuf" Version="3.21.12" />
|
||||
<PackageReference Include="Grpc.Net.Client" Version="2.51.0" />
|
||||
<PackageReference Include="GitInfo" Version="2.3.0">
|
||||
|
Loading…
Reference in New Issue
Block a user