forked from serilog/serilog-settings-configuration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigurationReaderTestHelpers.cs
62 lines (54 loc) · 2.17 KB
/
ConfigurationReaderTestHelpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using Microsoft.Extensions.Configuration;
using Serilog.Events;
namespace Serilog.Settings.Configuration.Tests.Support;
static class ConfigurationReaderTestHelpers
{
public const string minimumLevelFlatTemplate = """
{{
"Serilog": {{
"MinimumLevel": "{0}"
}}
}}
""";
public const string minimumLevelObjectTemplate = """
{{
"Serilog": {{
"MinimumLevel": {{
"Default": "{0}"
}}
}}
}}
""";
public const string minimumLevelFlatKey = "Serilog:MinimumLevel";
public const string minimumLevelObjectKey = "Serilog:MinimumLevel:Default";
public static void AssertLogEventLevels(LoggerConfiguration loggerConfig, LogEventLevel expectedMinimumLevel)
{
var logger = loggerConfig.CreateLogger();
var logEventValues = Enum.GetValues(typeof(LogEventLevel)).Cast<LogEventLevel>();
foreach (var logEvent in logEventValues)
{
if (logEvent < expectedMinimumLevel)
{
Assert.False(logger.IsEnabled(logEvent),
$"The log level {logEvent} should be disabled as it's lower priority than the minimum level of {expectedMinimumLevel}.");
}
else
{
Assert.True(logger.IsEnabled(logEvent),
$"The log level {logEvent} should be enabled as it's {(logEvent == expectedMinimumLevel ? "the same" : "higher")} priority {(logEvent == expectedMinimumLevel ? "as" : "than")} the minimum level of {expectedMinimumLevel}.");
}
}
}
// the naming is only to show priority as providers
public static IConfigurationRoot GetConfigRoot(
string? appsettingsJsonLevel = null,
string? appsettingsDevelopmentJsonLevel = null,
Dictionary<string, string?>? envVariables = null)
{
var configBuilder = new ConfigurationBuilder();
configBuilder.AddJsonString(appsettingsJsonLevel ?? "{}");
configBuilder.AddJsonString(appsettingsDevelopmentJsonLevel ?? "{}");
configBuilder.Add(new ReloadableConfigurationSource(envVariables ?? []));
return configBuilder.Build();
}
}