Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/changelog/3.5.1/338-size-backup-ignores-prefix-sibling.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="338" link="https://github.com/apache/logging-log4net/issues/338"/>
<issue id="339" link="https://github.com/apache/logging-log4net/pull/339"/>
<description format="asciidoc">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)</description>
</entry>
24 changes: 24 additions & 0 deletions src/log4net.Tests/Appender/RollingFileAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,30 @@ public void TestInitializeRollBackupsNotStaticDateWithoutDashes(string baseFile,
Assert.That(rfa.CurrentSizeRollBackups, Is.EqualTo(1));
}

/// <summary>
/// Tests that without date rolling a sibling such as log.txt.errors.3 is not counted as a backup
/// of a non-static name
/// </summary>
[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));
}

/// <summary>
/// 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
Expand Down
4 changes: 2 additions & 2 deletions src/log4net/Appender/RollingFileAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
{
Expand Down