1- using System ;
2- using System . IO ;
3- using System . Threading ;
4-
5- using Microsoft . Extensions . Configuration ;
6- using Microsoft . Extensions . Configuration . Json ;
7-
8- using Serilog . Core ;
1+ using Serilog . Core ;
92using Serilog . Events ;
103using Serilog . Settings . Configuration . Tests . Support ;
114
125using Xunit ;
6+ using Microsoft . Extensions . Configuration ;
7+
138using TestDummies . Console ;
149
1510namespace Serilog . Settings . Configuration . Tests
1611{
17- public class DynamicLevelChangeTests : IDisposable
12+ public class DynamicLevelChangeTests
1813 {
19- const string ConfigFilename = "dynamicLevels.json" ;
20-
21- readonly IConfigurationRoot _config ;
14+ const string DefaultConfig = @"{
15+ 'Serilog': {
16+ 'Using': [ 'TestDummies' ],
17+ 'MinimumLevel': {
18+ 'Default': 'Information',
19+ 'Override': {
20+ 'Root.Test': 'Information'
21+ }
22+ },
23+ 'LevelSwitches': { '$mySwitch': 'Information' },
24+ 'WriteTo:Dummy': {
25+ 'Name': 'DummyConsole',
26+ 'Args': {
27+ 'levelSwitch': '$mySwitch'
28+ }
29+ }
30+ }
31+ }" ;
2232
23- LogEventLevel _minimumLevel , _overrideLevel , _switchLevel ;
33+ readonly ReloadableConfigurationSource _configSource ;
2434
2535 public DynamicLevelChangeTests ( )
2636 {
27- UpdateConfig ( LogEventLevel . Information , LogEventLevel . Information , LogEventLevel . Information ) ;
28-
29- _config = new ConfigurationBuilder ( )
30- . AddJsonFile ( ConfigFilename , false , true )
31- . Build ( ) ;
32- }
33-
34- public void Dispose ( )
35- {
36- if ( File . Exists ( ConfigFilename ) )
37- {
38- File . Delete ( ConfigFilename ) ;
39- }
37+ _configSource = new ReloadableConfigurationSource ( JsonStringConfigSource . LoadData ( DefaultConfig ) ) ;
4038 }
4139
4240 [ Fact ]
4341 public void ShouldRespectDynamicLevelChanges ( )
4442 {
45- using ( var logger = new LoggerConfiguration ( ) . ReadFrom . Configuration ( _config ) . CreateLogger ( ) )
43+ using ( var logger = new LoggerConfiguration ( )
44+ . ReadFrom
45+ . Configuration ( new ConfigurationBuilder ( ) . Add ( _configSource ) . Build ( ) )
46+ . CreateLogger ( ) )
4647 {
4748 DummyConsoleSink . Emitted . Clear ( ) ;
4849 logger . Write ( Some . DebugEvent ( ) ) ;
@@ -64,39 +65,25 @@ public void ShouldRespectDynamicLevelChanges()
6465 logger . ForContext ( Constants . SourceContextPropertyName , "Root.Test" ) . Write ( Some . DebugEvent ( ) ) ;
6566 Assert . Single ( DummyConsoleSink . Emitted ) ;
6667 }
67- }
6868
69- void UpdateConfig ( LogEventLevel ? minimumLevel = null , LogEventLevel ? overrideLevel = null , LogEventLevel ? switchLevel = null )
70- {
71- File . WriteAllText ( ConfigFilename , BuildConfiguration ( ) ) ;
72- Thread . Sleep ( 300 ) ;
73-
74- string BuildConfiguration ( )
69+ void UpdateConfig ( LogEventLevel ? minimumLevel = null , LogEventLevel ? switchLevel = null , LogEventLevel ? overrideLevel = null )
7570 {
76- _minimumLevel = minimumLevel ?? _minimumLevel ;
77- _overrideLevel = overrideLevel ?? _overrideLevel ;
78- _switchLevel = switchLevel ?? _switchLevel ;
79-
80- var config = @"{
81- 'Serilog': {
82- 'Using': [ 'TestDummies' ],
83- 'MinimumLevel': {
84- 'Default': '" + _minimumLevel + @"',
85- 'Override': {
86- 'Root.Test': '" + _overrideLevel + @"'
87- }
88- },
89- 'LevelSwitches': { '$mySwitch': '" + _switchLevel + @"' },
90- 'WriteTo:Dummy': {
91- 'Name': 'DummyConsole',
92- 'Args': {
93- 'levelSwitch': '$mySwitch'
94- }
95- }
96- }
97- }" ;
98-
99- return config ;
71+ if ( minimumLevel . HasValue )
72+ {
73+ _configSource . Set ( "Serilog:MinimumLevel:Default" , minimumLevel . Value . ToString ( ) ) ;
74+ }
75+
76+ if ( switchLevel . HasValue )
77+ {
78+ _configSource . Set ( "Serilog:LevelSwitches:$mySwitch" , switchLevel . Value . ToString ( ) ) ;
79+ }
80+
81+ if ( overrideLevel . HasValue )
82+ {
83+ _configSource . Set ( "Serilog:MinimumLevel:Override:Root.Test" , overrideLevel . Value . ToString ( ) ) ;
84+ }
85+
86+ _configSource . Reload ( ) ;
10087 }
10188 }
10289 }
0 commit comments