2023-02-17 12:57:12 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-11-24 22:28:31 +00:00
|
|
|
|
using System;
|
2023-02-16 23:54:23 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2023-02-17 12:57:12 +00:00
|
|
|
|
using Serilog.Events;
|
2022-11-24 22:28:31 +00:00
|
|
|
|
|
2023-02-16 23:54:23 +00:00
|
|
|
|
namespace Pal.Client.DependencyInjection.Logging
|
2022-11-24 22:28:31 +00:00
|
|
|
|
{
|
2023-02-16 23:54:23 +00:00
|
|
|
|
internal sealed class DalamudLogger : ILogger
|
2022-11-24 22:28:31 +00:00
|
|
|
|
{
|
2023-02-17 12:57:12 +00:00
|
|
|
|
private static readonly string AssemblyName = typeof(Plugin).Assembly.GetName().Name!;
|
|
|
|
|
private static readonly Serilog.ILogger PluginLogDelegate = Serilog.Log.ForContext("SourceContext", AssemblyName);
|
2022-11-24 22:28:31 +00:00
|
|
|
|
private readonly string _name;
|
2023-02-16 23:54:23 +00:00
|
|
|
|
private readonly IExternalScopeProvider? _scopeProvider;
|
2022-11-24 22:28:31 +00:00
|
|
|
|
|
2023-02-16 23:54:23 +00:00
|
|
|
|
public DalamudLogger(string name, IExternalScopeProvider? scopeProvider)
|
2022-11-24 22:28:31 +00:00
|
|
|
|
{
|
|
|
|
|
_name = name;
|
2023-02-16 23:54:23 +00:00
|
|
|
|
_scopeProvider = scopeProvider;
|
2022-11-24 22:28:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-01 16:56:26 +00:00
|
|
|
|
public IDisposable BeginScope<TState>(TState state)
|
|
|
|
|
where TState : notnull
|
2023-02-16 23:54:23 +00:00
|
|
|
|
=> _scopeProvider?.Push(state) ?? NullScope.Instance;
|
2022-11-24 22:28:31 +00:00
|
|
|
|
|
2023-02-17 17:36:55 +00:00
|
|
|
|
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None && IsEnabled(ToSerilogLevel(logLevel));
|
|
|
|
|
|
|
|
|
|
private bool IsEnabled(LogEventLevel logEventLevel) => PluginLogDelegate.IsEnabled(logEventLevel);
|
2022-11-24 22:28:31 +00:00
|
|
|
|
|
2023-02-16 23:54:23 +00:00
|
|
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
|
|
|
|
|
Func<TState, Exception?, string> formatter)
|
2022-11-24 22:28:31 +00:00
|
|
|
|
{
|
2023-02-17 17:36:55 +00:00
|
|
|
|
if (logLevel == LogLevel.None)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
LogEventLevel logEventLevel = ToSerilogLevel(logLevel);
|
|
|
|
|
if (!IsEnabled(logEventLevel))
|
2022-11-24 22:28:31 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (formatter == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(formatter));
|
|
|
|
|
|
2023-02-16 23:54:23 +00:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2023-02-17 12:57:12 +00:00
|
|
|
|
sb.Append('[').Append(AssemblyName).Append("] ");
|
2023-02-16 23:54:23 +00:00
|
|
|
|
_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);
|
2023-02-17 12:57:12 +00:00
|
|
|
|
sb.Append(_name).Append(": ").Append(formatter(state, null));
|
2023-02-17 17:36:55 +00:00
|
|
|
|
PluginLogDelegate.Write(logEventLevel, exception, sb.ToString());
|
2023-02-17 12:57:12 +00:00
|
|
|
|
}
|
2022-11-24 22:28:31 +00:00
|
|
|
|
|
2023-02-17 12:57:12 +00:00
|
|
|
|
private LogEventLevel ToSerilogLevel(LogLevel logLevel)
|
|
|
|
|
{
|
|
|
|
|
return logLevel switch
|
2022-11-24 22:28:31 +00:00
|
|
|
|
{
|
2023-02-17 12:57:12 +00:00
|
|
|
|
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)
|
|
|
|
|
};
|
2022-11-24 22:28:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 23:54:23 +00:00
|
|
|
|
private sealed class NullScope : IDisposable
|
2022-11-24 22:28:31 +00:00
|
|
|
|
{
|
2023-02-11 20:10:45 +00:00
|
|
|
|
public static NullScope Instance { get; } = new();
|
2022-11-24 22:28:31 +00:00
|
|
|
|
|
|
|
|
|
private NullScope()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|