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 #160 from skomis-mm/issue99
Respect dynamic logging level changes for LevelSwitch section
- Loading branch information
Showing
9 changed files
with
183 additions
and
63 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
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.Settings.Configuration.Tests/DynamicLevelChangeTests.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.Settings.Configuration.Tests.Support; | ||
|
||
using Xunit; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
using TestDummies.Console; | ||
|
||
namespace Serilog.Settings.Configuration.Tests | ||
{ | ||
public class DynamicLevelChangeTests | ||
{ | ||
const string DefaultConfig = @"{ | ||
'Serilog': { | ||
'Using': [ 'TestDummies' ], | ||
'MinimumLevel': { | ||
'Default': 'Information', | ||
'Override': { | ||
'Root.Test': 'Information' | ||
} | ||
}, | ||
'LevelSwitches': { '$mySwitch': 'Information' }, | ||
'WriteTo:Dummy': { | ||
'Name': 'DummyConsole', | ||
'Args': { | ||
'levelSwitch': '$mySwitch' | ||
} | ||
} | ||
} | ||
}"; | ||
|
||
readonly ReloadableConfigurationSource _configSource; | ||
|
||
public DynamicLevelChangeTests() | ||
{ | ||
_configSource = new ReloadableConfigurationSource(JsonStringConfigSource.LoadData(DefaultConfig)); | ||
} | ||
|
||
[Fact] | ||
public void ShouldRespectDynamicLevelChanges() | ||
{ | ||
using (var logger = new LoggerConfiguration() | ||
.ReadFrom | ||
.Configuration(new ConfigurationBuilder().Add(_configSource).Build()) | ||
.CreateLogger()) | ||
{ | ||
DummyConsoleSink.Emitted.Clear(); | ||
logger.Write(Some.DebugEvent()); | ||
Assert.Empty(DummyConsoleSink.Emitted); | ||
|
||
DummyConsoleSink.Emitted.Clear(); | ||
UpdateConfig(minimumLevel: LogEventLevel.Debug); | ||
logger.Write(Some.DebugEvent()); | ||
Assert.Empty(DummyConsoleSink.Emitted); | ||
|
||
DummyConsoleSink.Emitted.Clear(); | ||
UpdateConfig(switchLevel: LogEventLevel.Debug); | ||
logger.Write(Some.DebugEvent()); | ||
logger.ForContext(Constants.SourceContextPropertyName, "Root.Test").Write(Some.DebugEvent()); | ||
Assert.Single(DummyConsoleSink.Emitted); | ||
|
||
DummyConsoleSink.Emitted.Clear(); | ||
UpdateConfig(overrideLevel: LogEventLevel.Debug); | ||
logger.ForContext(Constants.SourceContextPropertyName, "Root.Test").Write(Some.DebugEvent()); | ||
Assert.Single(DummyConsoleSink.Emitted); | ||
} | ||
} | ||
|
||
void UpdateConfig(LogEventLevel? minimumLevel = null, LogEventLevel? switchLevel = null, LogEventLevel? overrideLevel = null) | ||
{ | ||
if (minimumLevel.HasValue) | ||
{ | ||
_configSource.Set("Serilog:MinimumLevel:Default", minimumLevel.Value.ToString()); | ||
} | ||
|
||
if (switchLevel.HasValue) | ||
{ | ||
_configSource.Set("Serilog:LevelSwitches:$mySwitch", switchLevel.Value.ToString()); | ||
} | ||
|
||
if (overrideLevel.HasValue) | ||
{ | ||
_configSource.Set("Serilog:MinimumLevel:Override:Root.Test", overrideLevel.Value.ToString()); | ||
} | ||
|
||
_configSource.Reload(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.