in src/log4net/Appender/RollingFileAppender.cs [1453:1529]
protected void RollOverRenameFiles(string baseFileName)
{
// If maxBackups <= 0, then there is no file renaming to be done.
if (m_maxSizeRollBackups != 0)
{
if (m_countDirection < 0)
{
// Delete the oldest file, to keep Windows happy.
if (m_curSizeRollBackups == m_maxSizeRollBackups)
{
DeleteFile(CombinePath(baseFileName, "." + m_maxSizeRollBackups));
m_curSizeRollBackups--;
}
// Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
for (var i = m_curSizeRollBackups; i >= 1; i--)
{
RollFile((CombinePath(baseFileName, "." + i)), (CombinePath(baseFileName, "." + (i + 1))));
}
m_curSizeRollBackups++;
// Rename fileName to fileName.1
RollFile(baseFileName, CombinePath(baseFileName, ".1"));
}
else
{
//countDirection >= 0
if (m_curSizeRollBackups >= m_maxSizeRollBackups && m_maxSizeRollBackups > 0)
{
//delete the first and keep counting up.
var oldestFileIndex = m_curSizeRollBackups - m_maxSizeRollBackups;
// If static then there is 1 file without a number, therefore 1 less archive
if (m_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!
var archiveFileBaseName = baseFileName;
if (!m_staticLogFileName)
{
if (m_preserveLogFileNameExtension)
{
var extension = Path.GetExtension(archiveFileBaseName);
var baseName = Path.GetFileNameWithoutExtension(archiveFileBaseName);
var lastDotIndex = baseName.LastIndexOf(".");
if (lastDotIndex >= 0)
{
archiveFileBaseName = baseName.Substring(0, lastDotIndex) + extension;
}
}
else
{
var lastDotIndex = archiveFileBaseName.LastIndexOf(".");
if (lastDotIndex >= 0)
{
archiveFileBaseName = archiveFileBaseName.Substring(0, lastDotIndex);
}
}
}
// Delete the archive file
DeleteFile(CombinePath(archiveFileBaseName, "." + oldestFileIndex));
}
if (m_staticLogFileName)
{
m_curSizeRollBackups++;
RollFile(baseFileName, CombinePath(baseFileName, "." + m_curSizeRollBackups));
}
}
}
}