Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Jil with Newtonsoft.Json #39

Merged
merged 3 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" PrivateAssets="all" />
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
</ItemGroup>

Expand Down
24 changes: 24 additions & 0 deletions src/ZeroLog.Benchmarks/FodyWeavers.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="Warnings">
<xs:annotation>
<xs:documentation>Defines how warnings should be handled. Default value: Warnings</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Warnings">
<xs:annotation>
<xs:documentation>Emit build warnings (this is the default).</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="Ignore">
<xs:annotation>
<xs:documentation>Do not emit warnings.</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="Errors">
<xs:annotation>
<xs:documentation>Treat warnings as errors.</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:all>
Expand Down
5 changes: 2 additions & 3 deletions src/ZeroLog.Benchmarks/ZeroLog.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
<PackageReference Include="ConsoleTables" Version="2.3.0" />
<PackageReference Include="HdrHistogram" Version="2.5.0" />
<PackageReference Include="Fody" Version="6.0.5" PrivateAssets="all" />
<PackageReference Include="InlineIL.Fody" Version="1.3.4" PrivateAssets="all" />
<PackageReference Include="Fody" Version="6.2.0" PrivateAssets="all" />
<PackageReference Include="InlineIL.Fody" Version="1.4.2" PrivateAssets="all" />
<PackageReference Include="log4net" Version="2.0.8" />
<PackageReference Include="NLog" Version="4.6.8" />
<PackageReference Include="System.Net.Sockets" Version="4.3.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

</Project>
35 changes: 24 additions & 11 deletions src/ZeroLog.Tests/Config/ConfiguratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Linq;
using Jil;
using Newtonsoft.Json;
using NFluent;
using NUnit.Framework;
using ZeroLog.Appenders;
using ZeroLog.Config;
using ZeroLog.ConfigResolvers;

namespace ZeroLog.Tests.Config
{
Expand All @@ -14,8 +13,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,
Expand All @@ -24,13 +27,13 @@ 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 = JsonConvert.SerializeObject(config);

var loadedConfig = JsonConfigurator.DeserializeConfiguration(configJson);

Expand All @@ -42,11 +45,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]
Expand All @@ -68,7 +78,7 @@ public void should_handle_missing_part()
{
""Name"": ""B"",
""AppenderTypeName"": ""DateAndSizeRollingFileAppender"",
""AppenderJsonConfig"": ""{\""FilePathRoot\"":\""totopath \""}""
""AppenderJsonConfig"": {""FilePathRoot"":""totopath ""}
}
],

Expand All @@ -87,6 +97,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 ");
}
}
}
22 changes: 17 additions & 5 deletions src/ZeroLog/Appenders/AppenderFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using Jil;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ZeroLog.Config;

namespace ZeroLog.Appenders
Expand Down Expand Up @@ -40,11 +41,22 @@ 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);
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)
Expand Down
24 changes: 24 additions & 0 deletions src/ZeroLog/FodyWeavers.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="Warnings">
<xs:annotation>
<xs:documentation>Defines how warnings should be handled. Default value: Warnings</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Warnings">
<xs:annotation>
<xs:documentation>Emit build warnings (this is the default).</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="Ignore">
<xs:annotation>
<xs:documentation>Do not emit warnings.</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="Errors">
<xs:annotation>
<xs:documentation>Treat warnings as errors.</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:all>
Expand Down
4 changes: 2 additions & 2 deletions src/ZeroLog/Utils/JsonExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Jil;
using Newtonsoft.Json;

namespace ZeroLog.Utils
{
public static class JsonExtensions
{
public static T DeserializeOrDefault<T>(string? json, T @default)
=> string.IsNullOrEmpty(json) ? @default : JSON.Deserialize<T>(json);
=> string.IsNullOrEmpty(json) ? @default : JsonConvert.DeserializeObject<T>(json!);
}
}
8 changes: 4 additions & 4 deletions src/ZeroLog/ZeroLog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Fody" Version="6.0.5" PrivateAssets="all" />
<PackageReference Include="InlineIL.Fody" Version="1.3.4" PrivateAssets="all" />
<PackageReference Include="Jil" Version="2.15.4" />
<PackageReference Include="Fody" Version="6.2.0" PrivateAssets="all" />
<PackageReference Include="InlineIL.Fody" Version="1.4.2" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="StringFormatter" Version="1.0.0.13" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
Expand Down