Skip to content

Commit

Permalink
Added more tests for simple overrides w/ child loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
tsimbalar committed Sep 15, 2017
1 parent f292b59 commit 4d9a0f9
Showing 1 changed file with 80 additions and 1 deletion.
81 changes: 80 additions & 1 deletion test/Serilog.Tests/Core/ChildLoggerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Serilog.Events;
using Serilog.Core;
using Serilog.Core.Sinks;
using Serilog.Events;
using Serilog.Tests.Support;
using Xunit;
using static Serilog.Events.LogEventLevel;
Expand Down Expand Up @@ -168,5 +170,82 @@ public void WriteToLoggerMinimumLevelInheritanceScenarios(
Assert.Equal(eventShouldGetToChild, evt != null);
}



[Theory]
// Visualizing the pipeline from left to right ....
//
// Event --> Root Logger --> Child Logger -> YES or
// lvl override/lvl override/levl NO ?
//
// numbers are relative to incoming event level
// Information + 1 = Warning
// Information - 1 = Debug
//
// Incoming event is Information
// with SourceContext Root.N1.N2
//
// - default case - no overrides
[InlineData(null, 0, null, 0, true)]
// - root overrides with level lower or equal to event
// ... and child logger is out of the way
[InlineData("Root", +0, null, +0, true)]
[InlineData("Root", -1, null, +0, true)]
[InlineData("Root.N1", +0, null, +0, true)]
[InlineData("Root.N1", -1, null, +0, true)]
[InlineData("Root.N1.N2", +0, null, +0, true)]
[InlineData("Root.N1.N2", -1, null, +0, true)]
// - root overrides on irrelevant namespaces
[InlineData("xx", +1, null, +0, true)]
[InlineData("Root.xx", +1, null, +0, true)]
[InlineData("Root.N1.xx", +1, null, +0, true)]
// - root overrides prevent all processing from children
// even though children would happily accept it
[InlineData("Root", +1, null, +0, false)]
[InlineData("Root", +1, "Root", +0, false)]
[InlineData("Root.N1", +1, null, +0, false)]
[InlineData("Root.N1", +1, "Root.N1", +0, false)]
[InlineData("Root.N1.N2", +1, null, +0, false)]
[InlineData("Root.N1.N2", +1, "Root.N1.N2", +0, false)]
public void WriteToLoggerWithConfigCallbackMinimumLevelOverrideInheritanceScenarios(
string rootOverrideSource,
int rootOverrideLevelIncrement,
string childOverrideSource,
int childOverrideLevelIncrement,
bool eventShouldGetToChild)
{
var incomingEventLevel = Information;
var rootOverrideLevel = incomingEventLevel + rootOverrideLevelIncrement;
var childOverrideLevel = incomingEventLevel + childOverrideLevelIncrement;

LogEvent evt = null;
var sink = new DelegatingSink(e => evt = e);

var rootLoggerConfig = new LoggerConfiguration()
.MinimumLevel.Is(LevelAlias.Minimum);

if (rootOverrideSource != null)
{
rootLoggerConfig.MinimumLevel.Override(rootOverrideSource, rootOverrideLevel);
}

var logger = rootLoggerConfig
.WriteTo.Logger(lc =>
{
lc.MinimumLevel.Is(LevelAlias.Minimum);
if (childOverrideSource != null)
{
lc.MinimumLevel.Override(childOverrideSource, childOverrideLevel);
}
lc.WriteTo.Sink(sink);
})
.CreateLogger();

logger
.ForContext(Constants.SourceContextPropertyName, "Root.N1.N2")
.Write(Some.LogEvent(level: incomingEventLevel));

Assert.Equal(eventShouldGetToChild, evt != null);
}
}
}

0 comments on commit 4d9a0f9

Please sign in to comment.