From 10c1bcb5e2c0667ab175acd32ad2cddf40325609 Mon Sep 17 00:00:00 2001 From: Sergei Yemelianov <syemelyanov@deltixlab.com> Date: Fri, 13 Sep 2019 15:37:49 +0300 Subject: [PATCH 1/4] Fix issue #34: ZeroLog cannot instantiate an appender from an external assembly --- src/ZeroLog/Appenders/AppenderFactory.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/ZeroLog/Appenders/AppenderFactory.cs b/src/ZeroLog/Appenders/AppenderFactory.cs index bc0953bb..5f891c7c 100644 --- a/src/ZeroLog/Appenders/AppenderFactory.cs +++ b/src/ZeroLog/Appenders/AppenderFactory.cs @@ -28,10 +28,18 @@ public static IAppender CreateAppender(AppenderDefinition definition) private static Type GetAppenderType(AppenderDefinition definition) { - var appenderType = AppDomain.CurrentDomain.GetAssemblies() - .Select(x => x.GetType(definition.AppenderTypeName)) - .FirstOrDefault(x => x != null); - + Type appenderType; + // Check if we have an assembly-qualified name of a type + if (definition.AppenderTypeName.IndexOf(',') >= 0) + { + appenderType = Type.GetType(definition.AppenderTypeName, true, false); + } + else + { + appenderType = AppDomain.CurrentDomain.GetAssemblies() + .Select(x => x.GetType(definition.AppenderTypeName)) + .FirstOrDefault(x => x != null); + } return appenderType; } From f43b3db1c60c27de41ecdee78ef1318ec47287fa Mon Sep 17 00:00:00 2001 From: Sergei Yemelianov <syemelyanov@deltixlab.com> Date: Thu, 19 Sep 2019 16:27:07 +0300 Subject: [PATCH 2/4] Add a test of resolving of an external appender (issue #34) --- .../TestAppender.cs | 20 ++++++++ .../ZeroLog.Tests.ExternalAppender.csproj | 22 +++++++++ .../Appenders/ExternalAppenderTest.cs | 46 +++++++++++++++++++ src/ZeroLog.sln | 9 ++++ 4 files changed, 97 insertions(+) create mode 100644 src/ZeroLog.Tests.Externalappender/TestAppender.cs create mode 100644 src/ZeroLog.Tests.Externalappender/ZeroLog.Tests.ExternalAppender.csproj create mode 100644 src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs diff --git a/src/ZeroLog.Tests.Externalappender/TestAppender.cs b/src/ZeroLog.Tests.Externalappender/TestAppender.cs new file mode 100644 index 00000000..5c619f5c --- /dev/null +++ b/src/ZeroLog.Tests.Externalappender/TestAppender.cs @@ -0,0 +1,20 @@ +using System; +using ZeroLog.Appenders; + +namespace ZeroLog.Tests.ExternalAppender +{ + public class TestAppender : ConsoleAppender + { + public TestAppender() {} + + public TestAppender(string prefixPattern) : base(prefixPattern) + { + } + + public override void Configure(DefaultAppenderConfig parameters) + { + parameters.PrefixPattern = String.Concat("(", Name, "): ", parameters.PrefixPattern); + base.Configure(parameters); + } + } +} diff --git a/src/ZeroLog.Tests.Externalappender/ZeroLog.Tests.ExternalAppender.csproj b/src/ZeroLog.Tests.Externalappender/ZeroLog.Tests.ExternalAppender.csproj new file mode 100644 index 00000000..b0800387 --- /dev/null +++ b/src/ZeroLog.Tests.Externalappender/ZeroLog.Tests.ExternalAppender.csproj @@ -0,0 +1,22 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>netstandard2.0</TargetFramework> + </PropertyGroup> + + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <OutputPath></OutputPath> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\ZeroLog\ZeroLog.csproj"> + <Private>false</Private> + </ProjectReference> + </ItemGroup> + + <Target Name="PostBuild" AfterTargets="PostBuildEvent"> + <Exec Command="mkdir "$(ProjectDir)..\ZeroLog.Tests\bin\$(ConfigurationName)\net47"
copy "$(TargetPath)" "$(ProjectDir)..\ZeroLog.Tests\bin\$(ConfigurationName)\net47"
mkdir "$(ProjectDir)..\ZeroLog.Tests\bin\$(ConfigurationName)\netcoreapp2.0"
copy "$(TargetPath)" "$(ProjectDir)..\ZeroLog.Tests\bin\$(ConfigurationName)\netcoreapp2.0"
copy "$(TargetDir)$(TargetName).deps.json" "$(ProjectDir)..\ZeroLog.Tests\bin\$(ConfigurationName)\netcoreapp2.0"" /> + </Target> + +</Project> diff --git a/src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs b/src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs new file mode 100644 index 00000000..9348c5df --- /dev/null +++ b/src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs @@ -0,0 +1,46 @@ +using Jil; +using NUnit.Framework; +using ZeroLog.Appenders; +using ZeroLog.Config; +using ZeroLog.ConfigResolvers; + +namespace ZeroLog.Tests.Appenders +{ + [TestFixture] + public class ExternalAppenderTest + { + [Test] + public void resolve_appender() + { + var appenderDef = new AppenderDefinition + { + Name = "ExtApp1", + AppenderTypeName = "ZeroLog.Tests.ExternalAppender.TestAppender,ZeroLog.Tests.ExternalAppender", + AppenderJsonConfig = new DefaultAppenderConfig { PrefixPattern = "[%level] @ %time - %logger: " } + }; + var config = new ZeroLogJsonConfiguration + { + LogEventBufferSize = 5, + LogEventQueueSize = 7, + RootLogger = new LoggerDefinition + { + Level = Level.Info, + LogEventPoolExhaustionStrategy = LogEventPoolExhaustionStrategy.DropLogMessage, + AppenderReferences = new[] { "ExtApp1" }, + + }, + Appenders = new[] { appenderDef }, + }; + var configResolver = new HierarchicalResolver(); + configResolver.Build(config); + + Assert.DoesNotThrow(() => LogManager.Initialize(configResolver)); + + ILog logger = null; + Assert.DoesNotThrow(() => logger = LogManager.GetLogger(typeof(ExternalAppenderTest))); + Assert.DoesNotThrow(() => logger.Info("Logger initialized.")); + + LogManager.Shutdown(); + } + } +} diff --git a/src/ZeroLog.sln b/src/ZeroLog.sln index 47f7c3f1..100ebdba 100644 --- a/src/ZeroLog.sln +++ b/src/ZeroLog.sln @@ -6,6 +6,9 @@ MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZeroLog", "ZeroLog\ZeroLog.csproj", "{34665A87-497B-4C4E-928E-1DFBEB3F7441}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZeroLog.Tests", "ZeroLog.Tests\ZeroLog.Tests.csproj", "{F2405E3E-69ED-4BA5-A862-CF920B612B16}" + ProjectSection(ProjectDependencies) = postProject + {B40F81DD-A04A-4E50-8063-5023DB1E7975} = {B40F81DD-A04A-4E50-8063-5023DB1E7975} + EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{44EDCA3A-DBC5-415C-9A34-C742912674B5}" ProjectSection(SolutionItems) = preProject @@ -19,6 +22,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{44EDCA3A-D EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZeroLog.Benchmarks", "ZeroLog.Benchmarks\ZeroLog.Benchmarks.csproj", "{D8CB63CF-F35E-4D38-88AF-9FB4C6B29630}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZeroLog.Tests.ExternalAppender", "ZeroLog.Tests.Externalappender\ZeroLog.Tests.ExternalAppender.csproj", "{B40F81DD-A04A-4E50-8063-5023DB1E7975}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -37,6 +42,10 @@ Global {D8CB63CF-F35E-4D38-88AF-9FB4C6B29630}.Debug|Any CPU.Build.0 = Debug|Any CPU {D8CB63CF-F35E-4D38-88AF-9FB4C6B29630}.Release|Any CPU.ActiveCfg = Release|Any CPU {D8CB63CF-F35E-4D38-88AF-9FB4C6B29630}.Release|Any CPU.Build.0 = Release|Any CPU + {B40F81DD-A04A-4E50-8063-5023DB1E7975}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B40F81DD-A04A-4E50-8063-5023DB1E7975}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B40F81DD-A04A-4E50-8063-5023DB1E7975}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B40F81DD-A04A-4E50-8063-5023DB1E7975}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 04bc39e4b82b5862bcb56199ea550ee0032e10dc Mon Sep 17 00:00:00 2001 From: Lucas Trzesniewski <lucas.trzesniewski@gmail.com> Date: Mon, 23 Sep 2019 14:47:07 +0200 Subject: [PATCH 3/4] Cleanup --- .../TestAppender.cs | 12 ++++++---- .../ZeroLog.Tests.ExternalAppender.csproj | 10 +++++++++ .../ZeroLog.Tests.ExternalAppender.csproj | 22 ------------------- .../Appenders/ExternalAppenderTest.cs | 6 ++--- src/ZeroLog.Tests/ZeroLog.Tests.csproj | 1 + src/ZeroLog.sln | 2 +- src/ZeroLog/Appenders/AppenderFactory.cs | 18 +++++---------- 7 files changed, 29 insertions(+), 42 deletions(-) rename src/{ZeroLog.Tests.Externalappender => ZeroLog.Tests.ExternalAppender}/TestAppender.cs (51%) create mode 100644 src/ZeroLog.Tests.ExternalAppender/ZeroLog.Tests.ExternalAppender.csproj delete mode 100644 src/ZeroLog.Tests.Externalappender/ZeroLog.Tests.ExternalAppender.csproj diff --git a/src/ZeroLog.Tests.Externalappender/TestAppender.cs b/src/ZeroLog.Tests.ExternalAppender/TestAppender.cs similarity index 51% rename from src/ZeroLog.Tests.Externalappender/TestAppender.cs rename to src/ZeroLog.Tests.ExternalAppender/TestAppender.cs index 5c619f5c..aba5c722 100644 --- a/src/ZeroLog.Tests.Externalappender/TestAppender.cs +++ b/src/ZeroLog.Tests.ExternalAppender/TestAppender.cs @@ -1,19 +1,23 @@ -using System; +using JetBrains.Annotations; using ZeroLog.Appenders; namespace ZeroLog.Tests.ExternalAppender { + [UsedImplicitly] public class TestAppender : ConsoleAppender { - public TestAppender() {} + public TestAppender() + { + } - public TestAppender(string prefixPattern) : base(prefixPattern) + public TestAppender(string prefixPattern) + : base(prefixPattern) { } public override void Configure(DefaultAppenderConfig parameters) { - parameters.PrefixPattern = String.Concat("(", Name, "): ", parameters.PrefixPattern); + parameters.PrefixPattern = $"({Name}): {parameters.PrefixPattern}"; base.Configure(parameters); } } diff --git a/src/ZeroLog.Tests.ExternalAppender/ZeroLog.Tests.ExternalAppender.csproj b/src/ZeroLog.Tests.ExternalAppender/ZeroLog.Tests.ExternalAppender.csproj new file mode 100644 index 00000000..6540ad3d --- /dev/null +++ b/src/ZeroLog.Tests.ExternalAppender/ZeroLog.Tests.ExternalAppender.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <PropertyGroup> + <TargetFramework>netstandard2.0</TargetFramework> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\ZeroLog\ZeroLog.csproj" /> + </ItemGroup> + +</Project> diff --git a/src/ZeroLog.Tests.Externalappender/ZeroLog.Tests.ExternalAppender.csproj b/src/ZeroLog.Tests.Externalappender/ZeroLog.Tests.ExternalAppender.csproj deleted file mode 100644 index b0800387..00000000 --- a/src/ZeroLog.Tests.Externalappender/ZeroLog.Tests.ExternalAppender.csproj +++ /dev/null @@ -1,22 +0,0 @@ -<Project Sdk="Microsoft.NET.Sdk"> - - <PropertyGroup> - <TargetFramework>netstandard2.0</TargetFramework> - </PropertyGroup> - - <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> - <DefineConstants>DEBUG;TRACE</DefineConstants> - <OutputPath></OutputPath> - </PropertyGroup> - - <ItemGroup> - <ProjectReference Include="..\ZeroLog\ZeroLog.csproj"> - <Private>false</Private> - </ProjectReference> - </ItemGroup> - - <Target Name="PostBuild" AfterTargets="PostBuildEvent"> - <Exec Command="mkdir "$(ProjectDir)..\ZeroLog.Tests\bin\$(ConfigurationName)\net47"
copy "$(TargetPath)" "$(ProjectDir)..\ZeroLog.Tests\bin\$(ConfigurationName)\net47"
mkdir "$(ProjectDir)..\ZeroLog.Tests\bin\$(ConfigurationName)\netcoreapp2.0"
copy "$(TargetPath)" "$(ProjectDir)..\ZeroLog.Tests\bin\$(ConfigurationName)\netcoreapp2.0"
copy "$(TargetDir)$(TargetName).deps.json" "$(ProjectDir)..\ZeroLog.Tests\bin\$(ConfigurationName)\netcoreapp2.0"" /> - </Target> - -</Project> diff --git a/src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs b/src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs index 9348c5df..e2d4ea6e 100644 --- a/src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs +++ b/src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs @@ -1,5 +1,4 @@ -using Jil; -using NUnit.Framework; +using NUnit.Framework; using ZeroLog.Appenders; using ZeroLog.Config; using ZeroLog.ConfigResolvers; @@ -18,6 +17,7 @@ public void resolve_appender() AppenderTypeName = "ZeroLog.Tests.ExternalAppender.TestAppender,ZeroLog.Tests.ExternalAppender", AppenderJsonConfig = new DefaultAppenderConfig { PrefixPattern = "[%level] @ %time - %logger: " } }; + var config = new ZeroLogJsonConfiguration { LogEventBufferSize = 5, @@ -27,10 +27,10 @@ public void resolve_appender() Level = Level.Info, LogEventPoolExhaustionStrategy = LogEventPoolExhaustionStrategy.DropLogMessage, AppenderReferences = new[] { "ExtApp1" }, - }, Appenders = new[] { appenderDef }, }; + var configResolver = new HierarchicalResolver(); configResolver.Build(config); diff --git a/src/ZeroLog.Tests/ZeroLog.Tests.csproj b/src/ZeroLog.Tests/ZeroLog.Tests.csproj index 48efb490..e5843c40 100644 --- a/src/ZeroLog.Tests/ZeroLog.Tests.csproj +++ b/src/ZeroLog.Tests/ZeroLog.Tests.csproj @@ -7,6 +7,7 @@ </PropertyGroup> <ItemGroup> + <ProjectReference Include="..\ZeroLog.Tests.ExternalAppender\ZeroLog.Tests.ExternalAppender.csproj" /> <ProjectReference Include="..\ZeroLog\ZeroLog.csproj" /> </ItemGroup> diff --git a/src/ZeroLog.sln b/src/ZeroLog.sln index 100ebdba..bca02441 100644 --- a/src/ZeroLog.sln +++ b/src/ZeroLog.sln @@ -22,7 +22,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{44EDCA3A-D EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZeroLog.Benchmarks", "ZeroLog.Benchmarks\ZeroLog.Benchmarks.csproj", "{D8CB63CF-F35E-4D38-88AF-9FB4C6B29630}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZeroLog.Tests.ExternalAppender", "ZeroLog.Tests.Externalappender\ZeroLog.Tests.ExternalAppender.csproj", "{B40F81DD-A04A-4E50-8063-5023DB1E7975}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZeroLog.Tests.ExternalAppender", "ZeroLog.Tests.ExternalAppender\ZeroLog.Tests.ExternalAppender.csproj", "{B40F81DD-A04A-4E50-8063-5023DB1E7975}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/ZeroLog/Appenders/AppenderFactory.cs b/src/ZeroLog/Appenders/AppenderFactory.cs index 5f891c7c..cc3b1b8b 100644 --- a/src/ZeroLog/Appenders/AppenderFactory.cs +++ b/src/ZeroLog/Appenders/AppenderFactory.cs @@ -28,19 +28,13 @@ public static IAppender CreateAppender(AppenderDefinition definition) private static Type GetAppenderType(AppenderDefinition definition) { - Type appenderType; // Check if we have an assembly-qualified name of a type - if (definition.AppenderTypeName.IndexOf(',') >= 0) - { - appenderType = Type.GetType(definition.AppenderTypeName, true, false); - } - else - { - appenderType = AppDomain.CurrentDomain.GetAssemblies() - .Select(x => x.GetType(definition.AppenderTypeName)) - .FirstOrDefault(x => x != null); - } - return appenderType; + if (definition.AppenderTypeName.Contains(",")) + return Type.GetType(definition.AppenderTypeName, true, false); + + return AppDomain.CurrentDomain.GetAssemblies() + .Select(x => x.GetType(definition.AppenderTypeName)) + .FirstOrDefault(x => x != null); } private static object GetAppenderParameters(AppenderDefinition definition, Type appenderParameterType) From 4bf2f1cf7387dd715de74afb3da987bf952a5b3a Mon Sep 17 00:00:00 2001 From: Lucas Trzesniewski <lucas.trzesniewski@gmail.com> Date: Mon, 23 Sep 2019 14:52:25 +0200 Subject: [PATCH 4/4] Assert only that the proper appender has been resolved --- .../Appenders/ExternalAppenderTest.cs | 17 +++++++---------- src/ZeroLog/Appenders/GuardedAppender.cs | 17 +++++++++-------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs b/src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs index e2d4ea6e..62b50f6a 100644 --- a/src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs +++ b/src/ZeroLog.Tests/Appenders/ExternalAppenderTest.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Linq; +using NUnit.Framework; using ZeroLog.Appenders; using ZeroLog.Config; using ZeroLog.ConfigResolvers; @@ -9,12 +10,12 @@ namespace ZeroLog.Tests.Appenders public class ExternalAppenderTest { [Test] - public void resolve_appender() + public void should_resolve_appender_from_assembly_qualified_name() { var appenderDef = new AppenderDefinition { Name = "ExtApp1", - AppenderTypeName = "ZeroLog.Tests.ExternalAppender.TestAppender,ZeroLog.Tests.ExternalAppender", + AppenderTypeName = "ZeroLog.Tests.ExternalAppender.TestAppender, ZeroLog.Tests.ExternalAppender", AppenderJsonConfig = new DefaultAppenderConfig { PrefixPattern = "[%level] @ %time - %logger: " } }; @@ -34,13 +35,9 @@ public void resolve_appender() var configResolver = new HierarchicalResolver(); configResolver.Build(config); - Assert.DoesNotThrow(() => LogManager.Initialize(configResolver)); - - ILog logger = null; - Assert.DoesNotThrow(() => logger = LogManager.GetLogger(typeof(ExternalAppenderTest))); - Assert.DoesNotThrow(() => logger.Info("Logger initialized.")); - - LogManager.Shutdown(); + var appenders = configResolver.GetAllAppenders().ToList(); + Assert.AreEqual(1, appenders.Count); + Assert.AreEqual("ZeroLog.Tests.ExternalAppender.TestAppender", ((GuardedAppender)appenders[0]).Appender.GetType().FullName); } } } diff --git a/src/ZeroLog/Appenders/GuardedAppender.cs b/src/ZeroLog/Appenders/GuardedAppender.cs index 55d1db66..b3a516ae 100644 --- a/src/ZeroLog/Appenders/GuardedAppender.cs +++ b/src/ZeroLog/Appenders/GuardedAppender.cs @@ -6,21 +6,22 @@ namespace ZeroLog.Appenders { internal class GuardedAppender : IAppender { - private readonly IAppender _appender; private readonly TimeSpan _quarantineDelay; private DateTime? _nextActivationTime; + internal IAppender Appender { get; } + public GuardedAppender(IAppender appender, TimeSpan quarantineDelay) { - _appender = appender; + Appender = appender; _quarantineDelay = quarantineDelay; _nextActivationTime = null; } public string Name { - get => _appender.Name; - set => _appender.Name = value; + get => Appender.Name; + set => Appender.Name = value; } public void WriteEvent(ILogEventHeader logEventHeader, byte[] messageBytes, int messageLength) @@ -30,7 +31,7 @@ public void WriteEvent(ILogEventHeader logEventHeader, byte[] messageBytes, int try { - _appender.WriteEvent(logEventHeader, messageBytes, messageLength); + Appender.WriteEvent(logEventHeader, messageBytes, messageLength); _nextActivationTime = null; } catch (Exception) @@ -40,12 +41,12 @@ public void WriteEvent(ILogEventHeader logEventHeader, byte[] messageBytes, int } public void SetEncoding(Encoding encoding) - => _appender.SetEncoding(encoding); + => Appender.SetEncoding(encoding); public void Flush() - => _appender.Flush(); + => Appender.Flush(); public void Dispose() - => _appender.Dispose(); + => Appender.Dispose(); } }