in src/log4net/Appender/RollingFileAppender.cs [1306:1384]
protected virtual void RollOverRenameFiles(string baseFileName)
{
baseFileName.EnsureNotNull();
// If maxBackups <= 0, then there is no file renaming to be done.
if (MaxSizeRollBackups == 0)
{
return;
}
if (CountDirection < 0)
{
// Delete the oldest file, to keep Windows happy.
if (CurrentSizeRollBackups == MaxSizeRollBackups)
{
DeleteFile(CombinePath(baseFileName, "." + MaxSizeRollBackups));
CurrentSizeRollBackups--;
}
// Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
for (int i = CurrentSizeRollBackups; i >= 1; i--)
{
RollFile(CombinePath(baseFileName, "." + i), CombinePath(baseFileName, "." + (i + 1)));
}
CurrentSizeRollBackups++;
// Rename fileName to fileName.1
RollFile(baseFileName, CombinePath(baseFileName, ".1"));
}
else
{
//countDirection >= 0
if (CurrentSizeRollBackups >= MaxSizeRollBackups && MaxSizeRollBackups > 0)
{
//delete the first and keep counting up.
int oldestFileIndex = CurrentSizeRollBackups - MaxSizeRollBackups;
// If static then there is 1 file without a number, therefore 1 less archive
if (StaticLogFileName)
{
oldestFileIndex++;
}
// If using a static log file then the base for the numbered sequence is the baseFileName passed in
// If not using a static log file then the baseFileName will already have a numbered postfix which
// we must remove, however it may have a date postfix which we must keep!
string archiveFileBaseName = baseFileName;
if (!StaticLogFileName)
{
if (PreserveLogFileNameExtension)
{
string extension = Path.GetExtension(archiveFileBaseName);
string baseName = Path.Combine(Path.GetDirectoryName(archiveFileBaseName), Path.GetFileNameWithoutExtension(archiveFileBaseName));
int lastDotIndex = baseName.LastIndexOf(".", StringComparison.Ordinal);
if (lastDotIndex >= 0)
{
archiveFileBaseName = baseName.Substring(0, lastDotIndex) + extension;
}
}
else
{
int lastDotIndex = archiveFileBaseName.LastIndexOf(".", StringComparison.Ordinal);
if (lastDotIndex >= 0)
{
archiveFileBaseName = archiveFileBaseName.Substring(0, lastDotIndex);
}
}
}
// Delete the archive file
DeleteFile(CombinePath(archiveFileBaseName, "." + oldestFileIndex));
}
if (StaticLogFileName)
{
CurrentSizeRollBackups++;
RollFile(baseFileName, CombinePath(baseFileName, "." + CurrentSizeRollBackups));
}
}
}