Skip to content

Commit 88ef290

Browse files
authored
Merge pull request #14 from skomis-mm/serilog-dev
Support for `MinimumLevel.Overide()`
2 parents 660977b + b9c7b5d commit 88ef290

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

sample/Sample/Program.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using Serilog;
55
using System.IO;
66

7+
using Serilog.Core;
8+
79
namespace Sample
810
{
911
public class Program
@@ -21,7 +23,11 @@ public static void Main(string[] args)
2123

2224
do
2325
{
24-
logger.Information("Hello, world!");
26+
logger.ForContext<Program>().Information("Hello, world!");
27+
logger.ForContext(Constants.SourceContextPropertyName, "Microsoft").Warning("Hello, world!");
28+
logger.ForContext(Constants.SourceContextPropertyName, "MyApp.Something.Tricky").Verbose("Hello, world!");
29+
30+
Console.WriteLine();
2531
}
2632
while (Console.ReadKey().KeyChar != 'q');
2733
}

sample/Sample/appsettings.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
{
22
"Serilog": {
33
"Using": ["Serilog.Sinks.Literate"],
4-
"MinimumLevel": "Debug",
4+
"MinimumLevel": {
5+
"Default": "Debug",
6+
"Override": {
7+
"Microsoft": "Warning",
8+
"MyApp.Something.Tricky": "Verbose"
9+
}
10+
},
511
"WriteTo": [
6-
{ "Name": "LiterateConsole" },
12+
{
13+
"Name": "LiterateConsole",
14+
"Args": {
15+
"outputTemplate": "[{Timestamp:HH:mm:ss} {SourceContext} [{Level}] {Message}{NewLine}{Exception}"
16+
}
17+
},
718
{
819
"Name": "File",
920
"Args": {

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ Dictionary<string, Dictionary<string, string>> GetMethodCalls(IConfigurationSect
8080
else
8181
{
8282
var name = child.GetSection("Name");
83-
if (name == null)
84-
throw new InvalidOperationException("The configuration value in Serilog.WriteTo has no Name element.");
83+
if (name.Value == null)
84+
throw new InvalidOperationException($"The configuration value in {name.Path} has no Name element.");
8585

8686
var callArgs = new Dictionary<string, string>();
8787
var args = child.GetSection("Args");
@@ -100,25 +100,39 @@ Dictionary<string, Dictionary<string, string>> GetMethodCalls(IConfigurationSect
100100

101101
void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration)
102102
{
103+
var applyMinimumLevelAction =
104+
new Action<IConfigurationSection, Action<LoggerMinimumLevelConfiguration, LoggingLevelSwitch>>(
105+
(directive, applyConfigAction) =>
106+
{
107+
LogEventLevel minimumLevel;
108+
if (!Enum.TryParse(directive.Value, out minimumLevel))
109+
throw new InvalidOperationException($"The value {directive.Value} is not a valid Serilog level.");
110+
111+
var levelSwitch = new LoggingLevelSwitch(minimumLevel);
112+
applyConfigAction(loggerConfiguration.MinimumLevel, levelSwitch);
113+
114+
ChangeToken.OnChange(
115+
directive.GetReloadToken,
116+
() =>
117+
{
118+
if (Enum.TryParse(directive.Value, out minimumLevel))
119+
levelSwitch.MinimumLevel = minimumLevel;
120+
else
121+
SelfLog.WriteLine($"The value {directive.Value} is not a valid Serilog level.");
122+
});
123+
});
124+
103125
var minimumLevelDirective = _configuration.GetSection("MinimumLevel");
104-
if (minimumLevelDirective?.Value != null)
105-
{
106-
LogEventLevel minimumLevel;
107-
if (!Enum.TryParse(minimumLevelDirective.Value, out minimumLevel))
108-
throw new InvalidOperationException($"The value {minimumLevelDirective.Value} is not a valid Serilog level.");
109126

110-
var levelSwitch = new LoggingLevelSwitch(minimumLevel);
111-
loggerConfiguration.MinimumLevel.ControlledBy(levelSwitch);
127+
var defaultMinLevelDirective = minimumLevelDirective.Value != null ? minimumLevelDirective : minimumLevelDirective.GetSection("Default");
128+
if (defaultMinLevelDirective.Value != null)
129+
{
130+
applyMinimumLevelAction(defaultMinLevelDirective, (configuration, levelSwitch) => configuration.ControlledBy(levelSwitch));
131+
}
112132

113-
ChangeToken.OnChange(
114-
() => minimumLevelDirective.GetReloadToken(),
115-
() =>
116-
{
117-
if (Enum.TryParse(minimumLevelDirective.Value, out minimumLevel))
118-
levelSwitch.MinimumLevel = minimumLevel;
119-
else
120-
SelfLog.WriteLine($"The value {minimumLevelDirective.Value} is not a valid Serilog level.");
121-
});
133+
foreach (var overrideDirective in minimumLevelDirective.GetSection("Override").GetChildren())
134+
{
135+
applyMinimumLevelAction(overrideDirective, (configuration, levelSwitch) => configuration.Override(overrideDirective.Key, levelSwitch));
122136
}
123137
}
124138

0 commit comments

Comments
 (0)