diff --git a/EonaCat.LogStack/EonaCat.LogStack.csproj b/EonaCat.LogStack/EonaCat.LogStack.csproj index 3628e05..f2d0491 100644 --- a/EonaCat.LogStack/EonaCat.LogStack.csproj +++ b/EonaCat.LogStack/EonaCat.LogStack.csproj @@ -14,7 +14,7 @@ It features a rich fluent API for routing log events to dozens of destinations f EonaCat (Jeroen Saey) EonaCat;Logger;EonaCatLogStack;Log;Writer;Flows;LogStack;Memory;Speed;Jeroen;Saey - 0.0.4 + 0.0.5 README.md True LICENSE @@ -25,7 +25,7 @@ It features a rich fluent API for routing log events to dozens of destinations f - 0.0.4+{chash:10}.{c:ymd} + 0.0.5+{chash:10}.{c:ymd} true true v[0-9]* @@ -36,7 +36,7 @@ It features a rich fluent API for routing log events to dozens of destinations f - 0.0.4 + 0.0.5 EonaCat.LogStack EonaCat.LogStack https://git.saey.me/EonaCat/EonaCat.LogStack diff --git a/EonaCat.LogStack/EonaCatLoggerCore/Flows/FileFlow.cs b/EonaCat.LogStack/EonaCatLoggerCore/Flows/FileFlow.cs index 4dcdbdf..9670ba4 100644 --- a/EonaCat.LogStack/EonaCatLoggerCore/Flows/FileFlow.cs +++ b/EonaCat.LogStack/EonaCatLoggerCore/Flows/FileFlow.cs @@ -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++; + } } } } diff --git a/EonaCat.LogStack/EonaCatLoggerCore/Policies/FileRetentionPolicy.cs b/EonaCat.LogStack/EonaCatLoggerCore/Policies/FileRetentionPolicy.cs index 4f0dd70..7f7c841 100644 --- a/EonaCat.LogStack/EonaCatLoggerCore/Policies/FileRetentionPolicy.cs +++ b/EonaCat.LogStack/EonaCatLoggerCore/Policies/FileRetentionPolicy.cs @@ -9,6 +9,9 @@ namespace EonaCat.LogStack.EonaCatLogStackCore.Policies /// Maximum number of rolled archive files to keep (0 = unlimited). public int MaxRolledFiles { get; set; } + /// Maximum number of compressed .gz files to keep (0 = unlimited). + public int MaxCompressedFiles { get; set; } + /// Maximum total size of all archives in bytes (0 = unlimited). public long MaxTotalArchiveBytes { get; set; } @@ -18,6 +21,7 @@ namespace EonaCat.LogStack.EonaCatLogStackCore.Policies public FileRetentionPolicy() { MaxRolledFiles = 10; + MaxCompressedFiles = 5; MaxTotalArchiveBytes = 0; MaxAgeDays = 0; }