Skip to content

Commit 2d2447d

Browse files
Merge pull request #207 from Stravaig-Projects/#177/blank-entry-at-strat
177 Blank entry at the start causes subsequent entries to be skewed
2 parents 3f6740a + 109618e commit 2d2447d

File tree

6 files changed

+120
-16
lines changed

6 files changed

+120
-16
lines changed

release-notes/wip-release-notes.md

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@ Date: ???
66

77
### Bugs
88

9-
### Features
10-
11-
### Miscellaneous
12-
13-
### Dependent Packages
14-
15-
- .NET 6.0
16-
- No changes
17-
- .NET 5.0
18-
- No changes
19-
- .NET Core 3.1
20-
- No changes
21-
- General
22-
- No changes
23-
24-
---
9+
- #177 : Fix serilog issue where it skews the config keys and values when there is a config key with an empty top-level path element.
2510

2611

src/Stravaig.Configuration.Diagnostics.AdHocTests/Stravaig.Configuration.Diagnostics.AdHocTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
<RootNamespace>Stravaig.Extensions.Configuration.Diagnostics.AdHocTests</RootNamespace>
7+
<LangVersion>10</LangVersion>
78
</PropertyGroup>
89

910
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">

src/Stravaig.Configuration.Diagnostics.Core/Renderers/StructuredConfigurationKeyRenderer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public MessageEntry Render(IConfiguration configuration, ConfigurationDiagnostic
2828

2929
foreach (var kvp in configKeyValues)
3030
{
31+
if (string.IsNullOrWhiteSpace(kvp.Key))
32+
continue;
3133
string configurationKeyPlaceholder = Placeholder(kvp.Key);
3234
string potentiallyObfuscatedValue = Obfuscate(kvp, options);
3335

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.Extensions.Configuration;
5+
using NUnit.Framework;
6+
using Shouldly;
7+
using Stravaig.Configuration.Diagnostics.Logging;
8+
using Stravaig.Extensions.Logging.Diagnostics.Render;
9+
10+
namespace Stravaig.Extensions.Configuration.Diagnostics.Tests.RegressionTests;
11+
12+
[TestFixture]
13+
public class LoggingEmptyKeyConfigTests : LoggerExtensionsTestBase
14+
{
15+
[Test]
16+
public void TwoEmptyKeys()
17+
{
18+
SetupConfig(c =>
19+
{
20+
c.AddInMemoryCollection(new[]
21+
{
22+
new KeyValuePair<string, string>(":EmptyTopLevel", "The top level key is empty"),
23+
new KeyValuePair<string, string>("A:Regular:Key", "This is a normal key"),
24+
});
25+
});
26+
27+
Console.WriteLine("---Enumerate over the config values---");
28+
var configValues = ConfigRoot.AsEnumerable()
29+
.OrderBy(e => e.Key)
30+
.ToArray();
31+
foreach (var configElement in configValues)
32+
Console.WriteLine($"[{configElement.Key}] = \"{configElement.Value}\"");
33+
34+
Console.WriteLine("---Log output---");
35+
SetupLogger();
36+
Logger.LogConfigurationValuesAsInformation(ConfigRoot);
37+
38+
Console.WriteLine("---Captured log output---");
39+
var logs = GetLogs();
40+
logs.RenderLogs(Formatter.SimpleBySequence, Console.WriteLine);
41+
42+
logs.Count.ShouldBe(1);
43+
var log = logs[0];
44+
var properties = log.PropertyDictionary;
45+
properties["_EmptyTopLevel"].ShouldBe("The top level key is empty");
46+
properties["A_Regular_Key"].ShouldBe("This is a normal key");
47+
var nullProperties = properties
48+
.Where(p => p.Key is not ("_EmptyTopLevel" or "A_Regular_Key" or "{OriginalFormat}"))
49+
.ToArray();
50+
51+
#if NET6_0_OR_GREATER
52+
foreach(var nullProperty in nullProperties)
53+
nullProperty.Value.ShouldBeNull($"Property \"{nullProperty.Key}\" should have a null value.");
54+
#else
55+
foreach(var nullProperty in nullProperties)
56+
nullProperty.Value.ShouldBe("(null)", $"Property \"{nullProperty.Key}\" should have a dummy null value.");
57+
#endif
58+
}
59+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.Extensions.Configuration;
5+
using NUnit.Framework;
6+
using Shouldly;
7+
using Stravaig.Configuration.Diagnostics.Serilog;
8+
9+
namespace Stravaig.Extensions.Configuration.Diagnostics.Tests.RegressionTests;
10+
11+
[TestFixture]
12+
public class SerilogEmptyKeyConfigTests : SerilogTestBase
13+
{
14+
[Test]
15+
public void TwoEmptyKeys()
16+
{
17+
SetupConfig(c =>
18+
{
19+
c.AddInMemoryCollection(new[]
20+
{
21+
new KeyValuePair<string, string>(":EmptyTopLevel", "The top level key is empty"),
22+
new KeyValuePair<string, string>("A:Regular:Key", "This is a normal key"),
23+
});
24+
});
25+
26+
Console.WriteLine("---Enumerate over the config values---");
27+
var configValues = ConfigRoot.AsEnumerable()
28+
.OrderBy(e => e.Key)
29+
.ToArray();
30+
foreach (var configElement in configValues)
31+
Console.WriteLine($"[{configElement.Key}] = \"{configElement.Value}\"");
32+
33+
Console.WriteLine("---Log output---");
34+
SetupLogger(true);
35+
Logger.LogConfigurationValuesAsInformation(ConfigRoot);
36+
37+
Console.WriteLine("---JSON log output---");
38+
var jsonLogs = GetLogs();
39+
foreach (var logEntry in jsonLogs)
40+
Console.WriteLine(logEntry);
41+
42+
jsonLogs.Count.ShouldBe(1);
43+
var log = jsonLogs[0];
44+
var properties = log["Properties"];
45+
properties.Value<string>("_EmptyTopLevel").ShouldBe("The top level key is empty");
46+
properties.Value<string>("A_Regular_Key").ShouldBe("This is a normal key");
47+
48+
var nullProperties = properties
49+
.Select(p => p.Path.Split('.').Last())
50+
.Where(p => p is not ("_EmptyTopLevel" or "A_Regular_Key"))
51+
.ToArray();
52+
53+
foreach (var nullProperty in nullProperties)
54+
properties.Value<string>(nullProperty).ShouldBeNull($"Property \"{nullProperty}\" should have a null value.");
55+
}
56+
}

src/Stravaig.Configuration.Diagnostics.Tests/Stravaig.Configuration.Diagnostics.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
<RootNamespace>Stravaig.Extensions.Configuration.Diagnostics.Tests</RootNamespace>
7+
<LangVersion>10</LangVersion>
78
</PropertyGroup>
89

910
<ItemGroup>

0 commit comments

Comments
 (0)