diff --git a/src/changelog/3.5.1/338-size-backup-ignores-prefix-sibling.xml b/src/changelog/3.5.1/338-size-backup-ignores-prefix-sibling.xml new file mode 100644 index 00000000..3b7cbcb8 --- /dev/null +++ b/src/changelog/3.5.1/338-size-backup-ignores-prefix-sibling.xml @@ -0,0 +1,12 @@ + + + + + Without date rolling, a `RollingFileAppender` with `StaticLogFileName` + false counted a file such as `log.txt.errors.3` as backup 3 of `log.txt` when it started. Only + files whose name is the base name followed by the index count as backups now (reported by + @dajiaohuang, fixed by @dajiaohuang) + diff --git a/src/log4net.Tests/Appender/RollingFileAppenderTest.cs b/src/log4net.Tests/Appender/RollingFileAppenderTest.cs index 8ef01257..588436c9 100644 --- a/src/log4net.Tests/Appender/RollingFileAppenderTest.cs +++ b/src/log4net.Tests/Appender/RollingFileAppenderTest.cs @@ -1799,6 +1799,30 @@ public void TestInitializeRollBackupsNotStaticDateWithoutDashes(string baseFile, Assert.That(rfa.CurrentSizeRollBackups, Is.EqualTo(1)); } + /// + /// Tests that without date rolling a sibling such as log.txt.errors.3 is not counted as a backup + /// of a non-static name + /// + [TestCase(true, "log.5.txt", 5)] + [TestCase(false, "log.txt.5", 5)] + [TestCase(true, "log.errors.3.txt", 0)] + [TestCase(false, "log.txt.errors.3", 0)] + public void TestInitializeRollBackupsSizeIgnoresPrefixSibling(bool preserveExtension, + string file, int expected) + { + RollingFileAppender rfa = new() + { + RollingStyle = RollingFileAppender.RollingMode.Size, + StaticLogFileName = false, + PreserveLogFileNameExtension = preserveExtension, + MaxSizeRollBackups = -1, + CurrentSizeRollBackups = 0 + }; + rfa.InitializeRollBackups("log.txt", [file]); + + Assert.That(rfa.CurrentSizeRollBackups, Is.EqualTo(expected)); + } + /// /// Tests that with a short date pattern the suffixes of a static file name count as backup /// indexes, as a date like .10 cannot be told apart from backup 10 diff --git a/src/log4net/Appender/RollingFileAppender.cs b/src/log4net/Appender/RollingFileAppender.cs index 15dc84a7..c6b17a31 100644 --- a/src/log4net/Appender/RollingFileAppender.cs +++ b/src/log4net/Appender/RollingFileAppender.cs @@ -971,8 +971,8 @@ private int GetBackupIndex(string baseFile, string curFileName) int index = fileName.LastIndexOf(".", StringComparison.Ordinal); if (index > 0) { - // with a static name, log.txt.20261003.1 is a backup of an earlier date - if (StaticLogFileName + // without dates log.txt.errors.3 is another file (#338) + if ((StaticLogFileName || !_rollDate) && (!string.Equals(fileName.Substring(0, index), baseFile, StringComparison.Ordinal) || IsDateSuffix(fileName.Substring(index)))) {