Skip to content

Commit 9ca765f

Browse files
(#177) Fix misaligned keys and values
1 parent 41b18da commit 9ca765f

File tree

4 files changed

+116
-33
lines changed

4 files changed

+116
-33
lines changed

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

src/Stravaig.Configuration.Diagnostics.Tests/RegressionTests/EmptyKeyConfigTests.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
foreach(var nullProperty in nullProperties)
51+
nullProperty.Value.ShouldBe("(null)", $"Property \"{nullProperty.Key}\" should have a dummy null value.");
52+
}
53+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.Extensions.Configuration;
5+
using Newtonsoft.Json.Linq;
6+
using NUnit.Framework;
7+
using Shouldly;
8+
using Stravaig.Configuration.Diagnostics.Serilog;
9+
10+
namespace Stravaig.Extensions.Configuration.Diagnostics.Tests.RegressionTests;
11+
12+
[TestFixture]
13+
public class SerilogEmptyKeyConfigTests : SerilogTestBase
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(true);
36+
Logger.LogConfigurationValuesAsInformation(ConfigRoot);
37+
38+
Console.WriteLine("---JSON log output---");
39+
var jsonLogs = GetLogs();
40+
foreach (var logEntry in jsonLogs)
41+
Console.WriteLine(logEntry);
42+
43+
jsonLogs.Count.ShouldBe(1);
44+
var log = jsonLogs[0];
45+
var properties = log["Properties"];
46+
properties.Value<string>("_EmptyTopLevel").ShouldBe("The top level key is empty");
47+
properties.Value<string>("A_Regular_Key").ShouldBe("This is a normal key");
48+
49+
var nullProperties = properties
50+
.Select(p => p.Path.Split('.').Last())
51+
.Where(p => p is not ("_EmptyTopLevel" or "A_Regular_Key"))
52+
.ToArray();
53+
foreach (var nullProperty in nullProperties)
54+
properties.Value<string>(nullProperty).ShouldBeNull($"Property \"{nullProperty}\" should have a null value.");
55+
// var nullProperties = properties.Children()
56+
// .Where(p => p.Key is not ("_EmptyTopLevel" or "A_Regular_Key" or "{OriginalFormat}"))
57+
// .ToArray();
58+
// foreach(var nullProperty in nullProperties)
59+
// nullProperty.Value.ShouldBe("(null)", $"Property \"{nullProperty.Key}\" should have a dummy null value.");
60+
}
61+
}

0 commit comments

Comments
 (0)