Skip to content

Commit

Permalink
Merge pull request #1456 from bhaeussermann/branch0
Browse files Browse the repository at this point in the history
FileTarget: Fix premature termination of appender-invalidator-thread.
  • Loading branch information
304NotModified committed May 12, 2016
2 parents 7a0d82d + 502a89a commit 754d190
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 33 deletions.
25 changes: 6 additions & 19 deletions src/NLog/Internal/FileAppenders/FileAppenderCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal sealed class FileAppenderCache
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
private string archiveFilePatternToWatch = null;
private readonly MultiFileWatcher externalFileArchivingWatcher = new MultiFileWatcher(NotifyFilters.FileName);
private readonly ManualResetEvent logFileArchiveWaitHandle = new ManualResetEvent(false);
private bool logFileWasArchived = false;
#endif

/// <summary>
Expand Down Expand Up @@ -87,22 +87,9 @@ public FileAppenderCache(int size, IFileAppenderFactory appenderFactory, ICreate
private void ExternalFileArchivingWatcher_OnChange(object sender, FileSystemEventArgs e)
{
if ((e.ChangeType & WatcherChangeTypes.Created) == WatcherChangeTypes.Created)
{
LogFileWasArchived = true;
logFileArchiveWaitHandle.Set();
}
}

public bool LogFileWasArchived
{
get; private set;
logFileWasArchived = true;
}

public EventWaitHandle LogArchiveWaitHandle
{
get { return logFileArchiveWaitHandle; }
}


/// <summary>
/// The archive file path pattern that is used to detect when archiving occurs.
/// </summary>
Expand All @@ -115,7 +102,7 @@ public string ArchiveFilePatternToWatch
{
archiveFilePatternToWatch = value;

LogFileWasArchived = false;
logFileWasArchived = false;
externalFileArchivingWatcher.StopWatching();
}
}
Expand All @@ -126,10 +113,10 @@ public string ArchiveFilePatternToWatch
/// </summary>
public void InvalidateAppendersForInvalidFiles()
{
if (LogFileWasArchived)
if (logFileWasArchived)
{
CloseAppenders();
LogFileWasArchived = false;
logFileWasArchived = false;
}
}
#endif
Expand Down
18 changes: 4 additions & 14 deletions src/NLog/Targets/FileTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public class FileTarget : TargetWithLayoutHeaderAndFooter, ICreateFileParameters

#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
private Thread appenderInvalidatorThread = null;
private EventWaitHandle stopAppenderInvalidatorThreadWaitHandle = new ManualResetEvent(false);
#endif

/// <summary>
Expand Down Expand Up @@ -724,16 +725,11 @@ private void RefreshArchiveFilePatternToWatch()
{
try
{
this.fileAppenderCache.LogArchiveWaitHandle.WaitOne(200);
if (this.stopAppenderInvalidatorThreadWaitHandle.WaitOne(200))
break;
lock (SyncRoot)
{
if (!this.fileAppenderCache.LogFileWasArchived)
{
//ThreadAbortException will be automatically re-thrown at the end of the try/catch/finally if ResetAbort isn't called.
break;
}
this.fileAppenderCache.InvalidateAppendersForInvalidFiles();
}
}
Expand Down Expand Up @@ -763,14 +759,8 @@ private void StopAppenderInvalidatorThread()
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
if (this.appenderInvalidatorThread != null)
{

if (this.fileAppenderCache.LogFileWasArchived)
this.fileAppenderCache.InvalidateAppendersForInvalidFiles();

this.fileAppenderCache.LogArchiveWaitHandle.Set();

this.stopAppenderInvalidatorThreadWaitHandle.Set();
this.appenderInvalidatorThread = null;

}
#endif
}
Expand Down

0 comments on commit 754d190

Please sign in to comment.