Skip to content

Commit

Permalink
Merge pull request #1472 from skomis-mm/subMinLvl
Browse files Browse the repository at this point in the history
Don't inherit parent Minimum Level for inline sublogger configuration
  • Loading branch information
nblumhardt committed Aug 2, 2020
2 parents c10e3a8 + 89ec7f3 commit 308208e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/Serilog/Configuration/LoggerAuditSinkConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Serilog Contributors
// Copyright 2016-2020 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -25,9 +25,9 @@ public class LoggerAuditSinkConfiguration
{
readonly LoggerSinkConfiguration _sinkConfiguration;

internal LoggerAuditSinkConfiguration(LoggerConfiguration loggerConfiguration, Action<ILogEventSink> addSink, Action<LoggerConfiguration> applyInheritedConfiguration)
internal LoggerAuditSinkConfiguration(LoggerConfiguration loggerConfiguration, Action<ILogEventSink> addSink)
{
_sinkConfiguration = new LoggerSinkConfiguration(loggerConfiguration, addSink, applyInheritedConfiguration);
_sinkConfiguration = new LoggerSinkConfiguration(loggerConfiguration, addSink);
}

/// <summary>
Expand Down
13 changes: 4 additions & 9 deletions src/Serilog/Configuration/LoggerSinkConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2015 Serilog Contributors
// Copyright 2013-2020 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,13 +30,11 @@ public class LoggerSinkConfiguration
{
readonly LoggerConfiguration _loggerConfiguration;
readonly Action<ILogEventSink> _addSink;
readonly Action<LoggerConfiguration> _applyInheritedConfiguration;

internal LoggerSinkConfiguration(LoggerConfiguration loggerConfiguration, Action<ILogEventSink> addSink, Action<LoggerConfiguration> applyInheritedConfiguration)
internal LoggerSinkConfiguration(LoggerConfiguration loggerConfiguration, Action<ILogEventSink> addSink)
{
_loggerConfiguration = loggerConfiguration ?? throw new ArgumentNullException(nameof(loggerConfiguration));
_addSink = addSink ?? throw new ArgumentNullException(nameof(addSink));
_applyInheritedConfiguration = applyInheritedConfiguration ?? throw new ArgumentNullException(nameof(applyInheritedConfiguration));
}

/// <summary>
Expand Down Expand Up @@ -126,9 +124,7 @@ internal LoggerSinkConfiguration(LoggerConfiguration loggerConfiguration, Action
{
if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger));

var lc = new LoggerConfiguration();

_applyInheritedConfiguration(lc);
var lc = new LoggerConfiguration().MinimumLevel.Is(LevelAlias.Minimum);
configureLogger(lc);

var subLogger = lc.CreateLogger();
Expand Down Expand Up @@ -237,8 +233,7 @@ public LoggerConfiguration Conditional(Func<LogEvent, bool> condition, Action<Lo
var capturingConfiguration = new LoggerConfiguration();
var capturingLoggerSinkConfiguration = new LoggerSinkConfiguration(
capturingConfiguration,
sinksToWrap.Add,
loggerSinkConfiguration._applyInheritedConfiguration);
sinksToWrap.Add);

// `WriteTo.Sink()` will return the capturing configuration; this ensures chained `WriteTo` gets back
// to the capturing sink configuration, enabling `WriteTo.X().WriteTo.Y()`.
Expand Down
14 changes: 3 additions & 11 deletions src/Serilog/LoggerConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2016 Serilog Contributors
// Copyright 2013-2020 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,18 +48,10 @@ public class LoggerConfiguration
/// </summary>
public LoggerConfiguration()
{
WriteTo = new LoggerSinkConfiguration(this, s => _logEventSinks.Add(s), ApplyInheritedConfiguration);
WriteTo = new LoggerSinkConfiguration(this, s => _logEventSinks.Add(s));
Enrich = new LoggerEnrichmentConfiguration(this, e => _enrichers.Add(e));
}

void ApplyInheritedConfiguration(LoggerConfiguration child)
{
if (_levelSwitch != null)
child.MinimumLevel.ControlledBy(_levelSwitch);
else
child.MinimumLevel.Is(_minimumLevel);
}

/// <summary>
/// Configures the sinks that log events will be emitted to.
/// </summary>
Expand All @@ -76,7 +68,7 @@ void ApplyInheritedConfiguration(LoggerConfiguration child)
/// extending <see cref="LoggerAuditSinkConfiguration"/>, though the generic <see cref="LoggerAuditSinkConfiguration.Sink"/>
/// method allows any sink class to be adapted for auditing.
/// </remarks>
public LoggerAuditSinkConfiguration AuditTo => new LoggerAuditSinkConfiguration(this, s => _auditSinks.Add(s), ApplyInheritedConfiguration);
public LoggerAuditSinkConfiguration AuditTo => new LoggerAuditSinkConfiguration(this, s => _auditSinks.Add(s));

/// <summary>
/// Configures the minimum level at which events will be passed to sinks. If
Expand Down
6 changes: 4 additions & 2 deletions test/Serilog.Tests/Context/LogContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,15 @@ public async Task ContextPropertiesCrossAsyncCalls()
{
var pre = Thread.CurrentThread.ManagedThreadId;
await Task.Delay(1000);
await Task.Yield();
var post = Thread.CurrentThread.ManagedThreadId;
log.Write(Some.InformationEvent());
Assert.Equal(1, lastEvent.Properties["A"].LiteralValue());
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
Assert.True(Thread.CurrentThread.IsBackground);
Assert.NotEqual(pre, post);
}
},
Expand Down Expand Up @@ -404,6 +406,6 @@ public bool IsCallable()

class ForceNewThreadSyncContext : SynchronizationContext
{
public override void Post(SendOrPostCallback d, object state) => new Thread(x => d(x)).Start(state);
public override void Post(SendOrPostCallback d, object state) => new Thread(x => d(x)) { IsBackground = true }.Start(state);
}
}
16 changes: 10 additions & 6 deletions test/Serilog.Tests/Core/ChildLoggerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Serilog.Core;
using Serilog.Events;
using Serilog.Tests.Support;
Expand Down Expand Up @@ -149,9 +149,9 @@ public static IEnumerable<object[]> GetMinimumLevelOverrideInheritanceTestCases(
// Event --> Root Logger --> Child Logger -> YES or
// lvl override/lvl override/levl NO ?
//
object[] T(string rs, int? rl, string cs, int? cl, bool r)
object[] T(string rs, int? rl, string cs, int? cl, bool r, LogEventLevel dl = LevelAlias.Minimum)
{
return new object[] { rs, rl, cs, cl, r };
return new object[] { dl, rs, rl, cs, cl, r };
}
// numbers are relative to incoming event level
// Information + 1 = Warning
Expand All @@ -166,10 +166,13 @@ object[] T(string rs, int? rl, string cs, int? cl, bool r)
// ... and child logger is out of the way
yield return T("Root", +0, null, +0, true);
yield return T("Root", -1, null, +0, true);
yield return T("Root", -1, null, +0, true, LevelAlias.Maximum);
yield return T("Root.N1", +0, null, +0, true);
yield return T("Root.N1", -1, null, +0, true);
yield return T("Root.N1", -1, null, +0, true, LevelAlias.Maximum);
yield return T("Root.N1.N2", +0, null, +0, true);
yield return T("Root.N1.N2", -1, null, +0, true);
yield return T("Root.N1.N2", -1, null, +0, true, LevelAlias.Maximum);
// - root overrides on irrelevant namespaces
yield return T("xx", +1, null, +0, true);
yield return T("Root.xx", +1, null, +0, true);
Expand All @@ -191,6 +194,7 @@ object[] T(string rs, int? rl, string cs, int? cl, bool r)
[Theory]
[MemberData(nameof(GetMinimumLevelOverrideInheritanceTestCases))]
public void WriteToLoggerWithConfigCallbackMinimumLevelOverrideInheritanceScenarios(
LogEventLevel defaultRootLevel,
string rootOverrideSource,
int rootOverrideLevelIncrement,
string childOverrideSource,
Expand All @@ -205,7 +209,7 @@ object[] T(string rs, int? rl, string cs, int? cl, bool r)
var sink = new DelegatingSink(e => evt = e);

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

if (rootOverrideSource != null)
{
Expand All @@ -215,7 +219,6 @@ object[] T(string rs, int? rl, string cs, int? cl, bool r)
var logger = rootLoggerConfig
.WriteTo.Logger(lc =>
{
lc.MinimumLevel.Is(LevelAlias.Minimum);
if (childOverrideSource != null)
{
lc.MinimumLevel.Override(childOverrideSource, childOverrideLevel);
Expand All @@ -241,6 +244,7 @@ object[] T(string rs, int? rl, string cs, int? cl, bool r)
[Theory]
[MemberData(nameof(GetMinimumLevelOverrideInheritanceTestCases))]
public void WriteToLoggerMinimumLevelOverrideInheritanceScenarios(
LogEventLevel defaultRootLevel,
string rootOverrideSource,
int rootOverrideLevelIncrement,
string childOverrideSource,
Expand All @@ -264,7 +268,7 @@ object[] T(string rs, int? rl, string cs, int? cl, bool r)
var childLogger = childLoggerConfig.CreateLogger();

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

if (rootOverrideSource != null)
{
Expand Down

0 comments on commit 308208e

Please sign in to comment.