Fri, Aug 12 2005 1:57 PM
Erwyn van der Meer
Fixed RollingFileSink to circumvent file system tunneling
Monday I blogged about a problem I encountered with the RollingFileSink. Unfortunately deleting the logfile once and waiting for more than 15 seconds before creating a new log file solves the problem only for one day (or whatever age limit you have set). To fix the problem completely I had to change the RollingFileSink. Fortunately that is possible because Hisham Baz released RollingFileSink in source code form.
I fixed the LogRoller class. You can find the code that I changed and added below or you download LogRoller.cs. The line with CreateNewLogFile(); in the PerformRenameRollover method is new and the CreateNewLogFile method itself is new. (Note that the indentation of the code is lost due to problems with our blog engine).
/// <summary>
/// Archive the current log file by renaming it with today's timestamp.
/// Generate a new filename for the current log file using a <see cref="FilenameBuilder"/>.
/// </summary>
public void PerformRenameRollover()
{
Purge();
if (File.Exists(_info.FullName))
{
string newName = _builder.CreateNewFilename();
File.Move(_info.FullName, newName);
CreateNewLogFile();
}
}
/// <summary>
/// Creates a new current log file and explicitly sets its creation <see cref="DateTime"/> to <see cref="DateTime.Now"/>.
/// </summary>
/// <remarks>Explicitly creating the new file and explicitly setting its creation <see cref="DateTime"/> is
/// necessary due to file system tunneling. Due the file system tunneling a new file will get the creation <see cref="DateTime"/>
/// of an older file that existed with the same name but that was deleted or renamed within 15 seconds of
/// the creation operation.</remarks>
private void CreateNewLogFile()
{
FileStream newLogFileStream = File.Create(_info.FullName, 1);
newLogFileStream.Close();
File.SetCreationTime(_info.FullName, DateTime.Now);
}
Filed under: .NET