@@ -65,7 +65,7 @@ public override LoggingConfiguration Reload()
6565
6666 private bool LoadConfigurationSection ( IConfigurationSection nlogConfig )
6767 {
68- var configElement = new LoggingConfigurationElement ( nlogConfig , true , RootSectionKey ) ;
68+ var configElement = new LoggingConfigurationElement ( nlogConfig , RootSectionKey ) ;
6969 LoadConfig ( configElement , null ) ;
7070 return configElement . AutoReload ;
7171 }
@@ -131,20 +131,22 @@ private class LoggingConfigurationElement : ILoggingConfigurationElement
131131 private const string TargetKey = "target" ;
132132 private const string TargetsKey = "targets" ;
133133 private const string DefaultTargetParameters = "Default-target-parameters" ;
134+ private const string TargetDefaultParameters = "TargetDefaultParameters" ;
134135 private const string VariableKey = "Variable" ;
135136 private const string DefaultWrapper = "Default-wrapper" ;
137+ private const string TargetDefaultWrapper = "TargetDefaultWrapper" ;
136138 private readonly IConfigurationSection _configurationSection ;
137- private IConfigurationSection DefaultTargetParametersSection { get ; set ; }
138- private IConfigurationSection DefaultTargetWrapperSection { get ; set ; }
139+ private IConfigurationSection TargetDefaultParametersSection { get ; set ; }
140+ private IConfigurationSection TargetDefaultWrapperSection { get ; set ; }
139141 private readonly string _nameOverride ;
140142 private readonly bool _topElement ;
141143
142- public LoggingConfigurationElement ( IConfigurationSection configurationSection , bool topElement , string nameOverride = null )
144+ public LoggingConfigurationElement ( IConfigurationSection configurationSection , string nameOverride = null )
143145 {
144146 _configurationSection = configurationSection ;
145147 _nameOverride = nameOverride ;
146- _topElement = topElement ;
147- if ( topElement && bool . TryParse ( configurationSection [ "autoreload" ] , out var autoReload ) )
148+ _topElement = ReferenceEquals ( nameOverride , RootSectionKey ) ;
149+ if ( _topElement && bool . TryParse ( configurationSection [ "autoreload" ] , out var autoReload ) )
148150 {
149151 AutoReload = autoReload ;
150152 }
@@ -192,7 +194,7 @@ private IEnumerable<ILoggingConfigurationElement> GetChildren()
192194 {
193195 foreach ( var variable in variables . GetChildren ( ) )
194196 {
195- yield return new LoggingConfigurationElement ( variable , false , VariableKey ) ;
197+ yield return new LoggingConfigurationElement ( variable , VariableKey ) ;
196198 }
197199 }
198200
@@ -214,11 +216,11 @@ private IEnumerable<ILoggingConfigurationElement> GetChildren()
214216
215217 private IEnumerable < ILoggingConfigurationElement > GetChildren ( IEnumerable < IConfigurationSection > children , IConfigurationSection variables , bool isTargetsSection )
216218 {
217- var defaultTargetWrapper = GetDefaultWrapperSection ( ) ;
218- var defaultTargetParameters = GetDefaultTargetParametersSection ( ) ;
219+ var targetDefaultWrapper = GetTargetDefaultWrapperSection ( ) ;
220+ var targetDefaultParameters = GetTargetDefaultParametersSection ( ) ;
219221 foreach ( var child in children )
220222 {
221- if ( AlreadyReadChild ( child , variables , defaultTargetWrapper , defaultTargetParameters ) )
223+ if ( AlreadyReadChild ( child , variables , targetDefaultWrapper , targetDefaultParameters ) )
222224 {
223225 continue ;
224226 }
@@ -231,34 +233,23 @@ private IEnumerable<ILoggingConfigurationElement> GetChildren(IEnumerable<IConfi
231233
232234 if ( IsTargetWithinWrapper ( child ) )
233235 {
234- yield return new LoggingConfigurationElement ( firstChildValue , false , TargetKey ) ;
236+ yield return new LoggingConfigurationElement ( firstChildValue , TargetKey ) ;
237+ }
238+ else if ( _topElement && GetConfigKey ( child ) . EqualsOrdinalIgnoreCase ( TargetsKey ) )
239+ {
240+ yield return new LoggingConfigurationElement ( child )
241+ {
242+ TargetDefaultParametersSection = targetDefaultParameters ,
243+ TargetDefaultWrapperSection = targetDefaultWrapper ,
244+ } ;
235245 }
236246 else
237247 {
238- yield return CreateLoggingConfigurationElement ( isTargetsSection , child , defaultTargetParameters , defaultTargetWrapper ) ;
248+ yield return new LoggingConfigurationElement ( child , isTargetsSection ? TargetKey : null ) ;
239249 }
240250 }
241251 }
242252
243- private static LoggingConfigurationElement CreateLoggingConfigurationElement ( bool isTargetsSection , IConfigurationSection child , IConfigurationSection defaultTargetParameters , IConfigurationSection defaultTargetWrapper )
244- {
245- IConfigurationSection defaultTargetParametersSection = null ;
246- IConfigurationSection defaultTargetWrapperSection = null ;
247-
248- if ( defaultTargetParameters != null || defaultTargetWrapper != null )
249- {
250- var isTargetsKey = GetConfigKey ( child ) . EqualsOrdinalIgnoreCase ( TargetsKey ) ;
251- defaultTargetParametersSection = defaultTargetParameters != null && isTargetsKey ? defaultTargetParameters : null ;
252- defaultTargetWrapperSection = defaultTargetWrapper != null && isTargetsKey ? defaultTargetWrapper : null ;
253- }
254-
255- return new LoggingConfigurationElement ( child , false , isTargetsSection ? TargetKey : null )
256- {
257- DefaultTargetParametersSection = defaultTargetParametersSection ,
258- DefaultTargetWrapperSection = defaultTargetWrapperSection ,
259- } ;
260- }
261-
262253 private static string GetConfigKey ( IConfigurationSection child )
263254 {
264255 return child . Key ? . Trim ( ) ?? string . Empty ;
@@ -272,28 +263,26 @@ private bool IsTargetsSection()
272263 /// <summary>
273264 /// Target-config inside Wrapper-Target
274265 /// </summary>
275- /// <param name="child"></param>
276- /// <returns></returns>
277266 private bool IsTargetWithinWrapper ( IConfigurationSection child )
278267 {
279268 return _nameOverride == TargetKey && GetConfigKey ( child ) . EqualsOrdinalIgnoreCase ( TargetKey ) && child . GetChildren ( ) . Count ( ) == 1 ;
280269 }
281270
282271 private IEnumerable < LoggingConfigurationElement > GetTargetsDefaultConfigElements ( )
283272 {
284- if ( DefaultTargetWrapperSection != null )
285- yield return new LoggingConfigurationElement ( DefaultTargetWrapperSection , true , DefaultWrapper ) ;
273+ if ( TargetDefaultWrapperSection != null )
274+ yield return new LoggingConfigurationElement ( TargetDefaultWrapperSection , DefaultWrapper ) ;
286275
287- if ( DefaultTargetParametersSection != null )
276+ if ( TargetDefaultParametersSection != null )
288277 {
289- foreach ( var targetParameters in DefaultTargetParametersSection . GetChildren ( ) )
278+ foreach ( var targetParameters in TargetDefaultParametersSection . GetChildren ( ) )
290279 {
291- yield return new LoggingConfigurationElement ( targetParameters , true , DefaultTargetParameters ) ;
280+ yield return new LoggingConfigurationElement ( targetParameters , DefaultTargetParameters ) ;
292281 }
293282 }
294283 }
295284
296- private bool AlreadyReadChild ( IConfigurationSection child , IConfigurationSection variables , IConfigurationSection defaultWrapper , IConfigurationSection defaultTargetParameters )
285+ private bool AlreadyReadChild ( IConfigurationSection child , IConfigurationSection variables , IConfigurationSection targetDefaultWrapper , IConfigurationSection targetDefaultParameters )
297286 {
298287 if ( _topElement )
299288 {
@@ -302,12 +291,12 @@ private bool AlreadyReadChild(IConfigurationSection child, IConfigurationSection
302291 return true ;
303292 }
304293
305- if ( defaultWrapper != null && child . Key . EqualsOrdinalIgnoreCase ( defaultWrapper . Key ) )
294+ if ( targetDefaultWrapper != null && child . Key . EqualsOrdinalIgnoreCase ( targetDefaultWrapper . Key ) )
306295 {
307296 return true ;
308297 }
309298
310- if ( defaultTargetParameters != null && child . Key . EqualsOrdinalIgnoreCase ( defaultTargetParameters . Key ) )
299+ if ( targetDefaultParameters != null && child . Key . EqualsOrdinalIgnoreCase ( targetDefaultParameters . Key ) )
311300 {
312301 return true ;
313302 }
@@ -322,27 +311,50 @@ private IConfigurationSection GetVariablesSection()
322311 return variables ;
323312 }
324313
325- private IConfigurationSection GetDefaultTargetParametersSection ( )
314+ private IConfigurationSection GetTargetDefaultParametersSection ( )
326315 {
327- var defaultTargetParameters = _topElement ? _configurationSection . GetSection ( DefaultTargetParameters ) : null ;
328- if ( defaultTargetParameters != null && defaultTargetParameters . GetChildren ( ) . Any ( ) )
316+ if ( _topElement )
329317 {
330- return defaultTargetParameters ;
318+ var targetDefaultParameters = _configurationSection . GetSection ( TargetDefaultParameters ) ;
319+ if ( targetDefaultParameters ? . GetChildren ( ) . Any ( ) == true )
320+ {
321+ return targetDefaultParameters ;
322+ }
323+
324+ targetDefaultParameters = _configurationSection . GetSection ( DefaultTargetParameters ) ;
325+ if ( targetDefaultParameters ? . GetChildren ( ) . Any ( ) == true )
326+ {
327+ return targetDefaultParameters ;
328+ }
331329 }
332330
333331 return null ;
334332 }
335333
336- private IConfigurationSection GetDefaultWrapperSection ( )
334+ private IConfigurationSection GetTargetDefaultWrapperSection ( )
337335 {
338- var defaultWrapper = _topElement ? _configurationSection . GetSection ( DefaultWrapper ) : null ;
339- if ( defaultWrapper != null && defaultWrapper . GetChildren ( ) . Any ( ) )
336+ if ( _topElement )
340337 {
341- return defaultWrapper ;
338+ var targetDefaultWrapper = _configurationSection . GetSection ( TargetDefaultWrapper ) ;
339+ if ( targetDefaultWrapper ? . GetChildren ( ) . Any ( ) == true )
340+ {
341+ return targetDefaultWrapper ;
342+ }
343+
344+ targetDefaultWrapper = _configurationSection . GetSection ( DefaultWrapper ) ;
345+ if ( targetDefaultWrapper ? . GetChildren ( ) . Any ( ) == true )
346+ {
347+ return targetDefaultWrapper ;
348+ }
342349 }
343350
344351 return null ;
345352 }
353+
354+ public override string ToString ( )
355+ {
356+ return Name ;
357+ }
346358 }
347359 }
348360}
0 commit comments