Skip to content

Commit 5a3f285

Browse files
#111: Write tests that demonstrate the bug
1 parent a7a3c96 commit 5a3f285

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.Extensions.Configuration;
4+
using NUnit.Framework;
5+
using Shouldly;
6+
using Stravaig.Configuration.Diagnostics.Logging;
7+
8+
namespace Stravaig.Extensions.Configuration.Diagnostics.Tests.RegressionTests
9+
{
10+
[TestFixture]
11+
public class DotsBugLoggerExtensionTests : LoggerExtensionsTestBase
12+
{
13+
[Test]
14+
public void ConfigElementsWithDotsShouldRenderPropertiesInMessage()
15+
{
16+
SetupConfig(c =>
17+
{
18+
c.AddInMemoryCollection(new[]
19+
{
20+
new KeyValuePair<string, string>("ItemA", "Alpha"),
21+
new KeyValuePair<string, string>("ItemB", "Beta"),
22+
new KeyValuePair<string, string>("ItemB.A", "Beta-Alpha"),
23+
new KeyValuePair<string, string>("ItemC", "Gamma"),
24+
});
25+
});
26+
SetupLogger();
27+
28+
Logger.LogConfigurationValuesAsInformation(ConfigRoot);
29+
30+
var logs = GetLogs();
31+
logs.Count.ShouldBe(1);
32+
var log = logs[0];
33+
log.FormattedMessage.ShouldContain("ItemC : Gamma");
34+
log.FormattedMessage.ShouldContain("ItemB.A : Beta-Alpha");
35+
log.FormattedMessage.ShouldContain("ItemB : Beta");
36+
log.FormattedMessage.ShouldContain("ItemA : Alpha");
37+
}
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.Extensions.Configuration;
4+
using Newtonsoft.Json.Linq;
5+
using NUnit.Framework;
6+
using Shouldly;
7+
using Stravaig.Configuration.Diagnostics.Serilog;
8+
using Stravaig.Extensions.Configuration.Diagnostics.Tests.__Extensions;
9+
10+
namespace Stravaig.Extensions.Configuration.Diagnostics.Tests.RegressionTests
11+
{
12+
[TestFixture]
13+
public class DotsBugSerilog : SerilogTestBase
14+
{
15+
[Test]
16+
public void ConfigElementsWithDotsShouldRenderPropertiesInMessage()
17+
{
18+
SetupConfig(c =>
19+
{
20+
c.AddInMemoryCollection(new[]
21+
{
22+
new KeyValuePair<string, string>("ItemA", "Alpha"),
23+
new KeyValuePair<string, string>("ItemB", "Beta"),
24+
new KeyValuePair<string, string>("ItemB.A", "Beta-Alpha"),
25+
new KeyValuePair<string, string>("ItemC", "Gamma"),
26+
});
27+
});
28+
SetupLogger(true);
29+
30+
Logger.LogConfigurationValuesAsInformation(ConfigRoot);
31+
32+
var logs = GetLogs();
33+
logs.Count.ShouldBe(1);
34+
var log = logs[0];
35+
36+
Console.WriteLine(log.GetMessage());
37+
Console.WriteLine(log.GetMessageTemplate());
38+
39+
log.GetMessage().ShouldContain("ItemC : \"Gamma\"");
40+
log.GetMessage().ShouldContain("ItemB.A : \"Beta-Alpha\"");
41+
log.GetMessage().ShouldContain("ItemB : \"Beta\"");
42+
log.GetMessage().ShouldContain("ItemA : \"Alpha\"");
43+
}
44+
}
45+
}

src/Stravaig.Configuration.Diagnostics.Tests/SerilogTestBase.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ public abstract class SerilogTestBase : TestBase
1616
protected ILogger Logger;
1717
private StringBuilder _logStringBuilder;
1818
private TextWriter _logTextWriter;
19-
protected void SetupLogger()
19+
protected void SetupLogger(bool writeToConsole = false)
2020
{
2121
_logStringBuilder = new StringBuilder();
2222
_logTextWriter = new StringWriter(_logStringBuilder);
23-
Logger = new LoggerConfiguration()
23+
var loggerConfiguration = new LoggerConfiguration()
2424
.WriteTo.TextWriter(new JsonFormatter(renderMessage: true), _logTextWriter)
25-
.MinimumLevel.Verbose()
25+
.MinimumLevel.Verbose();
26+
if (writeToConsole)
27+
loggerConfiguration = loggerConfiguration.WriteTo.Console();
28+
Logger = loggerConfiguration
2629
.CreateLogger();
2730
}
2831

0 commit comments

Comments
 (0)