ContextLogger

This commit is contained in:
Timerix22 2023-01-04 05:25:24 +06:00
parent cbb5471a91
commit 0dd9431c28
2 changed files with 41 additions and 4 deletions

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<!--package info--> <!--package info-->
<PackageId>DTLib.Logging</PackageId> <PackageId>DTLib.Logging</PackageId>
<Version>1.0.3</Version> <Version>1.0.4</Version>
<Authors>Timerix</Authors> <Authors>Timerix</Authors>
<Description>Loggers with dependency injection</Description> <Description>Loggers with dependency injection</Description>
<RepositoryType>GIT</RepositoryType> <RepositoryType>GIT</RepositoryType>
@ -30,6 +30,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(Configuration)' != 'Debug' "> <ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
<PackageReference Include="DTLib" Version="1.0.1" /> <PackageReference Include="DTLib" Version="1.0.1" />
<PackageReference Include="DTLib.Ben.Demystifier" Version="1.0.1" /> <PackageReference Include="DTLib.Ben.Demystifier" Version="1.0.2" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,12 +1,12 @@
namespace DTLib.Logging.New; namespace DTLib.Logging.New;
/// wrapper around ILogger and LoggerExtensions that stores context /// wrapper around ILogger and LoggerExtensions that stores context
public class LoggerContext public class ContextLogger : ILogger
{ {
public ILogger Logger; public ILogger Logger;
public readonly string Context; public readonly string Context;
public LoggerContext(ILogger logger, string context) public ContextLogger(ILogger logger, string context)
{ {
Logger = logger; Logger = logger;
Context = context; Context = context;
@ -41,4 +41,41 @@ public class LoggerContext
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void LogError(Exception ex) public void LogError(Exception ex)
=> Logger.LogError(Context, ex); => Logger.LogError(Context, ex);
public void Dispose()
{
Logger.Dispose();
}
public ILogFormat Format => Logger.Format;
public bool DebugLogEnabled
{
get => Logger.DebugLogEnabled;
set => Logger.DebugLogEnabled = value;
}
public bool InfoLogEnabled
{
get => Logger.InfoLogEnabled;
set => Logger.InfoLogEnabled = value;
}
public bool WarnLogEnabled
{
get => Logger.WarnLogEnabled;
set => Logger.WarnLogEnabled = value;
}
public bool ErrorLogenabled
{
get => Logger.ErrorLogenabled;
set => Logger.ErrorLogenabled = value;
}
/// Appends subContext to Context
public void Log(string subContext, LogSeverity severity, object message, ILogFormat format)
{
Logger.Log($"{Context}/{subContext}", severity, message, format);
}
} }