in src/log4net/Appender/RollingFileAppender.cs [790:870]
private void InitializeFromOneFile(string baseFile, string curFileName)
{
curFileName = curFileName.ToLowerInvariant();
baseFile = baseFile.ToLowerInvariant();
var baseFileWithoutExtension = Path.Combine(Path.GetDirectoryName(baseFile) ?? "", Path.GetFileNameWithoutExtension(baseFile));
if (curFileName.StartsWith(baseFileWithoutExtension) == false)
{
return; // This is not a log file, so ignore
}
if (curFileName.Equals(baseFile, StringComparison.Ordinal))
{
return; // Base log file is not an incremented logfile (.1 or .2, etc.)
}
// Only look for files in the current roll point
if (_rollDate && !StaticLogFileName)
{
string date = DateTimeStrategy.Now.ToString(DatePattern, DateTimeFormatInfo.InvariantInfo).ToLowerInvariant();
string prefix = (PreserveLogFileNameExtension
? Path.GetFileNameWithoutExtension(baseFile) + date
: baseFile + date).ToLowerInvariant();
string suffix = PreserveLogFileNameExtension
? Path.GetExtension(baseFile).ToLowerInvariant()
: "";
var curFileNameWithoutDir = Path.GetFileName(curFileName);
if (!curFileNameWithoutDir.StartsWith(prefix) || !curFileNameWithoutDir.EndsWith(suffix))
{
LogLog.Debug(_declaringType, $"Ignoring file [{curFileName}] because it is from a different date period");
return;
}
}
try
{
// Bump the counter up to the highest count seen so far
int backup = GetBackupIndex(curFileName);
// caution: we might get a false positive when certain
// date patterns such as yyyyMMdd are used...those are
// valid number but aren't the kind of back up index
// we're looking for
if (backup > CurrentSizeRollBackups)
{
if (0 == MaxSizeRollBackups)
{
// Stay at zero when zero backups are desired
}
else if (-1 == MaxSizeRollBackups)
{
// Infinite backups, so go as high as the highest value
CurrentSizeRollBackups = backup;
}
else
{
// Backups limited to a finite number
if (CountDirection >= 0)
{
// Go with the highest file when counting up
CurrentSizeRollBackups = backup;
}
else
{
// Clip to the limit when counting down
if (backup <= MaxSizeRollBackups)
{
CurrentSizeRollBackups = backup;
}
}
}
LogLog.Debug(_declaringType, $"File name [{curFileName}] moves current count to [{CurrentSizeRollBackups}]");
}
}
catch (FormatException)
{
//this happens when file.log -> file.log.yyyy-MM-dd which is normal
//when staticLogFileName == false
LogLog.Debug(_declaringType, $"Encountered a backup file not ending in .x [{curFileName}]");
}
}