DTLib.Logging.Microsoft and dependency upgrade
This commit is contained in:
34
DTLib.Logging.Microsoft/DTLib.Logging.Microsoft.csproj
Normal file
34
DTLib.Logging.Microsoft/DTLib.Logging.Microsoft.csproj
Normal file
@@ -0,0 +1,34 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<!--package info-->
|
||||
<PackageId>DTLib.Logging.Microsoft</PackageId>
|
||||
<Version>1.0.0</Version>
|
||||
<Authors>Timerix</Authors>
|
||||
<Description>DTLib logger wrapper with dependency injection</Description>
|
||||
<RepositoryType>GIT</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/Timerix22/DTLib</RepositoryUrl>
|
||||
<PackageProjectUrl>https://github.com/Timerix22/DTLib</PackageProjectUrl>
|
||||
<Configuration>Release</Configuration>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<!--compilation properties-->
|
||||
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
|
||||
<DebugType>embedded</DebugType>
|
||||
<!--language features-->
|
||||
<LangVersion>12</LangVersion>
|
||||
<Nullable>disable</Nullable>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<!--external dependencies-->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<!--DTLib dependencies-->
|
||||
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<ProjectReference Include="..\DTLib.Logging\DTLib.Logging.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
|
||||
<PackageReference Include="DTLib.Logging" Version="1.3.3" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
13
DTLib.Logging.Microsoft/LoggerService.cs
Normal file
13
DTLib.Logging.Microsoft/LoggerService.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace DTLib.Logging.Microsoft;
|
||||
|
||||
public class LoggerService<TCaller> : ServiceDescriptor
|
||||
{
|
||||
// ReSharper disable once RedundantNameQualifier
|
||||
public LoggerService(DTLib.Logging.ILogger logger) : base(
|
||||
typeof(global::Microsoft.Extensions.Logging.ILogger<TCaller>),
|
||||
new MyLoggerWrapper<TCaller>(logger))
|
||||
{
|
||||
}
|
||||
}
|
||||
42
DTLib.Logging.Microsoft/MyLoggerWrapper.cs
Normal file
42
DTLib.Logging.Microsoft/MyLoggerWrapper.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
// ReSharper disable RedundantNameQualifier
|
||||
|
||||
using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DTLib.Logging.Microsoft;
|
||||
|
||||
public class MyLoggerWrapper<TCaller> : global::Microsoft.Extensions.Logging.ILogger<TCaller>
|
||||
{
|
||||
public DTLib.Logging.ILogger Logger;
|
||||
public MyLoggerWrapper(DTLib.Logging.ILogger logger)=>
|
||||
Logger = logger;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
string message = formatter(state, exception);
|
||||
Logger.Log(typeof(TCaller).Name, LogSeverity_FromLogLevel(logLevel), message);
|
||||
}
|
||||
|
||||
private bool _isEnabled=true;
|
||||
public bool IsEnabled(LogLevel logLevel) => _isEnabled;
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
static LogSeverity LogSeverity_FromLogLevel(LogLevel l)
|
||||
=> l switch
|
||||
{
|
||||
LogLevel.Trace => LogSeverity.Debug,
|
||||
LogLevel.Debug => LogSeverity.Debug,
|
||||
LogLevel.Information => LogSeverity.Info,
|
||||
LogLevel.Warning => LogSeverity.Warn,
|
||||
LogLevel.Error => LogSeverity.Error,
|
||||
LogLevel.Critical => LogSeverity.Error,
|
||||
LogLevel.None => throw new NotImplementedException("LogLevel.None is not supported"),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(l), l, null)
|
||||
}
|
||||
;
|
||||
}
|
||||
Reference in New Issue
Block a user