Skip to content

Commit

Permalink
LogManager.Shutdown - Should disable file watcher and avoid auto reload
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Mar 8, 2018
1 parent b8107ac commit 0fb2b47
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
52 changes: 23 additions & 29 deletions src/NLog/LogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class LogFactory : IDisposable
{
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD1_3
private const int ReconfigAfterFileChangedTimeout = 1000;
internal Timer reloadTimer;
internal Timer _reloadTimer;
private readonly MultiFileWatcher _watcher;
#endif

Expand Down Expand Up @@ -269,17 +269,19 @@ public LoggingConfiguration Configuration
if (oldConfig != null)
{
InternalLogger.Info("Closing old configuration.");
#if !SILVERLIGHT
if (value == null)
{
_config = value;
_configLoaded = false;
ReconfigExistingLoggers(); // Disable all loggers, so things become quiet
}
Flush();
#endif
oldConfig.Close();
}

_config = value;

if (_config == null)
_configLoaded = false;
else
if (_config != null)
{
try
{
Expand Down Expand Up @@ -576,7 +578,6 @@ public void ReconfigExistingLoggers()
}
}

#if !SILVERLIGHT
/// <summary>
/// Flush any pending log messages (in case of asynchronous targets) with the default timeout of 15 seconds.
/// </summary>
Expand All @@ -603,7 +604,6 @@ public void Flush(TimeSpan timeout)
{
throw;
}

}
}

Expand All @@ -616,7 +616,6 @@ public void Flush(int timeoutMilliseconds)
{
Flush(TimeSpan.FromMilliseconds(timeoutMilliseconds));
}
#endif

/// <summary>
/// Flush any pending log messages (in case of asynchronous targets).
Expand Down Expand Up @@ -777,7 +776,7 @@ protected virtual void OnConfigurationReloaded(LoggingConfigurationReloadedEvent
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD1_3
internal void ReloadConfigOnTimer(object state)
{
if (reloadTimer == null && _isDisposing)
if (_reloadTimer == null && _isDisposing)
{
return; //timer was disposed already.
}
Expand All @@ -794,16 +793,16 @@ internal void ReloadConfigOnTimer(object state)
return; //timer was disposed already.
}

var currentTimer = reloadTimer;
var currentTimer = _reloadTimer;
if (currentTimer != null)
{
reloadTimer = null;
_reloadTimer = null;
currentTimer.WaitForDispose(TimeSpan.Zero);
}

_watcher.StopWatching();

if (Configuration != configurationToReload)
if (_config != configurationToReload)
{
throw new NLogConfigurationException("Config changed in between. Not reloading.");
}
Expand Down Expand Up @@ -962,6 +961,7 @@ private void Close(TimeSpan flushTimeout)
{
// Disable startup of new reload-timers
_watcher.FileChanged -= ConfigFileChanged;
_watcher.StopWatching();
}
#endif

Expand All @@ -970,10 +970,10 @@ private void Close(TimeSpan flushTimeout)
try
{
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD1_3
var currentTimer = reloadTimer;
var currentTimer = _reloadTimer;
if (currentTimer != null)
{
reloadTimer = null;
_reloadTimer = null;
currentTimer.WaitForDispose(TimeSpan.Zero);
}

Expand Down Expand Up @@ -1002,9 +1002,9 @@ private void CloseOldConfig(TimeSpan flushTimeout, LoggingConfiguration oldConfi
{
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD1_3 && !MONO
bool attemptClose = true;
if (flushTimeout != TimeSpan.Zero && !PlatformDetector.IsMono)
if (flushTimeout != TimeSpan.Zero && !PlatformDetector.IsMono && !PlatformDetector.IsUnix)
{
// MONO (and friends) have a hard time with spinning up flush threads/timers during shutdown (Maybe better with MONO 4.1)
// MONO (and friends) have a hard time with spinning up flush threads/timers during shutdown
ManualResetEvent flushCompleted = new ManualResetEvent(false);
oldConfig.FlushAllTargets((ex) => flushCompleted.Set());
attemptClose = flushCompleted.WaitOne(flushTimeout);
Expand All @@ -1017,8 +1017,9 @@ private void CloseOldConfig(TimeSpan flushTimeout, LoggingConfiguration oldConfi
#endif
{
// Flush completed within timeout, lets try and close down
oldConfig.Close();
_config = null;
ReconfigExistingLoggers(); // Disable all loggers, so things become quiet
oldConfig.Close();
OnConfigurationChanged(new LoggingConfigurationChangedEventArgs(null, oldConfig));
}
}
Expand Down Expand Up @@ -1046,14 +1047,7 @@ internal void Shutdown()
InternalLogger.Info("Logger closing down...");
if (!_isDisposing && _configLoaded)
{
var loadedConfig = Configuration;
if (loadedConfig != null)
{
ManualResetEvent flushCompleted = new ManualResetEvent(false);
loadedConfig.FlushAllTargets((ex) => flushCompleted.Set());
flushCompleted.WaitOne(DefaultFlushTimeout);
loadedConfig.Close();
}
Configuration = null;
}
InternalLogger.Info("Logger has been closed down.");
}
Expand Down Expand Up @@ -1288,12 +1282,12 @@ private void ConfigFileChanged(object sender, EventArgs args)
return;
}

if (reloadTimer == null)
if (_reloadTimer == null)
{
var configuration = Configuration;
if (configuration != null)
{
reloadTimer = new Timer(
_reloadTimer = new Timer(
ReloadConfigOnTimer,
configuration,
ReconfigAfterFileChangedTimeout,
Expand All @@ -1302,7 +1296,7 @@ private void ConfigFileChanged(object sender, EventArgs args)
}
else
{
reloadTimer.Change(
_reloadTimer.Change(
ReconfigAfterFileChangedTimeout,
Timeout.Infinite);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/NLog.UnitTests/Config/ReloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ public override LoggingConfiguration Reload()
var factory = LogManager.LogFactory;

//reloadTimer should be set for ReloadConfigOnTimer
factory.reloadTimer = new Timer((a) => { });
factory._reloadTimer = new Timer((a) => { });
factory.ReloadConfigOnTimer(this);
_reloading = false;
return factory.Configuration;
Expand Down

0 comments on commit 0fb2b47

Please sign in to comment.