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 ;
9
2
using Serilog . Events ;
10
3
using Serilog . Settings . Configuration . Tests . Support ;
11
4
12
5
using Xunit ;
6
+ using Microsoft . Extensions . Configuration ;
7
+
13
8
using TestDummies . Console ;
14
9
15
10
namespace Serilog . Settings . Configuration . Tests
16
11
{
17
- public class DynamicLevelChangeTests : IDisposable
12
+ public class DynamicLevelChangeTests
18
13
{
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
+ }" ;
22
32
23
- LogEventLevel _minimumLevel , _overrideLevel , _switchLevel ;
33
+ readonly ReloadableConfigurationSource _configSource ;
24
34
25
35
public DynamicLevelChangeTests ( )
26
36
{
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 ) ) ;
40
38
}
41
39
42
40
[ Fact ]
43
41
public void ShouldRespectDynamicLevelChanges ( )
44
42
{
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 ( ) )
46
47
{
47
48
DummyConsoleSink . Emitted . Clear ( ) ;
48
49
logger . Write ( Some . DebugEvent ( ) ) ;
@@ -64,39 +65,25 @@ public void ShouldRespectDynamicLevelChanges()
64
65
logger . ForContext ( Constants . SourceContextPropertyName , "Root.Test" ) . Write ( Some . DebugEvent ( ) ) ;
65
66
Assert . Single ( DummyConsoleSink . Emitted ) ;
66
67
}
67
- }
68
68
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 )
75
70
{
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 ( ) ;
100
87
}
101
88
}
102
89
}
0 commit comments