@@ -36,7 +36,16 @@ public NLogLoggingConfiguration(IConfigurationSection nlogConfig, LogFactory log
3636 : base ( logFactory )
3737 {
3838 _originalConfigSection = nlogConfig ;
39- _autoReload = LoadConfigurationSection ( nlogConfig ) ;
39+ LoadConfigurationSection ( nlogConfig ) ;
40+ }
41+
42+ /// <summary>
43+ /// Initializes a new instance of the <see cref="NLogLoggingConfiguration" /> class.
44+ /// </summary>
45+ private NLogLoggingConfiguration ( LogFactory logFactory , IConfigurationSection nlogConfig )
46+ : base ( logFactory )
47+ {
48+ _originalConfigSection = nlogConfig ;
4049 }
4150
4251 /// <summary>
@@ -60,14 +69,22 @@ public override IEnumerable<string> FileNamesToWatch
6069 /// <inheritdoc />
6170 public override LoggingConfiguration Reload ( )
6271 {
63- return new NLogLoggingConfiguration ( _originalConfigSection , LogFactory ) ;
72+ return ReloadLoggingConfiguration ( _originalConfigSection ) ;
73+ }
74+
75+ private LoggingConfiguration ReloadLoggingConfiguration ( IConfigurationSection nlogConfig )
76+ {
77+ var newConfig = new NLogLoggingConfiguration ( LogFactory , nlogConfig ) ;
78+ newConfig . PrepareForReload ( this ) ; // Ensure KeepVariablesOnReload works as intended
79+ newConfig . LoadConfigurationSection ( nlogConfig ) ;
80+ return newConfig ;
6481 }
6582
66- private bool LoadConfigurationSection ( IConfigurationSection nlogConfig )
83+ private void LoadConfigurationSection ( IConfigurationSection nlogConfig )
6784 {
6885 var configElement = new LoggingConfigurationElement ( nlogConfig , RootSectionKey ) ;
6986 LoadConfig ( configElement , null ) ;
70- return configElement . AutoReload ;
87+ _autoReload = configElement . AutoReload ;
7188 }
7289
7390 private void LogFactory_ConfigurationChanged ( object sender , LoggingConfigurationChangedEventArgs e )
@@ -100,15 +117,10 @@ private void ReloadConfigurationSection(IConfigurationSection nlogConfig)
100117 }
101118
102119 InternalLogger . Info ( "Reloading NLogLoggingConfiguration..." ) ;
103- var newConfig = new NLogLoggingConfiguration ( nlogConfig , LogFactory ) ;
120+ var newConfig = ReloadLoggingConfiguration ( nlogConfig ) ;
104121 var oldConfig = LogFactory . Configuration ;
105122 if ( oldConfig != null )
106123 {
107- if ( LogFactory . KeepVariablesOnReload )
108- {
109- foreach ( var variable in oldConfig . Variables )
110- newConfig . Variables [ variable . Key ] = variable . Value ;
111- }
112124 LogFactory . Configuration = newConfig ;
113125 }
114126 }
0 commit comments