Skip to content

Commit 2f49f8f

Browse files
gdziadkiewiczFreeAndNil
authored andcommitted
Add tests and fix identified issues
1 parent 17ac3f7 commit 2f49f8f

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

src/log4net.Tests/Appender/RollingFileAppenderTest.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ namespace log4net.Tests.Appender;
4040
/// Used for internal unit testing the <see cref="RollingFileAppender"/> class.
4141
/// </summary>
4242
[TestFixture]
43-
public sealed class RollingFileAppenderTest
43+
public class RollingFileAppenderTest
4444
{
45-
private const string FileName = "test_41d3d834_4320f4da.log";
45+
protected string FileName = "test_41d3d834_4320f4da.log";
4646

4747
private const string TestMessage98Chars =
4848
"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567";
@@ -99,7 +99,7 @@ private void InitializeVariables()
9999
/// with all appenders, and deletes any test files used
100100
/// for logging.
101101
/// </summary>
102-
private static void ResetAndDeleteTestFiles()
102+
private void ResetAndDeleteTestFiles()
103103
{
104104
// Regular users should not use the clear method lightly!
105105
LogManager.GetRepository().ResetConfiguration();
@@ -144,7 +144,7 @@ public void TearDown()
144144
/// Finds the number of files that match the base file name,
145145
/// and matches the result against an expected count
146146
/// </summary>
147-
private static void VerifyFileCount(int expectedCount, bool preserveLogFileNameExtension = false)
147+
private void VerifyFileCount(int expectedCount, bool preserveLogFileNameExtension = false)
148148
{
149149
List<string> files = GetExistingFiles(FileName, preserveLogFileNameExtension);
150150
Assert.That(files, Is.Not.Null);
@@ -154,7 +154,7 @@ private static void VerifyFileCount(int expectedCount, bool preserveLogFileNameE
154154
/// <summary>
155155
/// Creates a file with the given number, and the shared base file name
156156
/// </summary>
157-
private static void CreateFile(int fileNumber)
157+
private void CreateFile(int fileNumber)
158158
{
159159
FileInfo fileInfo = new(MakeFileName(FileName, fileNumber));
160160

@@ -240,7 +240,7 @@ public void RollingCombinedWithPreserveExtension()
240240
/// <summary>
241241
/// Removes all test files that exist
242242
/// </summary>
243-
private static void DeleteTestFiles()
243+
private void DeleteTestFiles()
244244
{
245245
List<string> files = GetExistingFiles(FileName);
246246
files.AddRange(GetExistingFiles(FileName, true));
@@ -469,7 +469,7 @@ private static int MessagesPerFile(int messageLength)
469469
/// Current file name is always the base file name when counting. Dates will need a different approach.
470470
/// </summary>
471471
/// <returns></returns>
472-
private static string GetCurrentFile() => FileName;
472+
private string GetCurrentFile() => FileName;
473473

474474
/// <summary>
475475
/// Turns a group of file names into an array of file entries that include the name
@@ -604,7 +604,7 @@ private static RollConditions BuildTableEntry(string backupFiles,
604604
/// <param name="rollingStats"></param>
605605
/// <param name="currentNext"></param>
606606
/// <returns></returns>
607-
private static RollFileEntry MoveNextEntry(RollingStats rollingStats, RollFileEntry currentNext)
607+
private RollFileEntry MoveNextEntry(RollingStats rollingStats, RollFileEntry currentNext)
608608
{
609609
rollingStats.MessagesThisFile++;
610610
if (rollingStats.MessagesThisFile >= rollingStats.MessagesPerFile)
@@ -621,7 +621,7 @@ private static RollFileEntry MoveNextEntry(RollingStats rollingStats, RollFileEn
621621
/// Callback point for the regular expression parser. Turns
622622
/// the number into a file name.
623623
/// </summary>
624-
private static string NumberedNameMaker(Match match)
624+
private string NumberedNameMaker(Match match)
625625
=> MakeFileName(FileName, int.Parse(match.Value));
626626

627627
/// <summary>
@@ -642,7 +642,7 @@ private static string ConvertToFiles(string backupInfo, MatchEvaluator evaluator
642642
/// that results after each message is logged</param>
643643
/// <param name="messagesToLog">How many times the test message will be repeatedly logged</param>
644644
/// <returns></returns>
645-
private static RollConditions[] MakeNumericTestEntries(
645+
private RollConditions[] MakeNumericTestEntries(
646646
string testMessage,
647647
string backupInfo,
648648
int messagesToLog)
@@ -660,7 +660,7 @@ private static RollConditions[] MakeNumericTestEntries(
660660
/// <param name="messagesToLog">How many times the test message will be repeatedly logged</param>
661661
/// <param name="evaluator">Function that can turn a number into a filename</param>
662662
/// <returns></returns>
663-
private static RollConditions[] MakeTestEntries(
663+
private RollConditions[] MakeTestEntries(
664664
string testMessage,
665665
string backupInfo,
666666
int messagesToLog,
@@ -1592,7 +1592,7 @@ private static void VerifyInitializeDownFixedExpectedValue(List<string> files, s
15921592
///
15931593
/// </summary>
15941594
/// <param name="fileNumbers">Comma separated list of numbers for counted file names</param>
1595-
private static List<string> MakeTestDataFromString(string fileNumbers)
1595+
private List<string> MakeTestDataFromString(string fileNumbers)
15961596
=> MakeTestDataFromString(FileName, fileNumbers);
15971597

15981598
/// <summary>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#region Apache License
2+
3+
//
4+
// Licensed to the Apache Software Foundation (ASF) under one or more
5+
// contributor license agreements. See the NOTICE file distributed with
6+
// this work for additional information regarding copyright ownership.
7+
// The ASF licenses this file to you under the Apache License, Version 2.0
8+
// (the "License"); you may not use this file except in compliance with
9+
// the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
20+
#endregion
21+
22+
using System.IO;
23+
using NUnit.Framework;
24+
25+
namespace log4net.Tests.Appender;
26+
27+
[TestFixture]
28+
public class RollingFileAppenderWithDirTest : RollingFileAppenderTest {
29+
public RollingFileAppenderWithDirTest()
30+
{
31+
base.FileName = Path.Combine(@".\dir\", base.FileName);
32+
}
33+
}

src/log4net/Appender/RollingFileAppender.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ protected List<string> GetExistingFiles(string baseFilePath)
681681
using (SecurityContext?.Impersonate(this))
682682
{
683683
string fullPath = Path.GetFullPath(baseFilePath);
684+
string dir = Path.GetDirectoryName(baseFilePath);
684685

685686
directory = Path.GetDirectoryName(fullPath);
686687
if (Directory.Exists(directory))
@@ -690,7 +691,8 @@ protected List<string> GetExistingFiles(string baseFilePath)
690691
string[] files = Directory.GetFiles(directory, GetWildcardPatternForFile(baseFileName));
691692
result.AddRange(files
692693
.Select(Path.GetFileName)
693-
.Where(curFileName => curFileName.StartsWith(Path.GetFileNameWithoutExtension(baseFileName))));
694+
.Where(curFileName => curFileName.StartsWith(Path.GetFileNameWithoutExtension(baseFileName)))
695+
.Select(file => Path.Combine(dir, file)));
694696
}
695697
}
696698
LogLog.Debug(_declaringType, "Searched for existing files in [" + directory + "]");
@@ -789,7 +791,9 @@ private void InitializeFromOneFile(string baseFile, string curFileName)
789791
{
790792
curFileName = curFileName.ToLowerInvariant();
791793
baseFile = baseFile.ToLowerInvariant();
792-
if (curFileName.StartsWith(Path.GetFileNameWithoutExtension(baseFile)) == false)
794+
var baseFileWithoutExtension = Path.Combine(Path.GetDirectoryName(baseFile), Path.GetFileNameWithoutExtension(baseFile));
795+
796+
if (curFileName.StartsWith(baseFileWithoutExtension) == false)
793797
{
794798
return; // This is not a log file, so ignore
795799
}

0 commit comments

Comments
 (0)