diff --git a/Arrowgene.Logging.Test/Arrowgene.Logging.Test.csproj b/Arrowgene.Logging.Test/Arrowgene.Logging.Test.csproj new file mode 100644 index 0000000..bda488c --- /dev/null +++ b/Arrowgene.Logging.Test/Arrowgene.Logging.Test.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/Arrowgene.Logging.Test/LogProviderTest.cs b/Arrowgene.Logging.Test/LogProviderTest.cs new file mode 100644 index 0000000..8d4249e --- /dev/null +++ b/Arrowgene.Logging.Test/LogProviderTest.cs @@ -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 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(this); + } +} \ No newline at end of file diff --git a/Arrowgene.Logging.Test/TestLogger.cs b/Arrowgene.Logging.Test/TestLogger.cs new file mode 100644 index 0000000..1923ad2 --- /dev/null +++ b/Arrowgene.Logging.Test/TestLogger.cs @@ -0,0 +1,34 @@ +using System; + +namespace Arrowgene.Logging.Test; + +public class TestLogger : ILogger +{ + public virtual void Initialize(string identity, string name, Action 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) + { + } +} \ No newline at end of file diff --git a/Arrowgene.Logging.sln b/Arrowgene.Logging.sln index e8a61aa..afe8472 100644 --- a/Arrowgene.Logging.sln +++ b/Arrowgene.Logging.sln @@ -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 @@ -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 diff --git a/Arrowgene.Logging/ILogger.cs b/Arrowgene.Logging/ILogger.cs index 4fcb4cc..b70f9ce 100644 --- a/Arrowgene.Logging/ILogger.cs +++ b/Arrowgene.Logging/ILogger.cs @@ -4,13 +4,17 @@ namespace Arrowgene.Logging { public interface ILogger { - void Initialize(string identity, string name, Action write, object configuration); + void Initialize(string identity, + string name, Action 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); - } } \ No newline at end of file diff --git a/Arrowgene.Logging/LogProvider.cs b/Arrowgene.Logging/LogProvider.cs index 3654721..398ac0c 100644 --- a/Arrowgene.Logging/LogProvider.cs +++ b/Arrowgene.Logging/LogProvider.cs @@ -9,18 +9,20 @@ public static class LogProvider { private static readonly BlockingCollection Events; private static readonly Dictionary Loggers; - private static readonly Dictionary Configurations; + private static readonly Dictionary LoggerTypeConfigurations; + private static readonly Dictionary NamespaceConfigurations; private static readonly object Lock; private static CancellationTokenSource _cancellationTokenSource; private static Thread _thread; private static bool _running; - + static LogProvider() { Events = new BlockingCollection(); Loggers = new Dictionary(); - Configurations = new Dictionary(); + LoggerTypeConfigurations = new Dictionary(); + NamespaceConfigurations = new Dictionary(); Lock = new object(); _running = false; } @@ -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); } } @@ -121,7 +140,22 @@ public static ILogger Logger(Type type) /// /// Provide a configuration object that will be passed to every instance - /// by calling on it. + /// that is created and inside the provided namespace + /// by calling on it. + /// + public static void ConfigureNamespace(string ns, object configuration) + { + if (NamespaceConfigurations.ContainsKey(ns)) + { + return; + } + + NamespaceConfigurations.Add(ns, configuration); + } + + /// + /// Provide a configuration object that will be passed to every instance + /// by calling on it. /// public static void Configure(object configuration) where T : ILogger, new() { @@ -135,16 +169,16 @@ public static void Configure(Type type, object configuration) /// /// Provide a configuration object that will be passed to every instance - /// by calling on it. + /// by calling on it. /// 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) diff --git a/Arrowgene.Logging/Logger.cs b/Arrowgene.Logging/Logger.cs index 5fc327d..56b08ba 100644 --- a/Arrowgene.Logging/Logger.cs +++ b/Arrowgene.Logging/Logger.cs @@ -8,7 +8,12 @@ public class Logger : ILogger private string _identity; private Action _write; - public virtual void Initialize(string identity, string name, Action write, object configuration) + public virtual void Initialize(string identity, + string name, + Action write, + object loggerTypeTag, + object identityTag + ) { _identity = identity; _name = name;