Skip to content

Commit af9efd3

Browse files
authored
Merge pull request #88 from tsimbalar/loglevelswitchsupport
Add support for LoggingLevelSwitch
2 parents 7d1f277 + 7034ca6 commit af9efd3

12 files changed

+973
-49
lines changed

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
root = true
2+
3+
[*]
4+
trim_trailing_whitespace = true
5+
insert_final_newline = true
6+
indent_style = space
7+
indent_size = 4
8+
9+
[*.{csproj,json,config,yml}]
10+
indent_size = 2
11+
12+
[*.sh]
13+
end_of_line = lf
14+
15+
[*.{cmd, bat}]
16+
end_of_line = crlf

serilog-settings-configuration.sln

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4E41FD57-5FA
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{62D0B904-1D11-4962-A4A8-DE28672AA28B}"
99
ProjectSection(SolutionItems) = preProject
10+
.editorconfig = .editorconfig
1011
appveyor.yml = appveyor.yml
1112
Build.ps1 = Build.ps1
1213
CHANGES.md = CHANGES.md
1314
LICENSE = LICENSE
1415
README.md = README.md
16+
serilog-settings-configuration.sln.DotSettings = serilog-settings-configuration.sln.DotSettings
1517
EndProjectSection
1618
EndProject
1719
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{D551DCB0-7771-4D01-BEBD-F7B57D1CF0E3}"

serilog-settings-configuration.sln.DotSettings

Lines changed: 559 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 108 additions & 33 deletions
Large diffs are not rendered by default.

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationSectionArgumentValue.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Serilog.Configuration;
22
using System;
3+
using System.Collections.Generic;
34
using System.Reflection;
5+
using Serilog.Core;
46

57
namespace Serilog.Settings.Configuration
68
{
@@ -13,10 +15,10 @@ public ConfigurationSectionArgumentValue(IConfigurationReader configReader)
1315
_configReader = configReader ?? throw new ArgumentNullException(nameof(configReader));
1416
}
1517

16-
public object ConvertTo(Type toType)
18+
public object ConvertTo(Type toType, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredLevelSwitches)
1719
{
1820
var typeInfo = toType.GetTypeInfo();
19-
if (!typeInfo.IsGenericType ||
21+
if (!typeInfo.IsGenericType ||
2022
typeInfo.GetGenericTypeDefinition() is Type genericType && genericType != typeof(Action<>))
2123
{
2224
throw new InvalidOperationException("Argument value should be of type Action<>.");
@@ -25,7 +27,7 @@ public object ConvertTo(Type toType)
2527
var configurationType = typeInfo.GenericTypeArguments[0];
2628
if (configurationType == typeof(LoggerSinkConfiguration))
2729
{
28-
return new Action<LoggerSinkConfiguration>(_configReader.ApplySinks);
30+
return new Action<LoggerSinkConfiguration>(loggerSinkConfig => _configReader.ApplySinks(loggerSinkConfig, declaredLevelSwitches));
2931
}
3032

3133
if (configurationType == typeof(LoggerConfiguration))
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Collections.Generic;
3+
using Serilog.Core;
24

35
namespace Serilog.Settings.Configuration
46
{
57
interface IConfigurationArgumentValue
68
{
7-
object ConvertTo(Type toType);
9+
object ConvertTo(Type toType, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredLevelSwitches);
810
}
911
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using Serilog.Configuration;
1+
using System.Collections.Generic;
2+
using Serilog.Configuration;
3+
using Serilog.Core;
24

35
namespace Serilog.Settings.Configuration
46
{
57
interface IConfigurationReader : ILoggerSettings
68
{
7-
void ApplySinks(LoggerSinkConfiguration loggerSinkConfiguration);
9+
void ApplySinks(LoggerSinkConfiguration loggerSinkConfiguration, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredLevelSwitches);
810
}
911
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Serilog.Core;
4+
5+
namespace Serilog.Settings.Configuration
6+
{
7+
internal static class LevelSwitchDictionaryExtensions
8+
{
9+
/// <summary>
10+
/// Looks up a switch in the declared LoggingLevelSwitches
11+
/// </summary>
12+
/// <param name="namedLevelSwitches">the dictionary of switches to look up by name</param>
13+
/// <param name="switchName">the name of a switch to look up</param>
14+
/// <returns>the LoggingLevelSwitch registered with the name</returns>
15+
/// <exception cref="InvalidOperationException">if no switch has been registered with <paramref name="switchName"/></exception>
16+
public static LoggingLevelSwitch LookUpSwitchByName(this IReadOnlyDictionary<string, LoggingLevelSwitch> namedLevelSwitches, string switchName)
17+
{
18+
if (namedLevelSwitches == null) throw new ArgumentNullException(nameof(namedLevelSwitches));
19+
if (namedLevelSwitches.TryGetValue(switchName, out var levelSwitch))
20+
{
21+
return levelSwitch;
22+
}
23+
24+
throw new InvalidOperationException($"No LoggingLevelSwitch has been declared with name \"{switchName}\". You might be missing a section \"LevelSwitches\":{{\"{switchName}\":\"InitialLevel\"}}");
25+
}
26+
}
27+
}

src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class StringArgumentValue : IConfigurationArgumentValue
1616
readonly Func<string> _valueProducer;
1717
readonly Func<IChangeToken> _changeTokenProducer;
1818

19-
private static readonly Regex StaticMemberAccessorRegex = new Regex("^(?<shortTypeName>[^:]+)::(?<memberName>[A-Za-z][A-Za-z0-9]*)(?<typeNameExtraQualifiers>[^:]*)$");
19+
static readonly Regex StaticMemberAccessorRegex = new Regex("^(?<shortTypeName>[^:]+)::(?<memberName>[A-Za-z][A-Za-z0-9]*)(?<typeNameExtraQualifiers>[^:]*)$");
2020

2121
public StringArgumentValue(Func<string> valueProducer, Func<IChangeToken> changeTokenProducer = null)
2222
{
@@ -30,10 +30,15 @@ public StringArgumentValue(Func<string> valueProducer, Func<IChangeToken> change
3030
{ typeof(TimeSpan), s => TimeSpan.Parse(s) }
3131
};
3232

33-
public object ConvertTo(Type toType)
33+
public object ConvertTo(Type toType, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredLevelSwitches)
3434
{
3535
var argumentValue = Environment.ExpandEnvironmentVariables(_valueProducer());
3636

37+
if (toType == typeof(LoggingLevelSwitch))
38+
{
39+
return declaredLevelSwitches.LookUpSwitchByName(argumentValue);
40+
}
41+
3742
var toTypeInfo = toType.GetTypeInfo();
3843
if (toTypeInfo.IsGenericType && toType.GetGenericTypeDefinition() == typeof(Nullable<>))
3944
{

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Xunit;
44
using System.Reflection;
55
using System.Linq;
6+
using Serilog.Core;
67
using Serilog.Settings.Configuration.Tests.Support;
78

89
namespace Serilog.Settings.Configuration.Tests
@@ -70,11 +71,11 @@ public void WriteToSupportExpandedSyntaxWithArgs()
7071

7172
Assert.Equal(1, result["LiterateConsole"].Count());
7273

73-
var args = result["LiterateConsole"].Single().Cast<KeyValuePair<string, IConfigurationArgumentValue>>().ToArray();
74+
var args = result["LiterateConsole"].Single().ToArray();
7475

7576
Assert.Equal(1, args.Length);
7677
Assert.Equal("outputTemplate", args[0].Key);
77-
Assert.Equal("{Message}", args[0].Value.ConvertTo(typeof(string)));
78+
Assert.Equal("{Message}", args[0].Value.ConvertTo(typeof(string), new Dictionary<string, LoggingLevelSwitch>()));
7879
}
7980

8081
[Fact]

0 commit comments

Comments
 (0)