Skip to content

Commit

Permalink
add unit test, provide namespace config
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-heinz committed Jan 16, 2023
1 parent 00e9a6e commit 9206f58
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 15 deletions.
24 changes: 24 additions & 0 deletions Arrowgene.Logging.Test/Arrowgene.Logging.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Arrowgene.Logging\Arrowgene.Logging.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions Arrowgene.Logging.Test/LogProviderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using Xunit;

namespace Arrowgene.Logging.Test;

public class LogProviderTest
{
private class TestNsLogger : TestLogger
{
public override void Initialize(string identity, string name, Action<Log> write, object loggerTypeTag,
object identityTag)
{
Assert.Equal("Arrowgene.Logging.Test", identityTag);
}
}

[Fact]
public void TestNsResolution()
{
LogProvider.ConfigureNamespace("Arrowgene.Logging.Test", "Arrowgene.Logging.Test");
LogProvider.Logger<TestNsLogger>(this);
}
}
34 changes: 34 additions & 0 deletions Arrowgene.Logging.Test/TestLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

namespace Arrowgene.Logging.Test;

public class TestLogger : ILogger
{
public virtual void Initialize(string identity, string name, Action<Log> write, object loggerTypeTag, object identityTag)
{
}

public virtual void Write(LogLevel logLevel, string message, object tag)
{
}

public virtual void Trace(string message)
{
}

public virtual void Info(string message)
{
}

public virtual void Debug(string message)
{
}

public virtual void Error(string message)
{
}

public virtual void Exception(Exception exception)
{
}
}
14 changes: 14 additions & 0 deletions Arrowgene.Logging.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Arrowgene.Logging", "Arrowgene.Logging\Arrowgene.Logging.csproj", "{5ECAC55A-187D-4834-993A-EB261E78DF3A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Arrowgene.Logging.Test", "Arrowgene.Logging.Test\Arrowgene.Logging.Test.csproj", "{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -30,5 +32,17 @@ Global
{5ECAC55A-187D-4834-993A-EB261E78DF3A}.Release|x64.Build.0 = Release|x64
{5ECAC55A-187D-4834-993A-EB261E78DF3A}.Release|x86.ActiveCfg = Release|x86
{5ECAC55A-187D-4834-993A-EB261E78DF3A}.Release|x86.Build.0 = Release|x86
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Debug|x64.ActiveCfg = Debug|Any CPU
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Debug|x64.Build.0 = Debug|Any CPU
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Debug|x86.ActiveCfg = Debug|Any CPU
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Debug|x86.Build.0 = Debug|Any CPU
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Release|Any CPU.Build.0 = Release|Any CPU
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Release|x64.ActiveCfg = Release|Any CPU
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Release|x64.Build.0 = Release|Any CPU
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Release|x86.ActiveCfg = Release|Any CPU
{B70B53CC-D0F8-43FC-94AB-B49536EDD55B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
8 changes: 6 additions & 2 deletions Arrowgene.Logging/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ namespace Arrowgene.Logging
{
public interface ILogger
{
void Initialize(string identity, string name, Action<Log> write, object configuration);
void Initialize(string identity,
string name, Action<Log> write,
object loggerTypeTag,
object identityTag
);

void Write(LogLevel logLevel, string message, object tag);
void Trace(string message);
void Info(string message);
void Debug(string message);
void Error(string message);
void Exception(Exception exception);

}
}
58 changes: 46 additions & 12 deletions Arrowgene.Logging/LogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ public static class LogProvider
{
private static readonly BlockingCollection<Log> Events;
private static readonly Dictionary<string, ILogger> Loggers;
private static readonly Dictionary<string, object> Configurations;
private static readonly Dictionary<string, object> LoggerTypeConfigurations;
private static readonly Dictionary<string, object> NamespaceConfigurations;
private static readonly object Lock;

private static CancellationTokenSource _cancellationTokenSource;
private static Thread _thread;
private static bool _running;

static LogProvider()
{
Events = new BlockingCollection<Log>();
Loggers = new Dictionary<string, ILogger>();
Configurations = new Dictionary<string, object>();
LoggerTypeConfigurations = new Dictionary<string, object>();
NamespaceConfigurations = new Dictionary<string, object>();
Lock = new object();
_running = false;
}
Expand Down Expand Up @@ -97,15 +99,32 @@ public static ILogger Logger(Type type)
{
if (!Loggers.TryGetValue(identity, out logger))
{
object configuration = null;
string typeName = typeof(T).FullName;
if (typeName != null && Configurations.ContainsKey(typeName))
object loggerTypeTag = null;
string loggerTypeName = typeof(T).FullName;
if (loggerTypeName != null && LoggerTypeConfigurations.ContainsKey(loggerTypeName))
{
configuration = Configurations[typeName];
loggerTypeTag = LoggerTypeConfigurations[loggerTypeName];
}

object identityTag = null;
string searchNs = identity;
while (true)
{
if (NamespaceConfigurations.ContainsKey(searchNs))
{
identityTag = NamespaceConfigurations[searchNs];
break;
}
int lastIdx = searchNs.LastIndexOf('.');
if (lastIdx == -1)
{
break;
}
searchNs = searchNs.Substring(0, lastIdx);
}

logger = new T();
logger.Initialize(identity, name, Write, configuration);
logger.Initialize(identity, name, Write, loggerTypeTag, identityTag);
Loggers.Add(identity, logger);
}
}
Expand All @@ -121,7 +140,22 @@ public static ILogger Logger(Type type)

/// <summary>
/// Provide a configuration object that will be passed to every <see cref="ILogger"/> instance
/// by calling <see cref="ILogger.Initialize(string,string,System.Action{Arrowgene.Logging.Log},object)"/> on it.
/// that is created and inside the provided namespace
/// by calling <see cref="ILogger.Initialize(string,string,System.Action{Arrowgene.Logging.Log},object,object)"/> on it.
/// </summary>
public static void ConfigureNamespace(string ns, object configuration)
{
if (NamespaceConfigurations.ContainsKey(ns))
{
return;
}

NamespaceConfigurations.Add(ns, configuration);
}

/// <summary>
/// Provide a configuration object that will be passed to every <see cref="ILogger"/> instance
/// by calling <see cref="ILogger.Initialize(string,string,System.Action{Arrowgene.Logging.Log},object,object)"/> on it.
/// </summary>
public static void Configure<T>(object configuration) where T : ILogger, new()
{
Expand All @@ -135,16 +169,16 @@ public static void Configure(Type type, object configuration)

/// <summary>
/// Provide a configuration object that will be passed to every <see cref="ILogger"/> instance
/// by calling <see cref="ILogger.Initialize(string,string,System.Action{Arrowgene.Logging.Log},object)"/> on it.
/// by calling <see cref="ILogger.Initialize(string,string,System.Action{Arrowgene.Logging.Log},object, object)"/> on it.
/// </summary>
public static void Configure(string identity, object configuration)
{
if (Configurations.ContainsKey(identity))
if (LoggerTypeConfigurations.ContainsKey(identity))
{
return;
}

Configurations.Add(identity, configuration);
LoggerTypeConfigurations.Add(identity, configuration);
}

public static void Write(Log log)
Expand Down
7 changes: 6 additions & 1 deletion Arrowgene.Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ public class Logger : ILogger
private string _identity;
private Action<Log> _write;

public virtual void Initialize(string identity, string name, Action<Log> write, object configuration)
public virtual void Initialize(string identity,
string name,
Action<Log> write,
object loggerTypeTag,
object identityTag
)
{
_identity = identity;
_name = name;
Expand Down

0 comments on commit 9206f58

Please sign in to comment.