@@ -46,64 +46,80 @@ private void LoadConfigurationSection(IConfigurationSection nlogConfig, bool? au
4646
4747 private class LoggingConfigurationElement : ILoggingConfigurationElement
4848 {
49- readonly IConfigurationSection _configurationSection ;
50- readonly string _nameOverride ;
49+ private const string VariablesKey = "Variables" ;
50+ private const string VariableKey = "Variable" ;
51+ private const string TargetKey = "target" ;
52+ private readonly IConfigurationSection _configurationSection ;
53+ private readonly string _nameOverride ;
54+ private readonly bool _topElement ;
5155
56+ public string Name => _nameOverride ?? _configurationSection . Key ;
57+ public IEnumerable < KeyValuePair < string , string > > Values => GetValues ( ) ;
58+ public IEnumerable < ILoggingConfigurationElement > Children => GetChildren ( ) ;
5259 public bool AutoReload { get ; }
5360
5461 public LoggingConfigurationElement ( IConfigurationSection configurationSection , bool topElement , string nameOverride = null )
5562 {
5663 _configurationSection = configurationSection ;
5764 _nameOverride = nameOverride ;
58- if ( topElement && bool . TryParse ( configurationSection [ "autoreload" ] , out var autoreload ) )
65+ _topElement = topElement ;
66+ if ( topElement )
5967 {
60- AutoReload = autoreload ;
68+ if ( bool . TryParse ( configurationSection [ "autoreload" ] , out var autoreload ) )
69+ {
70+ AutoReload = autoreload ;
71+ }
6172 }
6273 }
6374
64- public string Name => _nameOverride ?? _configurationSection . Key ;
65-
66- public IEnumerable < KeyValuePair < string , string > > Values
75+ private IEnumerable < KeyValuePair < string , string > > GetValues ( )
6776 {
68- get
77+ var children = _configurationSection . GetChildren ( ) ;
78+ foreach ( var child in children )
6979 {
70- var children = _configurationSection . GetChildren ( ) ;
71- foreach ( var child in children )
72- {
73- if ( ! child . GetChildren ( ) . Any ( ) )
74- yield return new KeyValuePair < string , string > ( child . Key , child . Value ) ;
75- }
76- if ( _nameOverride != null )
77- yield return new KeyValuePair < string , string > ( "name " , _configurationSection . Key ) ;
80+ if ( ! child . GetChildren ( ) . Any ( ) )
81+ yield return new KeyValuePair < string , string > ( child . Key , child . Value ) ;
82+ }
83+ if ( _nameOverride != null )
84+ {
85+ yield return new KeyValuePair < string , string > ( "name" , _configurationSection . Key ) ;
86+ if ( ReferenceEquals ( _nameOverride , VariableKey ) )
87+ yield return new KeyValuePair < string , string > ( "value " , _configurationSection . Value ) ;
7888 }
7989 }
8090
81- public IEnumerable < ILoggingConfigurationElement > Children
91+ private IEnumerable < ILoggingConfigurationElement > GetChildren ( )
8292 {
83- get
93+ var variables = _topElement ? _configurationSection . GetSection ( VariablesKey ) : null ;
94+ if ( variables != null )
8495 {
85- var children = _configurationSection . GetChildren ( ) ;
86- foreach ( var child in children )
96+ foreach ( var variable in variables . GetChildren ( ) )
97+ yield return new LoggingConfigurationElement ( variable , false , VariableKey ) ;
98+ }
99+
100+ var children = _configurationSection . GetChildren ( ) ;
101+ foreach ( var child in children )
102+ {
103+ var firstChildValue = child ? . GetChildren ( ) ? . FirstOrDefault ( ) ;
104+ if ( firstChildValue == null )
105+ continue ; // Simple value without children
106+
107+ if ( _nameOverride == TargetKey && child . Key . EqualsOrdinalIgnoreCase ( TargetKey ) && child . GetChildren ( ) . Count ( ) == 1 )
87108 {
88- var firstChildValue = child ? . GetChildren ( ) ? . FirstOrDefault ( ) ;
89- if ( firstChildValue == null )
90- {
109+ // Target-config inside Wrapper-Target
110+ yield return new LoggingConfigurationElement ( firstChildValue , false , TargetKey ) ;
111+ }
112+ else
113+ {
114+ if ( variables != null && string . Equals ( child . Key , VariablesKey , StringComparison . OrdinalIgnoreCase ) )
91115 continue ;
92- }
93116
94- if ( _nameOverride == "target" && child . Key . EqualsOrdinalIgnoreCase ( "target" ) && child . GetChildren ( ) . Count ( ) == 1 )
95- {
96- yield return new LoggingConfigurationElement ( firstChildValue , false , "target" ) ;
97- }
98- else
117+ string nameOverride = null ;
118+ if ( _configurationSection . Key . EqualsOrdinalIgnoreCase ( "targets" ) )
99119 {
100- string nameOverride = null ;
101- if ( _configurationSection . Key . EqualsOrdinalIgnoreCase ( "targets" ) )
102- {
103- nameOverride = "target" ;
104- }
105- yield return new LoggingConfigurationElement ( child , false , nameOverride ) ;
120+ nameOverride = TargetKey ;
106121 }
122+ yield return new LoggingConfigurationElement ( child , false , nameOverride ) ;
107123 }
108124 }
109125 }
0 commit comments