Skip to content

Multiple value exception, fixes #103 #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMet
IConfigurationArgumentValue GetArgumentValue(IConfigurationSection argumentSection)
{
IConfigurationArgumentValue argumentValue;

// Reject configurations where an element has both scalar and complex
// values as a result of reading multiple configuration sources.
if (argumentSection.Value != null && argumentSection.GetChildren().Any())
throw new InvalidOperationException(
$"The value for the argument '{argumentSection.Path}' is assigned different value " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep thinking ArgumentException but I guess that's best reserved for actual method arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first inclination, too, but then I noticed IOE is used elsewhere in the code for similar "processing" errors.

"types in more than one configuration source. Ensure all configurations consistently " +
"use either a scalar (int, string, boolean) or a complex (array, section, list, " +
"POCO, etc.) type for this argument value.");

if (argumentSection.Value != null)
{
argumentValue = new StringArgumentValue(() => argumentSection.Value, argumentSection.GetReloadToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ namespace Serilog.Settings.Configuration.Tests
{
public class ConfigurationSettingsTests
{
static LoggerConfiguration ConfigFromJson(string jsonString)
static LoggerConfiguration ConfigFromJson(string jsonString, string secondJsonSource = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could possibly make this params array, but rule of threee

Copy link
Contributor Author

@MV10 MV10 Apr 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And YAGNI... I can't think of a test scenario where n config sources would do anything two sources can't represent.

{
var config = new ConfigurationBuilder().AddJsonString(jsonString).Build();
var builder = new ConfigurationBuilder().AddJsonString(jsonString);
if (secondJsonSource != null)
builder.AddJsonString(secondJsonSource);
var config = builder.Build();
return new LoggerConfiguration()
.ReadFrom.Configuration(config);
}
Expand All @@ -31,7 +34,7 @@ public void PropertyEnrichmentIsApplied()
}
}
}";

var log = ConfigFromJson(json)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();
Expand Down Expand Up @@ -114,7 +117,7 @@ public void AuditSinksAreConfigured()

var log = ConfigFromJson(json)
.CreateLogger();

DummyRollingFileSink.Emitted.Clear();
DummyRollingFileAuditSink.Emitted.Clear();

Expand Down Expand Up @@ -227,7 +230,7 @@ public void LoggingLevelSwitchWithInvalidNameThrowsFormatException()
""LevelSwitches"": {""switchNameNotStartingWithDollar"" : ""Warning"" }
}
}";

var ex = Assert.Throws<FormatException>(() => ConfigFromJson(json));

Assert.Contains("\"switchNameNotStartingWithDollar\"", ex.Message);
Expand Down Expand Up @@ -271,7 +274,7 @@ public void SettingMinimumLevelControlledByToAnUndeclaredSwitchThrows()
}
}
}";

var ex = Assert.Throws<InvalidOperationException>(() =>
ConfigFromJson(json)
.CreateLogger());
Expand Down Expand Up @@ -332,7 +335,7 @@ public void ReferencingAnUndeclaredSwitchInSinkThrows()
}]
}
}";

var ex = Assert.Throws<InvalidOperationException>(() =>
ConfigFromJson(json)
.CreateLogger());
Expand Down Expand Up @@ -544,8 +547,8 @@ public void WriteToSubLoggerWithLevelSwitchIsSupported()
}
}]
}
}";
}";

var log = ConfigFromJson(json)
.CreateLogger();

Expand All @@ -556,5 +559,43 @@ public void WriteToSubLoggerWithLevelSwitchIsSupported()

Assert.Equal(1, DummyRollingFileSink.Emitted.Count);
}

[Trait("Bugfix", "#103")]
[Fact]
public void InconsistentComplexVsScalarArgumentValuesThrowsIOE()
{
var jsonDiscreteValue = @"{
""Serilog"": {
""Using"": [""TestDummies""],
""WriteTo"": [{
""Name"": ""DummyRollingFile"",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rolling file is deprecated, maybe example can start using File ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, I see its used in the rest of the test codebase here; prob leave it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking as well.

""Args"": {""pathFormat"" : ""C:\\""}
}]
}
}";

var jsonComplexValue = @"{
""Serilog"": {
""Using"": [""TestDummies""],
""WriteTo"": [{
""Name"": ""DummyRollingFile"",
""Args"": {""pathFormat"" : { ""foo"" : ""bar"" } }
}]
}
}";

// These will combine into a ConfigurationSection object that has both
// Value == "C:\" and GetChildren() == List<string>. No configuration
// extension matching this exists (in theory an "object" argument could
// accept either value). ConfigurationReader should throw as soon as
// the multiple values are recognized; it will never attempt to locate
// a matching argument.

var ex = Assert.Throws<InvalidOperationException>(()
=> ConfigFromJson(jsonDiscreteValue, jsonComplexValue));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think => shoudl go prev line ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one line would be consistent with the rest of the code. Earlier you suggested the split (I tend to split them myself).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that the => rarely starts a line in normal code and instead goes on prev line
i.e. the full () => on prev line


Assert.Contains("The value for the argument", ex.Message);
Assert.Contains("'Serilog:WriteTo:0:Args:pathFormat'", ex.Message);
}
}
}