This commit is contained in:
Jeroen Saey
2026-06-01 14:45:01 +02:00
parent 9e7ad2237b
commit ad201a91ff
3 changed files with 49 additions and 4 deletions
+3 -3
View File
@@ -14,7 +14,7 @@ It features a rich fluent API for routing log events to dozens of destinations f
<Copyright>EonaCat (Jeroen Saey)</Copyright>
<PackageTags>EonaCat;Logger;EonaCatLogStack;Log;Writer;Flows;LogStack;Memory;Speed;Jeroen;Saey</PackageTags>
<PackageIconUrl />
<FileVersion>0.0.4</FileVersion>
<FileVersion>0.0.5</FileVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
@@ -25,7 +25,7 @@ It features a rich fluent API for routing log events to dozens of destinations f
</PropertyGroup>
<PropertyGroup>
<EVRevisionFormat>0.0.4+{chash:10}.{c:ymd}</EVRevisionFormat>
<EVRevisionFormat>0.0.5+{chash:10}.{c:ymd}</EVRevisionFormat>
<EVDefault>true</EVDefault>
<EVInfo>true</EVInfo>
<EVTagMatch>v[0-9]*</EVTagMatch>
@@ -36,7 +36,7 @@ It features a rich fluent API for routing log events to dozens of destinations f
</PropertyGroup>
<PropertyGroup>
<Version>0.0.4</Version>
<Version>0.0.5</Version>
<PackageId>EonaCat.LogStack</PackageId>
<Product>EonaCat.LogStack</Product>
<RepositoryUrl>https://git.saey.me/EonaCat/EonaCat.LogStack</RepositoryUrl>
@@ -1732,6 +1732,38 @@ namespace EonaCat.LogStack.Flows
// Delete the original uncompressed file after successful compression
try { File.Delete(path); } catch { /* ignore */ }
EnforceCompressedFileLimit();
}
private void EnforceCompressedFileLimit()
{
if (_retention.MaxCompressedFiles <= 0)
{
return;
}
try
{
DirectoryInfo dir = new DirectoryInfo(_directory);
if (!dir.Exists)
{
return;
}
FileInfo[] compressedFiles = dir.GetFiles("*" + _fileExtension + ".gz")
.OrderByDescending(f => f.LastWriteTimeUtc)
.ToArray();
for (int i = _retention.MaxCompressedFiles; i < compressedFiles.Length; i++)
{
try { compressedFiles[i].Delete(); } catch { /* ignore */ }
}
}
catch (Exception ex)
{
WriteToConsoleError("[FileFlow] Compressed retention error: " + ex.Message);
}
}
private void PeriodicFlushLoop()
@@ -1796,17 +1828,22 @@ namespace EonaCat.LogStack.Flows
long totalBytes = 0;
int kept = 0;
int keptCompressed = 0;
foreach (FileInfo f in files)
{
bool isCompressed = f.Extension.Equals(".gz", StringComparison.OrdinalIgnoreCase);
bool tooOld = _retention.MaxAgeDays > 0
&& (DateTime.UtcNow - f.LastWriteTimeUtc).TotalDays > _retention.MaxAgeDays;
bool tooMany = _retention.MaxRolledFiles > 0 && kept >= _retention.MaxRolledFiles;
bool tooManyCompressed = isCompressed
&& _retention.MaxCompressedFiles > 0
&& keptCompressed >= _retention.MaxCompressedFiles;
bool tooLarge = _retention.MaxTotalArchiveBytes > 0
&& totalBytes + f.Length > _retention.MaxTotalArchiveBytes;
bool directoryTooLarge = totalBytes + f.Length > _maxDirectorySize;
if (tooOld || tooMany || tooLarge || directoryTooLarge)
if (tooOld || tooMany || tooManyCompressed || tooLarge || directoryTooLarge)
{
try { f.Delete(); } catch { /* ignore */ }
}
@@ -1814,6 +1851,10 @@ namespace EonaCat.LogStack.Flows
{
totalBytes += f.Length;
kept++;
if (isCompressed)
{
keptCompressed++;
}
}
}
}
@@ -9,6 +9,9 @@ namespace EonaCat.LogStack.EonaCatLogStackCore.Policies
/// <summary>Maximum number of rolled archive files to keep (0 = unlimited).</summary>
public int MaxRolledFiles { get; set; }
/// <summary>Maximum number of compressed .gz files to keep (0 = unlimited).</summary>
public int MaxCompressedFiles { get; set; }
/// <summary>Maximum total size of all archives in bytes (0 = unlimited).</summary>
public long MaxTotalArchiveBytes { get; set; }
@@ -18,6 +21,7 @@ namespace EonaCat.LogStack.EonaCatLogStackCore.Policies
public FileRetentionPolicy()
{
MaxRolledFiles = 10;
MaxCompressedFiles = 5;
MaxTotalArchiveBytes = 0;
MaxAgeDays = 0;
}