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
@@ -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;
}