Skip to content

Commit 3a6d87b

Browse files
author
Sergey Komisarchik
committed
- added ReloadableConfigurationSource to Support types
- fixed failing test
1 parent f6e55b5 commit 3a6d87b

File tree

3 files changed

+95
-61
lines changed

3 files changed

+95
-61
lines changed

test/Serilog.Settings.Configuration.Tests/DynamicLevelChangeTests.cs

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
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;
92
using Serilog.Events;
103
using Serilog.Settings.Configuration.Tests.Support;
114

125
using Xunit;
6+
using Microsoft.Extensions.Configuration;
7+
138
using TestDummies.Console;
149

1510
namespace 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
}

test/Serilog.Settings.Configuration.Tests/Support/JsonStringConfigSource.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using Microsoft.Extensions.Configuration;
2-
using Microsoft.Extensions.Configuration.Json;
3-
1+
using System.Collections.Generic;
42
using System.IO;
53

4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.Configuration.Json;
6+
67
namespace Serilog.Settings.Configuration.Tests.Support
78
{
89
class JsonStringConfigSource : IConfigurationSource
@@ -24,6 +25,13 @@ public static IConfigurationSection LoadSection(string json, string section)
2425
return new ConfigurationBuilder().Add(new JsonStringConfigSource(json)).Build().GetSection(section);
2526
}
2627

28+
public static IDictionary<string, string> LoadData(string json)
29+
{
30+
var provider = new JsonStringConfigProvider(json);
31+
provider.Load();
32+
return provider.Data;
33+
}
34+
2735
class JsonStringConfigProvider : JsonConfigurationProvider
2836
{
2937
readonly string _json;
@@ -33,6 +41,8 @@ public JsonStringConfigProvider(string json) : base(new JsonConfigurationSource
3341
_json = json;
3442
}
3543

44+
public new IDictionary<string, string> Data => base.Data;
45+
3646
public override void Load()
3747
{
3848
Load(StringToStream(_json));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using Microsoft.Extensions.Configuration;
3+
4+
namespace Serilog.Settings.Configuration.Tests.Support
5+
{
6+
class ReloadableConfigurationSource : IConfigurationSource
7+
{
8+
readonly ReloadableConfigurationProvider _configProvider;
9+
readonly IDictionary<string, string> _source;
10+
11+
public ReloadableConfigurationSource(IDictionary<string, string> source)
12+
{
13+
_source = source;
14+
_configProvider = new ReloadableConfigurationProvider(source);
15+
}
16+
17+
public IConfigurationProvider Build(IConfigurationBuilder builder) => _configProvider;
18+
19+
public void Reload() => _configProvider.Reload();
20+
21+
public void Set(string key, string value) => _source[key] = value;
22+
23+
class ReloadableConfigurationProvider : ConfigurationProvider
24+
{
25+
readonly IDictionary<string, string> _source;
26+
27+
public ReloadableConfigurationProvider(IDictionary<string, string> source)
28+
{
29+
_source = source;
30+
}
31+
32+
public override void Load() => Data = _source;
33+
34+
public void Reload() => OnReload();
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)