Skip to content

Commit 00f0583

Browse files
authored
FileTarget - Close old files when reaching OpenFileCacheSize (#5952)
1 parent abc9507 commit 00f0583

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

src/NLog/Targets/ConsoleWordHighlightingRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ConsoleWordHighlightingRule(string text, ConsoleOutputColor foregroundCol
7272
public ConditionExpression? Condition { get; set; }
7373

7474
/// <summary>
75-
/// Gets or sets the text to be matched. You must specify either <c>text</c> or <c>regex</c>.
75+
/// Gets or sets the text to be matched.
7676
/// </summary>
7777
/// <remarks>Default: <see cref="string.Empty"/></remarks>
7878
/// <docgen category='Highlighting Rules' order='10' />

src/NLog/Targets/FileTarget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ private void PruneOpenFileCache()
941941
oldestOpenFile = oldOpenFile;
942942
}
943943
}
944-
if (!string.IsNullOrEmpty(oldestOpenFile.Key))
944+
if (string.IsNullOrEmpty(oldestOpenFile.Key))
945945
break;
946946

947947
CloseFileWithFooter(oldestOpenFile.Key, oldestOpenFile.Value, false);

tests/NLog.Targets.ConcurrentFile.Tests/ConcurrentFileTargetTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ public void RollingArchiveEveryMonth()
403403
ArchiveNumbering = ArchiveNumberingMode.Rolling,
404404
ArchiveEvery = FileArchiveEveryPeriod.Month,
405405
MaxArchiveFiles = 1,
406+
EnableFileDelete = false,
406407
};
407408

408409
LogManager.Setup().LoadConfiguration(c => c.ForLogger().WriteTo(fileTarget));
@@ -426,6 +427,20 @@ public void RollingArchiveEveryMonth()
426427
Assert.Equal(14, Path.GetFileName(file).Length);
427428
}
428429

430+
var fileOpenList = files.Where(f =>
431+
{
432+
try
433+
{
434+
File.Delete(f);
435+
return false;
436+
}
437+
catch
438+
{
439+
return true;
440+
}
441+
}).ToList();
442+
Assert.Equal(fileTarget.OpenFileCacheSize, fileOpenList.Count);
443+
429444
LogManager.Configuration = null; // Flush and close
430445
}
431446
finally

tests/NLog.UnitTests/Targets/AsyncTaskTargetTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ class AsyncTaskTestTarget : AsyncTaskTarget
5555

5656
public Type RequiredDependency { get; set; }
5757

58-
public bool WaitForWriteEvent(int timeoutMilliseconds = 1000)
58+
public bool WaitForWriteEvent(int timeoutMilliseconds = 5000)
5959
{
6060
#if DEBUG
6161
if (System.Diagnostics.Debugger.IsAttached)
62-
timeoutMilliseconds = timeoutMilliseconds * 60;
62+
timeoutMilliseconds = timeoutMilliseconds * 10;
6363
#endif
6464
if (_writeEvent.WaitOne(TimeSpan.FromMilliseconds(timeoutMilliseconds)))
6565
{

tests/NLog.UnitTests/Targets/FileTargetTests.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ public void RollingArchiveEveryMonday(bool keepFileOpen)
360360
ArchiveEvery = FileArchivePeriod.Monday,
361361
MaxArchiveFiles = 1,
362362
KeepFileOpen = keepFileOpen,
363+
EnableFileDelete = false,
363364
};
364365

365366
LogManager.Setup().LoadConfiguration(c => c.ForLogger().WriteTo(fileTarget));
@@ -373,10 +374,17 @@ public void RollingArchiveEveryMonday(bool keepFileOpen)
373374
}
374375
}
375376

377+
LogManager.Flush();
378+
379+
Assert.True(File.Exists(Path.Combine(tempDir, "OO_AppName.log")));
380+
Assert.Equal(3, new FileInfo(Path.Combine(tempDir, "OO_AppName.log")).Length);
381+
File.Delete(Path.Combine(tempDir, "OO_AppName.log"));
382+
Assert.False(File.Exists(Path.Combine(tempDir, "OO_AppName.log")));
383+
376384
LogManager.Configuration = null; // Flush and close
377385

378386
var files = new DirectoryInfo(tempDir).GetFiles();
379-
Assert.Equal(25, files.Length);
387+
Assert.Equal(24, files.Length);
380388

381389
foreach (var file in files)
382390
{
@@ -2318,7 +2326,9 @@ public void MultiFileWrite()
23182326
{
23192327
FileName = Path.Combine(tempDir, "${level}.txt"),
23202328
LineEnding = LineEndingMode.LF,
2321-
Layout = "${message}"
2329+
Layout = "${message}",
2330+
OpenFileCacheSize = 4,
2331+
EnableFileDelete = false,
23222332
};
23232333

23242334
LogManager.Setup().LoadConfiguration(c => c.ForLogger(LogLevel.Debug).WriteTo(fileTarget));
@@ -2334,7 +2344,7 @@ public void MultiFileWrite()
23342344
logger.Fatal("eee");
23352345
}
23362346

2337-
LogManager.Configuration = null; // Flush
2347+
LogManager.Configuration = null; // Flush and close
23382348

23392349
Assert.False(File.Exists(Path.Combine(tempDir, "Trace.txt")));
23402350

@@ -2370,7 +2380,8 @@ public void BufferedMultiFileWrite()
23702380
{
23712381
FileName = Path.Combine(tempDir, "${level}.txt"),
23722382
LineEnding = LineEndingMode.LF,
2373-
Layout = "${message}"
2383+
Layout = "${message}",
2384+
OpenFileCacheSize = 4,
23742385
};
23752386

23762387
LogManager.Setup().LoadConfiguration(c => c.ForLogger(LogLevel.Debug).WriteTo(new BufferingTargetWrapper(fileTarget, 10)));
@@ -2386,7 +2397,7 @@ public void BufferedMultiFileWrite()
23862397
logger.Fatal("eee");
23872398
}
23882399

2389-
LogManager.Configuration = null; // Flush
2400+
LogManager.Configuration = null; // Flush and close
23902401

23912402
Assert.False(File.Exists(Path.Combine(tempDir, "Trace.txt")));
23922403

@@ -2422,7 +2433,8 @@ public void AsyncMultiFileWrite()
24222433
{
24232434
FileName = Path.Combine(tempDir, "${level}.txt"),
24242435
LineEnding = LineEndingMode.LF,
2425-
Layout = "${message} ${threadid}"
2436+
Layout = "${message} ${threadid}",
2437+
OpenFileCacheSize = 4,
24262438
};
24272439

24282440
// this also checks that thread-volatile layouts
@@ -2443,7 +2455,7 @@ public void AsyncMultiFileWrite()
24432455
logger.Fatal("eee");
24442456
}
24452457

2446-
LogManager.Configuration = null; // Flush
2458+
LogManager.Configuration = null; // Flush and close
24472459

24482460
Assert.False(File.Exists(Path.Combine(tempDir, "Trace.txt")));
24492461

0 commit comments

Comments
 (0)