Logging: Use underlying serilog logger directly
This commit is contained in:
parent
3954c839fb
commit
8b6dd52b54
@ -1,14 +1,16 @@
|
|||||||
using Dalamud.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Serilog.Events;
|
||||||
|
|
||||||
namespace Pal.Client.DependencyInjection.Logging
|
namespace Pal.Client.DependencyInjection.Logging
|
||||||
{
|
{
|
||||||
internal sealed class DalamudLogger : ILogger
|
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 string _name;
|
||||||
private readonly IExternalScopeProvider? _scopeProvider;
|
private readonly IExternalScopeProvider? _scopeProvider;
|
||||||
|
|
||||||
@ -22,10 +24,8 @@ namespace Pal.Client.DependencyInjection.Logging
|
|||||||
where TState : notnull
|
where TState : notnull
|
||||||
=> _scopeProvider?.Push(state) ?? NullScope.Instance;
|
=> _scopeProvider?.Push(state) ?? NullScope.Instance;
|
||||||
|
|
||||||
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;
|
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None && PluginLogDelegate.IsEnabled(ToSerilogLevel(logLevel));
|
||||||
|
|
||||||
// PluginLog detects the plugin name as `Microsoft.Extensions.Logging` if inlined
|
|
||||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
||||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
|
||||||
Func<TState, Exception?, string> formatter)
|
Func<TState, Exception?, string> formatter)
|
||||||
{
|
{
|
||||||
@ -36,6 +36,7 @@ namespace Pal.Client.DependencyInjection.Logging
|
|||||||
throw new ArgumentNullException(nameof(formatter));
|
throw new ArgumentNullException(nameof(formatter));
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.Append('[').Append(AssemblyName).Append("] ");
|
||||||
_scopeProvider?.ForEachScope((scope, builder) =>
|
_scopeProvider?.ForEachScope((scope, builder) =>
|
||||||
{
|
{
|
||||||
if (scope is IEnumerable<KeyValuePair<string, object>> properties)
|
if (scope is IEnumerable<KeyValuePair<string, object>> properties)
|
||||||
@ -50,37 +51,22 @@ namespace Pal.Client.DependencyInjection.Logging
|
|||||||
builder.Append('<').Append(scope).Append("> ");
|
builder.Append('<').Append(scope).Append("> ");
|
||||||
},
|
},
|
||||||
sb);
|
sb);
|
||||||
sb.Append('[').Append(_name).Append("] ").Append(formatter(state, null));
|
sb.Append(_name).Append(": ").Append(formatter(state, null));
|
||||||
string message = sb.ToString();
|
PluginLogDelegate.Write(ToSerilogLevel(logLevel), exception, sb.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
#pragma warning disable CS8604 // the nullability on PluginLog methods is wrong and allows nulls for exceptions, WriteLog even declares the parameter as `Exception? exception = null`
|
private LogEventLevel ToSerilogLevel(LogLevel logLevel)
|
||||||
switch (logLevel)
|
{
|
||||||
|
return logLevel switch
|
||||||
{
|
{
|
||||||
case LogLevel.Critical:
|
LogLevel.Critical => LogEventLevel.Fatal,
|
||||||
PluginLog.Fatal(exception, message);
|
LogLevel.Error => LogEventLevel.Error,
|
||||||
break;
|
LogLevel.Warning => LogEventLevel.Warning,
|
||||||
|
LogLevel.Information => LogEventLevel.Information,
|
||||||
case LogLevel.Error:
|
LogLevel.Debug => LogEventLevel.Debug,
|
||||||
PluginLog.Error(exception, message);
|
LogLevel.Trace => LogEventLevel.Verbose,
|
||||||
break;
|
_ => throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null)
|
||||||
|
};
|
||||||
case LogLevel.Warning:
|
|
||||||
PluginLog.Warning(exception, message);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LogLevel.Information:
|
|
||||||
PluginLog.Information(exception, message);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LogLevel.Debug:
|
|
||||||
PluginLog.Debug(exception, message);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LogLevel.Trace:
|
|
||||||
PluginLog.Verbose(exception, message);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#pragma warning restore CS8604
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class NullScope : IDisposable
|
private sealed class NullScope : IDisposable
|
||||||
|
@ -61,7 +61,8 @@ namespace Pal.Client
|
|||||||
CancellationToken token = _initCts.Token;
|
CancellationToken token = _initCts.Token;
|
||||||
IServiceCollection services = new ServiceCollection();
|
IServiceCollection services = new ServiceCollection();
|
||||||
services.AddLogging(builder =>
|
services.AddLogging(builder =>
|
||||||
builder.AddFilter("Microsoft.EntityFrameworkCore.Database", LogLevel.Warning)
|
builder.AddFilter("Pal", LogLevel.Trace)
|
||||||
|
.AddFilter("Microsoft.EntityFrameworkCore.Database", LogLevel.Warning)
|
||||||
.AddFilter("Grpc", LogLevel.Debug)
|
.AddFilter("Grpc", LogLevel.Debug)
|
||||||
.ClearProviders()
|
.ClearProviders()
|
||||||
.AddProvider(LoggerProvider));
|
.AddProvider(LoggerProvider));
|
||||||
|
@ -90,6 +90,10 @@
|
|||||||
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\FFXIVClientStructs.dll</HintPath>
|
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\FFXIVClientStructs.dll</HintPath>
|
||||||
<Private Condition="'$(Configuration)' != 'EF'">false</Private>
|
<Private Condition="'$(Configuration)' != 'EF'">false</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="Serilog">
|
||||||
|
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\Serilog.dll</HintPath>
|
||||||
|
<Private Condition="'$(Configuration)' != 'EF'">false</Private>
|
||||||
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
Reference in New Issue
Block a user