DTLibInternalLogging

This commit is contained in:
Timerix22 2023-01-04 05:26:19 +06:00
parent 0dd9431c28
commit bfe2c42f63
5 changed files with 50 additions and 19 deletions

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<!--package info--> <!--package info-->
<PackageId>DTLib.Logging</PackageId> <PackageId>DTLib.Logging</PackageId>
<Version>1.0.4</Version> <Version>1.0.5</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>

View File

@ -0,0 +1,37 @@
global using System;
global using System.Collections;
global using System.Collections.Generic;
global using System.Runtime.CompilerServices;
global using System.Linq;
global using System.Text;
global using System.Threading.Tasks;
global using DTLib.Extensions;
global using DTLib.Filesystem;
namespace DTLib.Logging.New;
/// this class can be used to setup logger for DTLib debug log messages
public static class DTLibInternalLogging
{
private static ContextLogger _loggerContext;
public static void SetLogger(ILogger logger)
{
_loggerContext = new ContextLogger(logger, "DTLib");
PublicLog.LogEvent+=LogHandler;
}
private static void LogHandler(string[] msg)
{
if (msg.Length == 1)
{
_loggerContext.LogDebug(msg[0]);
return;
}
StringBuilder b = new();
for (int i = 1; i < msg.Length; i++)
b.Append(msg[i]);
_loggerContext.LogDebug(b.ToString());
}
}

View File

@ -1,12 +0,0 @@
global using System;
global using System.Collections;
global using System.Collections.Generic;
global using System.Runtime.CompilerServices;
global using System.Linq;
global using System.Text;
global using System.Threading.Tasks;
global using DTLib.Extensions;
global using DTLib.Filesystem;
namespace DTLib.Logging.New;

View File

@ -28,11 +28,13 @@
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(Configuration)' != 'Debug' "> <ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
<PackageReference Include="DTLib.Network" Version="1.0.2" /> <PackageReference Include="DTLib.Network" Version="1.0.2" />
<PackageReference Include="DTLib.Logging" Version="1.0.3" />
</ItemGroup> </ItemGroup>
<!--project files--> <!--project files-->
<ItemGroup> <ItemGroup>
<None Update="Dtsod\TestResources\**\*" CopyToOutputDirectory="PreserveNewest" /> <None Update="Dtsod\TestResources\**\*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="DTLib.Logging" Version="1.0.5" />
</ItemGroup>
</Project> </Project>

View File

@ -17,14 +17,17 @@ namespace DTLib.Tests;
public static class Program public static class Program
{ {
public static readonly Logging.ConsoleLogger OldLogger = new("logs", "DTLib.Tests"); public static Logging.ConsoleLogger OldLogger = new("logs", "DTLib.Tests");
public static readonly ILogger NewLogger = new CompositeLogger(new ConsoleLogger(), new FileLogger(OldLogger.LogfileName)); public static ILogger Logger;
public static void Main() public static void Main()
{ {
Logging.PublicLog.LogEvent += OldLogger.Log;
Console.OutputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8;
Console.InputEncoding = Encoding.UTF8; Console.InputEncoding = Encoding.UTF8;
Console.Title="tester"; Logger=new CompositeLogger(new ConsoleLogger(),
new FileLogger("logs", "DTLib.Tests"));
var mainContext = new ContextLogger(Logger, "Main");
DTLibInternalLogging.SetLogger(Logger);
try try
{ {
TestPInvoke.TestAll(); TestPInvoke.TestAll();
@ -33,7 +36,8 @@ public static class Program
TestDtsodV24.TestAll(); TestDtsodV24.TestAll();
} }
catch (Exception ex) catch (Exception ex)
{ NewLogger.LogError("Main", ex); } { mainContext.LogError(ex); }
Console.ResetColor(); Console.ResetColor();
} }
} }