Skip to content

Commit 266e41c

Browse files
Merge pull request #76 from Stravaig-Projects/#75-all-connection-string-keys
Make structured connection string keys more concise
2 parents daf8a23 + 7a0db37 commit 266e41c

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

release-notes/wip-release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Date: ???
1212
* #35: Create Func matcher. Can now specify a predicate function that matches the key of secrets.
1313
* #42: Logging all connection strings now does so in a single log message.
1414
* #43: All logging is now using structured renderers.
15+
* #75: Make the connection string structured renderer keys more concise.
1516

1617
### Miscellaneous
1718

src/Stravaig.Extensions.Configuration.Diagnostics.Tests/ConnectionStringLogTests.cs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,37 @@ public void LogAllConnectionStringsWithOneDodgyConnectionString(LogLevel level)
173173
[Test]
174174
[TestCaseSource(typeof(LogLevelSource))]
175175
public void LogAllConnectionStringsGoodConnectionString(LogLevel level)
176+
{
177+
var options = SetupGoodConnectionStringTest();
178+
Logger.LogAllConnectionStrings(ConfigRoot, level, options);
179+
ValidateGoodConnectionStringTest(level);
180+
}
181+
182+
[Test]
183+
public void LogAllConnectionStringsAsInformationGoodConnectionString()
184+
{
185+
var options = SetupGoodConnectionStringTest();
186+
Logger.LogAllConnectionStringsAsInformation(ConfigRoot, options);
187+
ValidateGoodConnectionStringTest(LogLevel.Information);
188+
}
189+
190+
[Test]
191+
public void LogAllConnectionStringsAsDebugGoodConnectionString()
192+
{
193+
var options = SetupGoodConnectionStringTest();
194+
Logger.LogAllConnectionStringsAsDebug(ConfigRoot, options);
195+
ValidateGoodConnectionStringTest(LogLevel.Debug);
196+
}
197+
198+
[Test]
199+
public void LogAllConnectionStringsAsTraceGoodConnectionString()
200+
{
201+
var options = SetupGoodConnectionStringTest();
202+
Logger.LogAllConnectionStringsAsTrace(ConfigRoot, options);
203+
ValidateGoodConnectionStringTest(LogLevel.Trace);
204+
}
205+
206+
private ConfigurationDiagnosticsOptions SetupGoodConnectionStringTest()
176207
{
177208
SetupConfig(builder =>
178209
{
@@ -189,20 +220,26 @@ public void LogAllConnectionStringsGoodConnectionString(LogLevel level)
189220
});
190221
});
191222
var options = SetupOptions();
192-
Logger.LogAllConnectionStrings(ConfigRoot, level, options);
223+
return options;
224+
}
225+
226+
private void ValidateGoodConnectionStringTest(LogLevel level)
227+
{
193228
var logs = GetLogs();
194229
logs.Count.ShouldBe(1);
195230
Console.WriteLine(logs[0].OriginalMessage);
196231
logs[0].LogLevel.ShouldBe(level);
197232
logs[0].FormattedMessage.ShouldContain("The following connection strings were found");
198233
logs[0].FormattedMessage.ShouldContain(SqlServerTrustedSecurityKey);
199234
logs[0].FormattedMessage.ShouldContain(SqlServerStandardSecurityKey);
200-
201-
logs[0].OriginalMessage.ShouldContain("The following connection strings were found: {preamble_SqlServerStandardSecurity_name}, {preamble_SqlServerTrustedSecurity_name}.");
235+
236+
logs[0].OriginalMessage
237+
.ShouldContain(
238+
"The following connection strings were found: {preamble_SqlServerStandardSecurity_name}, {preamble_SqlServerTrustedSecurity_name}.");
202239
logs[0].OriginalMessage.ShouldContain("Connection string (named {SqlServerTrustedSecurity_name}) parameters:");
203240
logs[0].OriginalMessage.ShouldContain("Connection string (named {SqlServerStandardSecurity_name}) parameters:");
204241
}
205-
242+
206243
[Test]
207244
[TestCaseSource(typeof(LogLevelSource))]
208245
public void LogAllConnectionStringsWhenThereAreNone(LogLevel level)

src/Stravaig.Extensions.Configuration.Diagnostics.Tests/RegressionTests/ConnectionStringLoggingWithSerilogTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ public void KeyValuesRenderedInMessageCorrectly()
3838

3939

4040
var properties = (JObject) logObject["Properties"];
41-
properties.ShouldContainKey("Simple_database_key");
42-
properties.ShouldContainKey("Simple_database_value");
43-
properties.ShouldContainKey("Simple_server_key");
44-
properties.ShouldContainKey("Simple_server_value");
41+
properties.ShouldContainKey("Simple_database");
42+
properties.ShouldContainKey("Simple_server");
4543
}
4644

4745
private static ILogger<ConnectionStringLoggingWithSerilogTests> SetupLogger(StringWriter writer)

src/Stravaig.Extensions.Configuration.Diagnostics/Renderers/StructuredConnectionStringRenderer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ private void AddConnectionStringKeysAndValues(
9292
? options.Obfuscator.Obfuscate((string) builder[key])
9393
: (string) builder[key];
9494

95-
messageTemplate.AppendLine($" * {Placeholder(name, key, nameof(key))} = {Placeholder(name, key, nameof(value))}");
96-
args.Add(key);
95+
messageTemplate.AppendLine($" * [{name}].[{key}] = {Placeholder(name, key)}");
9796
args.Add(value);
9897
}
9998
}

0 commit comments

Comments
 (0)