Skip to content

Commit

Permalink
FileTarget - KeepFileOpen should watch for file deletio. Avoid time e…
Browse files Browse the repository at this point in the history
…very second. Restore OpenFileCacheTimeout timer when needed.
  • Loading branch information
snakefoot committed Feb 4, 2017
1 parent fc487be commit 823b137
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/NLog/Internal/FileAppenders/FileAppenderCache.cs
Expand Up @@ -115,7 +115,7 @@ private void ExternalFileArchivingWatcher_OnFileChanged(object sender, FileSyste
if (logFileWasArchived)
{
if (autoClosingTimer != null)
autoClosingTimer.Change(50, 1000);
autoClosingTimer.Change(50, Timeout.Infinite);
}
}

Expand Down Expand Up @@ -297,8 +297,6 @@ public BaseFileAppender AllocateAppender(string fileName)
}
externalFileArchivingWatcher.Watch(appenderToWrite.FileName); // Monitor the active file-appender
#endif
if (freeSpot == 0)
autoClosingTimer.Change(1000, 1000); // Check every second
}
}

Expand Down
23 changes: 19 additions & 4 deletions src/NLog/Targets/FileTarget.cs
Expand Up @@ -100,6 +100,8 @@ public class FileTarget : TargetWithLayoutHeaderAndFooter, ICreateFileParameters
/// </summary>
private FileAppenderCache fileAppenderCache;

private Timer autoClosingTimer;

/// <summary>
/// The number of initialised files at any one time.
/// </summary>
Expand Down Expand Up @@ -750,7 +752,7 @@ private void RefreshArchiveFilePatternToWatch()
this.fileAppenderCache.CheckCloseAppenders -= AutoClosingTimerCallback;

#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
if (KeepFileOpen || OpenFileCacheTimeout > 0)
if (KeepFileOpen)
this.fileAppenderCache.CheckCloseAppenders += AutoClosingTimerCallback;

bool mustWatchArchiving = IsArchivingEnabled() && ConcurrentWrites && KeepFileOpen;
Expand All @@ -773,9 +775,6 @@ private void RefreshArchiveFilePatternToWatch()
{
this.fileAppenderCache.ArchiveFilePatternToWatch = null;
}
#else
if (OpenFileCacheTimeout > 0)
this.fileAppenderCache.CheckCloseAppenders += AutoClosingTimerCallback;
#endif
}
}
Expand Down Expand Up @@ -921,6 +920,15 @@ protected override void InitializeTarget()

this.fileAppenderCache = new FileAppenderCache(this.OpenFileCacheSize, this.appenderFactory, this);
RefreshArchiveFilePatternToWatch();

if ((this.OpenFileCacheSize > 0 || this.EnableFileDelete) && this.OpenFileCacheTimeout > 0)
{
this.autoClosingTimer = new Timer(
(state) => this.AutoClosingTimerCallback(this, EventArgs.Empty),
null,
this.OpenFileCacheTimeout * 1000,
this.OpenFileCacheTimeout * 1000);
}
}

/// <summary>
Expand All @@ -935,6 +943,13 @@ protected override void CloseTarget()
this.FinalizeFile(fileName);
}

if (this.autoClosingTimer != null)
{
this.autoClosingTimer.Change(Timeout.Infinite, Timeout.Infinite);
this.autoClosingTimer.Dispose();
this.autoClosingTimer = null;
}

this.fileAppenderCache.CloseAppenders("Dispose");
this.fileAppenderCache.Dispose();
}
Expand Down

0 comments on commit 823b137

Please sign in to comment.