1
0
This commit is contained in:
Liza 2023-10-03 10:44:50 +02:00
parent 8a9b2a3757
commit 25065bfa97
Signed by: liza
GPG Key ID: 7199F8D727D55F67
4 changed files with 17 additions and 22 deletions

View File

@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net7.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<PackageId>Dalamud.Extensions.MicrosoftLogging</PackageId>
<Version>1.0.0</Version>
<Version>2.0.0</Version>
<Authors>Liza Carvelli</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/carvelli/Dalamud.Extensions.MicrosoftLogging</PackageProjectUrl>
<RepositoryUrl>https://github.com/carvelli/Dalamud.Extensions.MicrosoftLogging.git</RepositoryUrl>
<PackageProjectUrl>https://git.carvel.li/liza/Dalamud.Extensions.MicrosoftLogging</PackageProjectUrl>
<RepositoryUrl>https://git.carvel.li/liza/Dalamud.Extensions.MicrosoftLogging.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<GeneratePackageOnBuild Condition="$(Configuration) == 'Release'">true</GeneratePackageOnBuild>
<DebugType>portable</DebugType>

View File

@ -1,6 +1,7 @@
using System.Reflection;
using Microsoft.Extensions.Logging;
using System.Text;
using Dalamud.Plugin.Services;
using Serilog.Events;
namespace Dalamud.Extensions.MicrosoftLogging
@ -8,19 +9,14 @@ namespace Dalamud.Extensions.MicrosoftLogging
public sealed class DalamudLogger : ILogger
{
private readonly string _name;
private readonly string _assemblyName;
private readonly IExternalScopeProvider? _scopeProvider;
private readonly Serilog.ILogger _pluginLogDelegate;
private readonly IPluginLog _pluginLogDelegate;
public DalamudLogger(string name, Assembly assembly, IExternalScopeProvider? scopeProvider)
public DalamudLogger(string name, IExternalScopeProvider? scopeProvider, IPluginLog pluginLogDelegate)
{
_name = name;
_assemblyName = assembly.GetName().Name ??
typeof(DalamudLogger).Assembly.GetName().Name ??
"{unknown DalamudLogger}";
_scopeProvider = scopeProvider;
_pluginLogDelegate =
Serilog.Log.ForContext("SourceContext", _assemblyName);
_pluginLogDelegate = pluginLogDelegate;
}
public IDisposable BeginScope<TState>(TState state)
@ -29,7 +25,7 @@ namespace Dalamud.Extensions.MicrosoftLogging
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None && IsEnabled(ToSerilogLevel(logLevel));
private bool IsEnabled(LogEventLevel logEventLevel) => _pluginLogDelegate.IsEnabled(logEventLevel);
private bool IsEnabled(LogEventLevel logEventLevel) => logEventLevel >= _pluginLogDelegate.MinimumLogLevel;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> formatter)
@ -45,7 +41,6 @@ namespace Dalamud.Extensions.MicrosoftLogging
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)

View File

@ -1,4 +1,4 @@
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
@ -8,11 +8,11 @@ namespace Dalamud.Extensions.MicrosoftLogging
public static class DalamudLoggerExtensions
{
public static ILoggingBuilder AddDalamudLogger(
this ILoggingBuilder builder, IDalamudPlugin dalamudPlugin)
this ILoggingBuilder builder, IPluginLog pluginLog)
{
builder.Services.TryAddEnumerable(
ServiceDescriptor.Singleton<ILoggerProvider>(
new DalamudLoggerProvider(dalamudPlugin.GetType().Assembly)));
new DalamudLoggerProvider(pluginLog)));
return builder;
}
}

View File

@ -1,16 +1,16 @@
using System.Reflection;
using Dalamud.Plugin.Services;
using Microsoft.Extensions.Logging;
namespace Dalamud.Extensions.MicrosoftLogging
{
public sealed class DalamudLoggerProvider : ILoggerProvider, ISupportExternalScope
{
private readonly Assembly _assembly;
private readonly IPluginLog _pluginLog;
private IExternalScopeProvider? _scopeProvider;
public DalamudLoggerProvider(Assembly assembly)
public DalamudLoggerProvider(IPluginLog pluginLog)
{
_assembly = assembly;
_pluginLog = pluginLog;
}
ILogger ILoggerProvider.CreateLogger(string categoryName)
@ -29,7 +29,7 @@ namespace Dalamud.Extensions.MicrosoftLogging
=> CreateLoggerImpl(typeof(T).FullName ?? typeof(T).ToString());
private ILogger CreateLoggerImpl(string categoryName)
=> new DalamudLogger(categoryName, _assembly, _scopeProvider);
=> new DalamudLogger(categoryName, _scopeProvider, _pluginLog);
public void SetScopeProvider(IExternalScopeProvider scopeProvider)
{