Skip to content

Commit

Permalink
Make ReadFrom.KeyValuePairs not throw when duplicate keys
Browse files Browse the repository at this point in the history
Instead, take the last value of each key, to remove work from future Serilog.Settings.Combined
  • Loading branch information
Thibaud DESODT authored and tsimbalar committed Nov 8, 2017
1 parent a80ea3e commit 64e45ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
12 changes: 11 additions & 1 deletion src/Serilog/Configuration/LoggerSettingsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,21 @@ public LoggerConfiguration Settings(ILoggerSettings settings)
/// </summary>
/// <param name="settings">A list of key-value pairs describing logger settings.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>In case of duplicate keys, the last value for the key is kept and the previous ones are ignored.</remarks>
public LoggerConfiguration KeyValuePairs(IEnumerable<KeyValuePair<string, string>> settings)
{
if (settings == null) throw new ArgumentNullException(nameof(settings));
var uniqueSettings = new Dictionary<string, string>();
foreach (var kvp in settings)
{
uniqueSettings[kvp.Key] = kvp.Value;
}
return KeyValuePairs(uniqueSettings);
}

return Settings(new KeyValuePairSettings(settings.ToDictionary(x => x.Key, x => x.Value)));
LoggerConfiguration KeyValuePairs(IReadOnlyDictionary<string, string> settings)
{
return Settings(new KeyValuePairSettings(settings));
}
}
}
25 changes: 15 additions & 10 deletions test/Serilog.Tests/Settings/KeyValuePairSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ namespace Serilog.Tests.Settings
public class KeyValuePairSettingsTests
{
[Fact]
public void DuplicateKeysCauseArgumentException()
public void LastValueIsTakenWhenKeysAreDuplicate()
{
Action action = () => new LoggerConfiguration()
LogEvent evt = null;
var log = new LoggerConfiguration()
.ReadFrom.KeyValuePairs(new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("setting", "Value"),
new KeyValuePair<string, string>("setting", "SameSettingOtherValue"),
});
new KeyValuePair<string, string>("enrich:with-property:App", "InitialValue"),
new KeyValuePair<string, string>("enrich:with-property:App", "OverridenValue"),
new KeyValuePair<string, string>("enrich:with-property:App", "FinalValue")
})
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

var ex = Assert.ThrowsAny<ArgumentException>(action);
Assert.NotNull(ex);
Assert.Contains("An item with the same key has already been added.", ex.Message);
}
log.Information("Has a test property");

Assert.NotNull(evt);
Assert.Equal("FinalValue", evt.Properties["App"].LiteralValue());
}

[Fact]
public void FindsConfigurationAssemblies()
Expand Down Expand Up @@ -328,7 +333,7 @@ public void LoggingLevelSwitchCanBeUsedForMinimumLevelOverrides()
systemLogger.Write(Some.WarningEvent());
Assert.False(evt is null, "LoggingLevelSwitch initial level was Warning for logger System.*. It should log Warning messages for SourceContext System.Bar");


evt = null;
var controlSwitch = DummyWithLevelSwitchSink.ControlLevelSwitch;

Expand Down

0 comments on commit 64e45ab

Please sign in to comment.