Skip to content

Commit 400b1de

Browse files
committed
moved ConfigurationSectionArgumentValue functionality into ObjectArgumentValue, removed hint hack
1 parent da883fa commit 400b1de

File tree

6 files changed

+44
-76
lines changed

6 files changed

+44
-76
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,13 @@ dotnet run
9494

9595
### Nested configuration sections
9696

97-
Some Serilog packages require a reference to a logger configuration object. These must be "hinted" by appending a `>` character to the end of the argument name. The sample program in this project illustrates this with the following entry configuring the _Serilog.Sinks.Async_ package to wrap the _Serilog.Sinks.File_ package.
98-
99-
Note the `configure>` parameter name with the trailing hint character:
97+
Some Serilog packages require a reference to a logger configuration object. The sample program in this project illustrates this with the following entry configuring the _Serilog.Sinks.Async_ package to wrap the _Serilog.Sinks.File_ package. The `configure` parameter references the File sink configuration:
10098

10199
```json
102100
"WriteTo:Async": {
103101
"Name": "Async",
104102
"Args": {
105-
"configure>": [
103+
"configure": [
106104
{
107105
"Name": "File",
108106
"Args": {

sample/Sample/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"WriteTo:Sublogger": {
1212
"Name": "Logger",
1313
"Args": {
14-
"configureLogger>": {
14+
"configureLogger": {
1515
"WriteTo": [
1616
{
1717
"Name": "Console",
@@ -28,7 +28,7 @@
2828
"WriteTo:Async": {
2929
"Name": "Async",
3030
"Args": {
31-
"configure>": [
31+
"configure": [
3232
{
3333
"Name": "File",
3434
"Args": {

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

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ namespace Serilog.Settings.Configuration
1919
class ConfigurationReader : IConfigurationReader
2020
{
2121
const string LevelSwitchNameRegex = @"^\$[A-Za-z]+[A-Za-z0-9]*$";
22-
const string NestedConfigHintChar = ">";
2322

2423
static IConfiguration _configuration;
2524

2625
readonly IConfigurationSection _section;
2726
readonly DependencyContext _dependencyContext;
2827
readonly IReadOnlyCollection<Assembly> _configurationAssemblies;
2928

30-
#region Constructors
31-
3229
public ConfigurationReader(IConfiguration configuration, DependencyContext dependencyContext)
3330
{
3431
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
@@ -47,17 +44,13 @@ public ConfigurationReader(IConfiguration configuration, DependencyContext depen
4744
}
4845

4946
// Used internally for processing nested configuration sections -- see GetMethodCalls below.
50-
ConfigurationReader(IConfigurationSection configSection, IReadOnlyCollection<Assembly> configurationAssemblies, DependencyContext dependencyContext)
47+
internal ConfigurationReader(IConfigurationSection configSection, IReadOnlyCollection<Assembly> configurationAssemblies, DependencyContext dependencyContext)
5148
{
5249
_section = configSection ?? throw new ArgumentNullException(nameof(configSection));
5350
_dependencyContext = dependencyContext;
5451
_configurationAssemblies = configurationAssemblies ?? throw new ArgumentNullException(nameof(configurationAssemblies));
5552
}
5653

57-
#endregion
58-
59-
#region Configure and related Apply methods
60-
6154
public void Configure(LoggerConfiguration loggerConfiguration)
6255
{
6356
var declaredLevelSwitches = ProcessLevelSwitchDeclarations();
@@ -204,10 +197,6 @@ void ApplyEnrichment(LoggerConfiguration loggerConfiguration, IReadOnlyDictionar
204197
}
205198
}
206199

207-
#endregion
208-
209-
#region Internal implementation
210-
211200
internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMethodCalls(IConfigurationSection directive)
212201
{
213202
var children = directive.GetChildren().ToList();
@@ -222,7 +211,7 @@ internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMet
222211
let name = GetSectionName(child)
223212
let callArgs = (from argument in child.GetSection("Args").GetChildren()
224213
select new {
225-
Name = argument.Key.Replace(NestedConfigHintChar, string.Empty),
214+
Name = argument.Key,
226215
Value = GetArgumentValue(argument) }).ToDictionary(p => p.Name, p => p.Value)
227216
select new { Name = name, Args = callArgs }))
228217
.ToLookup(p => p.Name, p => p.Args);
@@ -238,14 +227,7 @@ IConfigurationArgumentValue GetArgumentValue(IConfigurationSection argumentSecti
238227
}
239228
else
240229
{
241-
if(argumentSection.Key.EndsWith(NestedConfigHintChar))
242-
{
243-
argumentValue = new ConfigurationSectionArgumentValue(new ConfigurationReader(argumentSection, _configurationAssemblies, _dependencyContext));
244-
}
245-
else
246-
{
247-
argumentValue = new ObjectArgumentValue(argumentSection);
248-
}
230+
argumentValue = new ObjectArgumentValue(argumentSection, _configurationAssemblies, _dependencyContext);
249231
}
250232

251233
return argumentValue;
@@ -391,10 +373,6 @@ internal static IList<MethodInfo> FindConfigurationMethods(IReadOnlyCollection<A
391373
.ToList();
392374
}
393375

394-
#endregion
395-
396-
#region Internal helpers
397-
398376
// don't support (yet?) arrays in the parameter list (ILogEventEnricher[])
399377
internal static LoggerConfiguration With(LoggerFilterConfiguration loggerFilterConfiguration, ILogEventFilter filter)
400378
=> loggerFilterConfiguration.With(filter);
@@ -426,6 +404,5 @@ internal static LogEventLevel ParseLogEventLevel(string value)
426404
return parsedLevel;
427405
}
428406

429-
#endregion
430407
}
431408
}

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

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/Serilog.Settings.Configuration/Settings/Configuration/ObjectArgumentValue.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,56 @@
11
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyModel;
3+
using Serilog.Configuration;
24
using Serilog.Core;
35
using System;
46
using System.Collections.Generic;
7+
using System.Reflection;
58

69
namespace Serilog.Settings.Configuration
710
{
811
class ObjectArgumentValue : IConfigurationArgumentValue
912
{
1013
readonly IConfigurationSection section;
14+
readonly IReadOnlyCollection<Assembly> configurationAssemblies;
15+
readonly DependencyContext dependencyContext;
1116

12-
public ObjectArgumentValue(IConfigurationSection section)
17+
public ObjectArgumentValue(IConfigurationSection section, IReadOnlyCollection<Assembly> configurationAssemblies, DependencyContext dependencyContext)
1318
{
1419
this.section = section;
20+
21+
// used by nested logger configurations to feed a new pass by ConfigurationReader
22+
this.configurationAssemblies = configurationAssemblies;
23+
this.dependencyContext = dependencyContext;
1524
}
1625

1726
public object ConvertTo(Type toType, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredLevelSwitches)
1827
{
28+
// return the entire section for internal processing
1929
if(toType == typeof(IConfigurationSection)) return section;
30+
31+
// process a nested configuration to populate an Action<> logger/sink config parameter?
32+
var typeInfo = toType.GetTypeInfo();
33+
if(typeInfo.IsGenericType &&
34+
typeInfo.GetGenericTypeDefinition() is Type genericType && genericType == typeof(Action<>))
35+
{
36+
var configType = typeInfo.GenericTypeArguments[0];
37+
if(configType != typeof(LoggerConfiguration) && configType != typeof(LoggerSinkConfiguration))
38+
throw new ArgumentException($"Configuration for Action<{configType}> is not implemented.");
39+
40+
IConfigurationReader configReader = new ConfigurationReader(section, configurationAssemblies, dependencyContext);
41+
42+
if(configType == typeof(LoggerConfiguration))
43+
{
44+
return new Action<LoggerConfiguration>(configReader.Configure);
45+
}
46+
47+
if(configType == typeof(LoggerSinkConfiguration))
48+
{
49+
return new Action<LoggerSinkConfiguration>(loggerSinkConfig => configReader.ApplySinks(loggerSinkConfig, declaredLevelSwitches));
50+
}
51+
}
52+
53+
// MS Config binding
2054
return section.Get(toType);
2155
}
2256
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public void WriteToLoggerWithRestrictedToMinimumLevelIsSupported()
402402
""WriteTo"": [{
403403
""Name"": ""Logger"",
404404
""Args"": {
405-
""configureLogger>"" : {
405+
""configureLogger"" : {
406406
""WriteTo"": [{
407407
""Name"": ""DummyRollingFile"",
408408
""Args"": {""pathFormat"" : ""C:\\""}
@@ -438,7 +438,7 @@ public void WriteToSubLoggerWithLevelSwitchIsSupported()
438438
""WriteTo"": [{
439439
""Name"": ""Logger"",
440440
""Args"": {
441-
""configureLogger>"" : {
441+
""configureLogger"" : {
442442
""WriteTo"": [{
443443
""Name"": ""DummyRollingFile"",
444444
""Args"": {""pathFormat"" : ""C:\\""}

0 commit comments

Comments
 (0)