-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from nblumhardt/dev
Fixes #41 - level switch behaviour when server sends no level
- Loading branch information
Showing
7 changed files
with
206 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Serilog.Sinks.Seq Copyright 2016 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Sinks.Seq | ||
{ | ||
/// <summary> | ||
/// Instances of this type are single-threaded, generally only updated on a background | ||
/// timer thread. An exception is <see cref="IsIncluded(LogEvent)"/>, which may be called | ||
/// concurrently but performs no synchronization. | ||
/// </summary> | ||
class ControlledLevelSwitch | ||
{ | ||
// If non-null, then background level checks will be performed; set either through the constructor | ||
// or in response to a level specification from the server. Never set to null after being made non-null. | ||
LoggingLevelSwitch _controlledSwitch; | ||
LogEventLevel? _originalLevel; | ||
|
||
public ControlledLevelSwitch(LoggingLevelSwitch controlledSwitch = null) | ||
{ | ||
_controlledSwitch = controlledSwitch; | ||
} | ||
|
||
public bool IsActive => _controlledSwitch != null; | ||
|
||
public bool IsIncluded(LogEvent evt) | ||
{ | ||
// Concurrent, but not synchronized. | ||
var controlledSwitch = _controlledSwitch; | ||
return controlledSwitch == null || | ||
(int)controlledSwitch.MinimumLevel <= (int)evt.Level; | ||
} | ||
|
||
public void Update(LogEventLevel? minimumAcceptedLevel) | ||
{ | ||
if (minimumAcceptedLevel == null) | ||
{ | ||
if (_controlledSwitch != null && _originalLevel.HasValue) | ||
_controlledSwitch.MinimumLevel = _originalLevel.Value; | ||
|
||
return; | ||
} | ||
|
||
if (_controlledSwitch == null) | ||
{ | ||
// The server is controlling the logging level, but not the overall logger. Hence, if the server | ||
// stops controlling the level, the switch should become transparent. | ||
_originalLevel = LevelAlias.Minimum; | ||
_controlledSwitch = new LoggingLevelSwitch(minimumAcceptedLevel.Value); | ||
return; | ||
} | ||
|
||
if (!_originalLevel.HasValue) | ||
_originalLevel = _controlledSwitch.MinimumLevel; | ||
|
||
_controlledSwitch.MinimumLevel = minimumAcceptedLevel.Value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
test/Serilog.Sinks.Seq.Tests/ControlledLevelSwitchTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
using Serilog.Sinks.Seq.Tests.Support; | ||
using Xunit; | ||
|
||
namespace Serilog.Sinks.Seq.Tests | ||
{ | ||
public class ControlledLevelSwitchTests | ||
{ | ||
[Fact] | ||
public void WhenTheServerSendsALevelTheSwitchIsAdjusted() | ||
{ | ||
var lls = new LoggingLevelSwitch(LogEventLevel.Warning); | ||
var cls = new ControlledLevelSwitch(lls); | ||
cls.Update(LogEventLevel.Debug); | ||
Assert.Equal(LogEventLevel.Debug, lls.MinimumLevel); | ||
} | ||
|
||
[Fact] | ||
public void WhenTheServerSendsNoLevelTheSwitchIsNotInitiallyAdjusted() | ||
{ | ||
var lls = new LoggingLevelSwitch(LogEventLevel.Warning); | ||
lls.MinimumLevel = LogEventLevel.Fatal; | ||
var cls = new ControlledLevelSwitch(lls); | ||
cls.Update(null); | ||
Assert.Equal(LogEventLevel.Fatal, lls.MinimumLevel); | ||
} | ||
|
||
[Fact] | ||
public void WhenTheServerSendsNoLevelTheSwitchIsResetIfPreviouslyAdjusted() | ||
{ | ||
var lls = new LoggingLevelSwitch(LogEventLevel.Warning); | ||
var cls = new ControlledLevelSwitch(lls); | ||
cls.Update(LogEventLevel.Information); | ||
cls.Update(null); | ||
Assert.Equal(LogEventLevel.Warning, lls.MinimumLevel); | ||
} | ||
|
||
[Fact] | ||
public void WithNoSwitchToControlAllEventsAreIncluded() | ||
{ | ||
var cls = new ControlledLevelSwitch(null); | ||
Assert.True(cls.IsIncluded(Some.DebugEvent())); | ||
} | ||
|
||
[Fact] | ||
public void WithNoSwitchToControlEventsAreStillFiltered() | ||
{ | ||
var cls = new ControlledLevelSwitch(null); | ||
cls.Update(LogEventLevel.Warning); | ||
Assert.True(cls.IsIncluded(Some.ErrorEvent())); | ||
Assert.False(cls.IsIncluded(Some.InformationEvent())); | ||
} | ||
|
||
[Fact] | ||
public void WithNoSwitchToControlAllEventsAreIncludedAfterReset() | ||
{ | ||
var cls = new ControlledLevelSwitch(null); | ||
cls.Update(LogEventLevel.Warning); | ||
cls.Update(null); | ||
Assert.True(cls.IsIncluded(Some.DebugEvent())); | ||
} | ||
|
||
[Fact] | ||
public void WhenControllingASwitchTheControllerIsActive() | ||
{ | ||
var cls = new ControlledLevelSwitch(new LoggingLevelSwitch()); | ||
Assert.True(cls.IsActive); | ||
} | ||
|
||
[Fact] | ||
public void WhenNotControllingASwitchTheControllerIsNotActive() | ||
{ | ||
var cls = new ControlledLevelSwitch(); | ||
Assert.False(cls.IsActive); | ||
} | ||
|
||
[Fact] | ||
public void AfterServerControlhTheControllerIsAlwaysActive() | ||
{ | ||
var cls = new ControlledLevelSwitch(); | ||
|
||
cls.Update(LogEventLevel.Information); | ||
Assert.True(cls.IsActive); | ||
|
||
cls.Update(null); | ||
Assert.True(cls.IsActive); | ||
} | ||
} | ||
} |
Oops, something went wrong.