From 3c515c1cf01afd6624a2c4e13bf03a00591188c5 Mon Sep 17 00:00:00 2001 From: Lucas Trzesniewski Date: Thu, 9 Jul 2020 11:39:53 +0200 Subject: [PATCH 1/3] Fix JSON tests --- src/ZeroLog.Tests/Config/ConfiguratorTests.cs | 70 ++++++++++++++++--- src/ZeroLog/Appenders/AppenderFactory.cs | 2 +- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/ZeroLog.Tests/Config/ConfiguratorTests.cs b/src/ZeroLog.Tests/Config/ConfiguratorTests.cs index 8ca96248..a7712590 100644 --- a/src/ZeroLog.Tests/Config/ConfiguratorTests.cs +++ b/src/ZeroLog.Tests/Config/ConfiguratorTests.cs @@ -1,10 +1,8 @@ using System.Linq; -using Jil; using NFluent; using NUnit.Framework; using ZeroLog.Appenders; using ZeroLog.Config; -using ZeroLog.ConfigResolvers; namespace ZeroLog.Tests.Config { @@ -14,8 +12,12 @@ public class ConfiguratorTests [Test] public void should_load_configuration() { - var appenderA = new AppenderDefinition { Name = "A", AppenderTypeName = nameof(ConsoleAppender), AppenderJsonConfig = JSON.Serialize(new DefaultAppenderConfig{PrefixPattern = "[%level] @ %time - %logger: " })}; - var appenderB = new AppenderDefinition { Name = "B", AppenderTypeName = nameof(DateAndSizeRollingFileAppender), AppenderJsonConfig = JSON.Serialize(new DateAndSizeRollingFileAppenderConfig { FilePathRoot = "totopath " }) }; + var appenderAConfig = new DefaultAppenderConfig { PrefixPattern = "[%level] @ %time - %logger: " }; + var appenderBConfig = new DateAndSizeRollingFileAppenderConfig { FilePathRoot = "totopath " }; + + var appenderA = new AppenderDefinition { Name = "A", AppenderTypeName = nameof(ConsoleAppender), AppenderJsonConfig = appenderAConfig }; + var appenderB = new AppenderDefinition { Name = "B", AppenderTypeName = nameof(DateAndSizeRollingFileAppender), AppenderJsonConfig = appenderBConfig }; + var config = new ZeroLogJsonConfiguration { LogEventBufferSize = 5, @@ -24,13 +26,49 @@ public void should_load_configuration() { Level = Level.Warn, LogEventPoolExhaustionStrategy = LogEventPoolExhaustionStrategy.DropLogMessage, - AppenderReferences = new []{ "A" }, - + AppenderReferences = new[] { "A" }, }, Appenders = new[] { appenderA, appenderB }, - Loggers = new[] {new LoggerDefinition{ Name = "Abc.Zebus", Level = Level.Debug, AppenderReferences = new []{ "B" } }} + Loggers = new[] { new LoggerDefinition { Name = "Abc.Zebus", Level = Level.Debug, AppenderReferences = new[] { "B" } } } }; - var configJson = JSON.Serialize(config, Options.PrettyPrint); + + var configJson = @" + { + ""LazyRegisterEnums"": false, + ""LogEventArgumentCapacity"": 32, + ""LogEventBufferSize"": 5, + ""LogEventQueueSize"": 7, + ""Appenders"": [{ + ""AppenderJsonConfig"": { + ""PrefixPattern"": ""[%level] @ %time - %logger: "" + }, + ""AppenderTypeName"": ""ConsoleAppender"", + ""Name"": ""A"" + }, { + ""AppenderJsonConfig"": { + ""FilePathRoot"": ""totopath "" + }, + ""AppenderTypeName"": ""DateAndSizeRollingFileAppender"", + ""Name"": ""B"" + }], + ""Loggers"": [{ + ""IncludeParentAppenders"": false, + ""AppenderReferences"": [""B""], + ""Level"": ""Debug"", + ""LogEventPoolExhaustionStrategy"": ""DropLogMessageAndNotifyAppenders"", + ""LogEventArgumentExhaustionStrategy"": ""TruncateMessage"", + ""Name"": ""Abc.Zebus"" + }], + ""RootLogger"": { + ""IncludeParentAppenders"": false, + ""AppenderReferences"": [""A""], + ""Level"": ""Warn"", + ""LogEventPoolExhaustionStrategy"": ""DropLogMessage"", + ""LogEventArgumentExhaustionStrategy"": ""TruncateMessage"", + ""Name"": null + }, + ""NullDisplayString"": null + }"; var loadedConfig = JsonConfigurator.DeserializeConfiguration(configJson); @@ -42,11 +80,18 @@ public void should_load_configuration() Check.That(loadedConfig.Appenders.Single(a => a.Name == "A").Name).Equals(appenderA.Name); Check.That(loadedConfig.Appenders.Single(a => a.Name == "A").AppenderTypeName).Equals(appenderA.AppenderTypeName); - Check.That(loadedConfig.Appenders.Single(a => a.Name == "A").AppenderJsonConfig).Equals(appenderA.AppenderJsonConfig); Check.That(loadedConfig.Appenders.Single(a => a.Name == "B").Name).Equals(appenderB.Name); Check.That(loadedConfig.Appenders.Single(a => a.Name == "B").AppenderTypeName).Equals(appenderB.AppenderTypeName); - Check.That(loadedConfig.Appenders.Single(a => a.Name == "B").AppenderJsonConfig).Equals(appenderB.AppenderJsonConfig); + + var appenderALoadedConfig = (DefaultAppenderConfig)AppenderFactory.GetAppenderParameters(loadedConfig.Appenders.Single(a => a.Name == "A"), typeof(DefaultAppenderConfig)); + Check.That(appenderALoadedConfig.PrefixPattern).IsEqualTo(appenderAConfig.PrefixPattern); + + var appenderBLoadedConfig = (DateAndSizeRollingFileAppenderConfig)AppenderFactory.GetAppenderParameters(loadedConfig.Appenders.Single(a => a.Name == "B"), typeof(DateAndSizeRollingFileAppenderConfig)); + Check.That(appenderBLoadedConfig.Extension).IsEqualTo(appenderBConfig.Extension); + Check.That(appenderBLoadedConfig.PrefixPattern).IsEqualTo(appenderBConfig.PrefixPattern); + Check.That(appenderBLoadedConfig.FilePathRoot).IsEqualTo(appenderBConfig.FilePathRoot); + Check.That(appenderBLoadedConfig.MaxFileSizeInBytes).IsEqualTo(appenderBConfig.MaxFileSizeInBytes); } [Test] @@ -68,7 +113,7 @@ public void should_handle_missing_part() { ""Name"": ""B"", ""AppenderTypeName"": ""DateAndSizeRollingFileAppender"", - ""AppenderJsonConfig"": ""{\""FilePathRoot\"":\""totopath \""}"" + ""AppenderJsonConfig"": {""FilePathRoot"":""totopath ""} } ], @@ -87,6 +132,9 @@ public void should_handle_missing_part() Check.That(config.RootLogger.LogEventPoolExhaustionStrategy).Equals(LogEventPoolExhaustionStrategy.Default); Check.That(config.LogEventBufferSize).Equals(new ZeroLogJsonConfiguration().LogEventBufferSize); Check.That(config.LogEventQueueSize).Equals(new ZeroLogJsonConfiguration().LogEventQueueSize); + + var appenderConfig = (DateAndSizeRollingFileAppenderConfig)AppenderFactory.GetAppenderParameters(config.Appenders[1], typeof(DateAndSizeRollingFileAppenderConfig)); + Check.That(appenderConfig.FilePathRoot).IsEqualTo("totopath "); } } } diff --git a/src/ZeroLog/Appenders/AppenderFactory.cs b/src/ZeroLog/Appenders/AppenderFactory.cs index bbc24401..d01901a5 100644 --- a/src/ZeroLog/Appenders/AppenderFactory.cs +++ b/src/ZeroLog/Appenders/AppenderFactory.cs @@ -40,7 +40,7 @@ public static IAppender CreateAppender(AppenderDefinition definition) .FirstOrDefault(x => x != null); } - private static object GetAppenderParameters(AppenderDefinition definition, Type appenderParameterType) + internal static object GetAppenderParameters(AppenderDefinition definition, Type appenderParameterType) { var appenderParameterJson = JSON.SerializeDynamic(definition.AppenderJsonConfig); var appenderParameters = (object)JSON.Deserialize(appenderParameterJson, appenderParameterType); From f0d063b69130af2e49bb82d75714996b0d459e1a Mon Sep 17 00:00:00 2001 From: Lucas Trzesniewski Date: Thu, 9 Jul 2020 10:58:57 +0200 Subject: [PATCH 2/3] Replace Jil with Newtonsoft.Json --- src/ZeroLog.Tests/Config/ConfiguratorTests.cs | 39 +------------------ src/ZeroLog/Appenders/AppenderFactory.cs | 22 ++++++++--- src/ZeroLog/Utils/JsonExtensions.cs | 4 +- src/ZeroLog/ZeroLog.csproj | 3 +- 4 files changed, 23 insertions(+), 45 deletions(-) diff --git a/src/ZeroLog.Tests/Config/ConfiguratorTests.cs b/src/ZeroLog.Tests/Config/ConfiguratorTests.cs index a7712590..1dbd9396 100644 --- a/src/ZeroLog.Tests/Config/ConfiguratorTests.cs +++ b/src/ZeroLog.Tests/Config/ConfiguratorTests.cs @@ -1,4 +1,5 @@ using System.Linq; +using Newtonsoft.Json; using NFluent; using NUnit.Framework; using ZeroLog.Appenders; @@ -32,43 +33,7 @@ public void should_load_configuration() Loggers = new[] { new LoggerDefinition { Name = "Abc.Zebus", Level = Level.Debug, AppenderReferences = new[] { "B" } } } }; - var configJson = @" - { - ""LazyRegisterEnums"": false, - ""LogEventArgumentCapacity"": 32, - ""LogEventBufferSize"": 5, - ""LogEventQueueSize"": 7, - ""Appenders"": [{ - ""AppenderJsonConfig"": { - ""PrefixPattern"": ""[%level] @ %time - %logger: "" - }, - ""AppenderTypeName"": ""ConsoleAppender"", - ""Name"": ""A"" - }, { - ""AppenderJsonConfig"": { - ""FilePathRoot"": ""totopath "" - }, - ""AppenderTypeName"": ""DateAndSizeRollingFileAppender"", - ""Name"": ""B"" - }], - ""Loggers"": [{ - ""IncludeParentAppenders"": false, - ""AppenderReferences"": [""B""], - ""Level"": ""Debug"", - ""LogEventPoolExhaustionStrategy"": ""DropLogMessageAndNotifyAppenders"", - ""LogEventArgumentExhaustionStrategy"": ""TruncateMessage"", - ""Name"": ""Abc.Zebus"" - }], - ""RootLogger"": { - ""IncludeParentAppenders"": false, - ""AppenderReferences"": [""A""], - ""Level"": ""Warn"", - ""LogEventPoolExhaustionStrategy"": ""DropLogMessage"", - ""LogEventArgumentExhaustionStrategy"": ""TruncateMessage"", - ""Name"": null - }, - ""NullDisplayString"": null - }"; + var configJson = JsonConvert.SerializeObject(config); var loadedConfig = JsonConfigurator.DeserializeConfiguration(configJson); diff --git a/src/ZeroLog/Appenders/AppenderFactory.cs b/src/ZeroLog/Appenders/AppenderFactory.cs index d01901a5..2fea253b 100644 --- a/src/ZeroLog/Appenders/AppenderFactory.cs +++ b/src/ZeroLog/Appenders/AppenderFactory.cs @@ -1,6 +1,7 @@ using System; using System.Linq; -using Jil; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using ZeroLog.Config; namespace ZeroLog.Appenders @@ -40,11 +41,22 @@ public static IAppender CreateAppender(AppenderDefinition definition) .FirstOrDefault(x => x != null); } - internal static object GetAppenderParameters(AppenderDefinition definition, Type appenderParameterType) + internal static object? GetAppenderParameters(AppenderDefinition definition, Type appenderParameterType) { - var appenderParameterJson = JSON.SerializeDynamic(definition.AppenderJsonConfig); - var appenderParameters = (object)JSON.Deserialize(appenderParameterJson, appenderParameterType); - return appenderParameters; + switch (definition.AppenderJsonConfig) + { + case null: + return null; + + case JObject jObject: + var json = jObject.ToString(Formatting.None); + return JsonConvert.DeserializeObject(json, appenderParameterType); + + case object obj: + return appenderParameterType.IsInstanceOfType(obj) + ? obj + : null; + } } private static Type? GetAppenderParameterType(Type? appenderType) diff --git a/src/ZeroLog/Utils/JsonExtensions.cs b/src/ZeroLog/Utils/JsonExtensions.cs index 0d76cc86..0546f6cb 100644 --- a/src/ZeroLog/Utils/JsonExtensions.cs +++ b/src/ZeroLog/Utils/JsonExtensions.cs @@ -1,10 +1,10 @@ -using Jil; +using Newtonsoft.Json; namespace ZeroLog.Utils { public static class JsonExtensions { public static T DeserializeOrDefault(string? json, T @default) - => string.IsNullOrEmpty(json) ? @default : JSON.Deserialize(json); + => string.IsNullOrEmpty(json) ? @default : JsonConvert.DeserializeObject(json!); } } diff --git a/src/ZeroLog/ZeroLog.csproj b/src/ZeroLog/ZeroLog.csproj index ac625f9c..345637ad 100644 --- a/src/ZeroLog/ZeroLog.csproj +++ b/src/ZeroLog/ZeroLog.csproj @@ -12,8 +12,9 @@ - + + From 435d7f155e84bf3a0df250a351f56053c77d4918 Mon Sep 17 00:00:00 2001 From: Lucas Trzesniewski Date: Thu, 9 Jul 2020 11:46:23 +0200 Subject: [PATCH 3/3] Update dependencies --- src/Directory.Build.props | 2 +- src/ZeroLog.Benchmarks/FodyWeavers.xsd | 24 +++++++++++++++++++ .../ZeroLog.Benchmarks.csproj | 5 ++-- src/ZeroLog/FodyWeavers.xsd | 24 +++++++++++++++++++ src/ZeroLog/ZeroLog.csproj | 5 ++-- 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 81cace4f..7baeebb2 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -33,7 +33,7 @@ - + diff --git a/src/ZeroLog.Benchmarks/FodyWeavers.xsd b/src/ZeroLog.Benchmarks/FodyWeavers.xsd index 9d212489..15adf790 100644 --- a/src/ZeroLog.Benchmarks/FodyWeavers.xsd +++ b/src/ZeroLog.Benchmarks/FodyWeavers.xsd @@ -35,6 +35,30 @@ + + + Defines how warnings should be handled. Default value: Warnings + + + + + + Emit build warnings (this is the default). + + + + + Do not emit warnings. + + + + + Treat warnings as errors. + + + + + diff --git a/src/ZeroLog.Benchmarks/ZeroLog.Benchmarks.csproj b/src/ZeroLog.Benchmarks/ZeroLog.Benchmarks.csproj index 012b5f64..2f5ca8d8 100644 --- a/src/ZeroLog.Benchmarks/ZeroLog.Benchmarks.csproj +++ b/src/ZeroLog.Benchmarks/ZeroLog.Benchmarks.csproj @@ -14,12 +14,11 @@ - - + + - diff --git a/src/ZeroLog/FodyWeavers.xsd b/src/ZeroLog/FodyWeavers.xsd index 9d212489..15adf790 100644 --- a/src/ZeroLog/FodyWeavers.xsd +++ b/src/ZeroLog/FodyWeavers.xsd @@ -35,6 +35,30 @@ + + + Defines how warnings should be handled. Default value: Warnings + + + + + + Emit build warnings (this is the default). + + + + + Do not emit warnings. + + + + + Treat warnings as errors. + + + + + diff --git a/src/ZeroLog/ZeroLog.csproj b/src/ZeroLog/ZeroLog.csproj index 345637ad..176acc11 100644 --- a/src/ZeroLog/ZeroLog.csproj +++ b/src/ZeroLog/ZeroLog.csproj @@ -10,12 +10,11 @@ - - + + -