Skip to content

Commit

Permalink
Merge pull request #2776 from snakefoot/FileTargetOpenFileFlushTimeout
Browse files Browse the repository at this point in the history
FileTarget - Introduced OpenFileFlushTimeout to help when AutoFlush = false
  • Loading branch information
304NotModified committed Jul 8, 2018
2 parents 1995801 + 776de3b commit 8770641
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 248 deletions.
38 changes: 0 additions & 38 deletions src/NLog/Internal/FileAppenders/BaseFileAppender.cs
Expand Up @@ -59,12 +59,8 @@ protected BaseFileAppender(string fileName, ICreateFileParameters createParamete
CreateFileParameters = createParameters;
FileName = fileName;
OpenTimeUtc = DateTime.UtcNow; // to be consistent with timeToKill in FileTarget.AutoClosingTimerCallback
LastWriteTimeUtc = DateTime.MinValue;
CaptureLastWriteTime = createParameters.CaptureLastWriteTime;
}

protected bool CaptureLastWriteTime { get; private set; }

/// <summary>
/// Gets the path of the file, including file extension.
/// </summary>
Expand Down Expand Up @@ -100,13 +96,6 @@ internal set
/// <returns>The time the file was last opened.</returns>
public DateTime OpenTimeUtc { get; private set; }

/// <summary>
/// Gets the last time the file associated with the appeander is written. The time returned is in
/// Coordinated Universal Time [UTC] standard.
/// </summary>
/// <returns>The time the file was last written to.</returns>
public DateTime LastWriteTimeUtc { get; private set; }

/// <summary>
/// Gets the file creation parameters.
/// </summary>
Expand Down Expand Up @@ -141,13 +130,6 @@ public void Write(byte[] bytes)
/// <returns>The file creation time.</returns>
public abstract DateTime? GetFileCreationTimeUtc();

/// <summary>
/// Gets the last time the file associated with the appeander is written. The time returned is in Coordinated
/// Universal Time [UTC] standard.
/// </summary>
/// <returns>The time the file was last written to.</returns>
public abstract DateTime? GetFileLastWriteTimeUtc();

/// <summary>
/// Gets the length in bytes of the file associated with the appeander.
/// </summary>
Expand Down Expand Up @@ -175,26 +157,6 @@ protected virtual void Dispose(bool disposing)
}
}

/// <summary>
/// Updates the last write time of the file.
/// </summary>
protected void FileTouched()
{
if (CaptureLastWriteTime)
{
FileTouched(DateTime.UtcNow);
}
}

/// <summary>
/// Updates the last write time of the file to the specified date.
/// </summary>
/// <param name="dateTime">Date and time when the last write occurred in UTC.</param>
protected void FileTouched(DateTime dateTime)
{
LastWriteTimeUtc = dateTime;
}

/// <summary>
/// Creates the file stream.
/// </summary>
Expand Down
Expand Up @@ -60,20 +60,7 @@ public CountingSingleProcessFileAppender(string fileName, ICreateFileParameters
: base(fileName, parameters)
{
var fileInfo = new FileInfo(fileName);
if (fileInfo.Exists)
{
if (CaptureLastWriteTime)
{
FileTouched(fileInfo.GetLastWriteTimeUtc());
}
_currentFileLength = fileInfo.Length;
}
else
{
FileTouched();
_currentFileLength = 0;
}

_currentFileLength = fileInfo.Exists ? fileInfo.Length : 0;
_file = CreateFileStream(false);
}

Expand Down Expand Up @@ -114,7 +101,6 @@ public override void Flush()
}

_file.Flush();
FileTouched();
}

/// <summary>
Expand All @@ -127,16 +113,6 @@ public override void Flush()
return CreationTimeUtc;
}

/// <summary>
/// Gets the last time the file associated with the appeander is written. The time returned is in Coordinated
/// Universal Time [UTC] standard.
/// </summary>
/// <returns>The time the file was last written to.</returns>
public override DateTime? GetFileLastWriteTimeUtc()
{
return LastWriteTimeUtc;
}

/// <summary>
/// Gets the length in bytes of the file associated with the appeander.
/// </summary>
Expand All @@ -161,11 +137,6 @@ public override void Write(byte[] bytes, int offset, int count)

_currentFileLength += count;
_file.Write(bytes, offset, count);

if (CaptureLastWriteTime)
{
FileTouched();
}
}

/// <summary>
Expand Down
81 changes: 38 additions & 43 deletions src/NLog/Internal/FileAppenders/FileAppenderCache.cs
Expand Up @@ -399,7 +399,16 @@ public void FlushAppenders()
break;
}

appender.Flush();
try
{
appender.Flush();
}
catch (Exception ex)
{
InternalLogger.Error(ex, "Failed to flush file '{0}'.", appender.FileName);
InvalidateAppender(appender.FileName)?.Dispose();
throw;
}
}
}

Expand All @@ -418,15 +427,15 @@ private BaseFileAppender GetAppender(string fileName)
return null;
}

public DateTime? GetFileCreationTimeSource(string filePath, bool fallback, DateTime? fallbackTimeSource = null)
public DateTime? GetFileCreationTimeSource(string filePath, DateTime? fallbackTimeSource = null)
{
var appender = GetAppender(filePath);
DateTime? result = null;
if (appender != null)
{
try
{
result = FileCharacteristicsHelper.ValidateFileCreationTime(appender, (f) => f.GetFileCreationTimeUtc(), (f) => fallbackTimeSource ?? f.CreationTimeUtc, (f) => f.GetFileLastWriteTimeUtc());
result = FileCharacteristicsHelper.ValidateFileCreationTime(appender, (f) => f.GetFileCreationTimeUtc(), f => fallbackTimeSource);
if (result.HasValue)
{
// Check if cached value is still valid, and update if not (Will automatically update CreationTimeSource)
Expand All @@ -446,57 +455,45 @@ private BaseFileAppender GetAppender(string fileName)
}
}

if (fallback)
var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
result = FileCharacteristicsHelper.ValidateFileCreationTime(fileInfo, (f) => f.GetCreationTimeUtc(), (f) => fallbackTimeSource, (f) => f.GetLastWriteTimeUtc()).Value;
return Time.TimeSource.Current.FromSystemTime(result.Value);
}
result = FileCharacteristicsHelper.ValidateFileCreationTime(fileInfo, (f) => f.GetCreationTimeUtc(), (f) => fallbackTimeSource, (f) => f.GetLastWriteTimeUtc()).Value;
return Time.TimeSource.Current.FromSystemTime(result.Value);
}

return result;
}

public DateTime? GetFileLastWriteTimeUtc(string filePath, bool fallback)
/// <summary>
/// File Archive Logic uses the File-Creation-TimeStamp to detect if time to archive, and the File-LastWrite-Timestamp to name the archive-file.
/// </summary>
/// <remarks>
/// NLog always closes all relevant appenders during archive operation, so no need to lookup file-appender
/// </remarks>
public DateTime? GetFileLastWriteTimeUtc(string filePath)
{
var appender = GetAppender(filePath);
DateTime? result = null;
if (appender != null)
var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
try
{
result = appender.GetFileLastWriteTimeUtc();
}
catch (Exception ex)
{
InternalLogger.Error(ex, "Failed to get last write time for file '{0}'.", appender.FileName);
InvalidateAppender(appender.FileName)?.Dispose();
throw;
}
}
if (result == null && fallback)
{
var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
return fileInfo.GetLastWriteTimeUtc();
}
return fileInfo.GetLastWriteTimeUtc();
}

return result;
return null;
}

public long? GetFileLength(string filePath, bool fallback)
public long? GetFileLength(string filePath)
{
var appender = GetAppender(filePath);
long? result = null;
if (appender != null)
{
try
{
result = appender.GetFileLength();
var result = appender.GetFileLength();
if (result.HasValue)
{
return result;
}
}
catch (Exception ex)
{
Expand All @@ -505,16 +502,14 @@ private BaseFileAppender GetAppender(string fileName)
throw;
}
}
if (result == null && fallback)

var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
return fileInfo.Length;
}
return fileInfo.Length;
}

return result;
return null;
}

/// <summary>
Expand Down
5 changes: 0 additions & 5 deletions src/NLog/Internal/FileAppenders/ICreateFileParameters.cs
Expand Up @@ -91,11 +91,6 @@ internal interface ICreateFileParameters
Win32FileAttributes FileAttributes { get; }
#endif

/// <summary>
/// Should we capture the last write time of a file?
/// </summary>
bool CaptureLastWriteTime { get; }

/// <summary>
/// Should archive mutex be created?
/// </summary>
Expand Down
17 changes: 0 additions & 17 deletions src/NLog/Internal/FileAppenders/MutexMultiProcessFileAppender.cs
Expand Up @@ -126,10 +126,6 @@ public override void Write(byte[] bytes, int offset, int count)
_fileStream.Seek(0, SeekOrigin.End);
_fileStream.Write(bytes, offset, count);
_fileStream.Flush();
if (CaptureLastWriteTime)
{
FileTouched();
}
}
finally
{
Expand Down Expand Up @@ -176,8 +172,6 @@ public override void Close()
{
_fileStream = null;
}

FileTouched();
}

/// <summary>
Expand All @@ -198,17 +192,6 @@ public override void Flush()
return CreationTimeUtc; // File is kept open, so creation time is static
}

/// <summary>
/// Gets the last time the file associated with the appeander is written. The time returned is in Coordinated
/// Universal Time [UTC] standard.
/// </summary>
/// <returns>The time the file was last written to.</returns>
public override DateTime? GetFileLastWriteTimeUtc()
{
var fileChars = GetFileCharacteristics();
return fileChars.LastWriteTimeUtc;
}

/// <summary>
/// Gets the length in bytes of the file associated with the appeander.
/// </summary>
Expand Down
5 changes: 0 additions & 5 deletions src/NLog/Internal/FileAppenders/NullAppender.cs
Expand Up @@ -61,11 +61,6 @@ public override void Flush()
return DateTime.UtcNow;
}

public override DateTime? GetFileLastWriteTimeUtc()
{
return DateTime.UtcNow;
}

public override long? GetFileLength()
{
return 0;
Expand Down
Expand Up @@ -67,11 +67,6 @@ public override void Write(byte[] bytes, int offset, int count)
{
fileStream.Write(bytes, offset, count);
}

if (CaptureLastWriteTime)
{
FileTouched();
}
}

/// <summary>
Expand Down Expand Up @@ -105,21 +100,6 @@ public override void Close()
return null;
}

/// <summary>
/// Gets the last time the file associated with the appeander is written. The time returned is in Coordinated
/// Universal Time [UTC] standard.
/// </summary>
/// <returns>The time the file was last written to.</returns>
public override DateTime? GetFileLastWriteTimeUtc()
{
FileInfo fileInfo = new FileInfo(FileName);
if (fileInfo.Exists)
{
return fileInfo.GetLastWriteTimeUtc();
}
return null;
}

/// <summary>
/// Gets the length in bytes of the file associated with the appeander.
/// </summary>
Expand Down

0 comments on commit 8770641

Please sign in to comment.