Skip to content

Commit

Permalink
FileTarget - Handle archive file already exists by appending should n…
Browse files Browse the repository at this point in the history
…ot just truncate file, but delete file if possible
  • Loading branch information
snakefoot committed Jun 28, 2018
1 parent efc08aa commit 776de3b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
23 changes: 22 additions & 1 deletion src/NLog/Targets/FileTarget.cs
Expand Up @@ -1295,19 +1295,40 @@ private void ArchiveFile(string fileName, string archiveFileName)
//todo maybe needs a better filelock behaviour

//copy to archive file.
using (FileStream fileStream = File.Open(fileName, FileMode.Open))
var fileShare = FileShare.ReadWrite;
if (EnableFileDelete)
{
fileShare |= FileShare.Delete;
}

using (FileStream fileStream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, fileShare))
using (FileStream archiveFileStream = File.Open(archiveFileName, FileMode.Append))
{
fileStream.CopyAndSkipBom(archiveFileStream, Encoding);
//clear old content
fileStream.SetLength(0);

if (EnableFileDelete)
{
// Attempt to delete file to reset File-Creation-Time (Delete under file-lock)
if (!DeleteOldArchiveFile(fileName))
{
fileShare &= ~FileShare.Delete; // Retry after having released file-lock
}
}

fileStream.Close(); // This flushes the content, too.
#if NET3_5
archiveFileStream.Flush();
#else
archiveFileStream.Flush(true);
#endif
}

if ((fileShare & FileShare.Delete) == FileShare.None)
{
DeleteOldArchiveFile(fileName); // Attempt to delete file to reset File-Creation-Time
}
}
else
{
Expand Down
7 changes: 5 additions & 2 deletions tests/NLog.UnitTests/Targets/FileTargetTests.cs
Expand Up @@ -3934,7 +3934,6 @@ private void HandleArchiveFileAlreadyExistsTest(Encoding encoding, bool hasBom)
// set log file access times the same way as when this issue comes up.
Directory.CreateDirectory(tempDir);


File.WriteAllText(logFile, "some content" + Environment.NewLine, encoding);
var oldTime = DateTime.Now.AddDays(-2);
File.SetCreationTime(logFile, oldTime);
Expand All @@ -3959,7 +3958,9 @@ private void HandleArchiveFileAlreadyExistsTest(Encoding encoding, bool hasBom)
ArchiveFileName = archiveFileNamePattern,
ArchiveNumbering = ArchiveNumberingMode.Date,
ArchiveDateFormat = archiveDateFormat,
Encoding = encoding
Encoding = encoding,
Layout = "${message}",
WriteBom = hasBom,
};


Expand All @@ -3972,10 +3973,12 @@ private void HandleArchiveFileAlreadyExistsTest(Encoding encoding, bool hasBom)
var logger = LogManager.GetLogger("HandleArchiveFileAlreadyExistsTest");
// write, this should append.
logger.Info("log to force archiving");
logger.Info("log to same file");

LogManager.Configuration = null; // Flush

AssertFileContents(archiveFileName, "message already in archive" + Environment.NewLine + "some content" + Environment.NewLine, encoding, hasBom);
AssertFileContents(logFile, "log to force archiving" + Environment.NewLine + "log to same file" + Environment.NewLine, encoding, hasBom);
}
finally
{
Expand Down

0 comments on commit 776de3b

Please sign in to comment.