Skip to content

Commit ca8c943

Browse files
authored
feat: support for file retention
#8 support retainedFileCountLimit
2 parents 8eb08c6 + fcc0e5f commit ca8c943

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

src/Serilog.Sinks.File.Archive/ArchiveHooks.cs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.IO.Compression;
5+
using System.Linq;
46
using Serilog.Debugging;
57

68
namespace Serilog.Sinks.File.Archive
@@ -13,6 +15,7 @@ namespace Serilog.Sinks.File.Archive
1315
public class ArchiveHooks : FileLifecycleHooks
1416
{
1517
private readonly CompressionLevel compressionLevel;
18+
private readonly int retainedFileCountLimit;
1619
private readonly string targetDirectory;
1720

1821
/// <summary>
@@ -27,12 +30,26 @@ public class ArchiveHooks : FileLifecycleHooks
2730
public ArchiveHooks(CompressionLevel compressionLevel = CompressionLevel.Fastest, string targetDirectory = null)
2831
{
2932
if (compressionLevel == CompressionLevel.NoCompression && targetDirectory == null)
30-
throw new ArgumentException("Either compressionLevel or targetDirectory must be set");
33+
throw new ArgumentException($"Either {nameof(compressionLevel)} or {nameof(targetDirectory)} must be set");
3134

3235
this.compressionLevel = compressionLevel;
3336
this.targetDirectory = targetDirectory;
3437
}
3538

39+
public ArchiveHooks(int retainedFileCountLimit, CompressionLevel compressionLevel = CompressionLevel.Fastest, string targetDirectory = null)
40+
{
41+
if (retainedFileCountLimit <= 0)
42+
throw new ArgumentException($"{nameof(retainedFileCountLimit)} must be greater than zero", nameof(retainedFileCountLimit));
43+
if (targetDirectory is not null && TokenExpander.IsTokenised(targetDirectory))
44+
throw new ArgumentException($"{nameof(targetDirectory)} must not be tokenised when using {nameof(retainedFileCountLimit)}", nameof(targetDirectory));
45+
if (compressionLevel == CompressionLevel.NoCompression)
46+
throw new ArgumentException($"{nameof(compressionLevel)} must not be 'NoCompression' when using {nameof(retainedFileCountLimit)}", nameof(compressionLevel));
47+
48+
this.compressionLevel = compressionLevel;
49+
this.retainedFileCountLimit = retainedFileCountLimit;
50+
this.targetDirectory = null;
51+
}
52+
3653
public override void OnFileDeleting(string path)
3754
{
3855
try
@@ -70,12 +87,62 @@ public override void OnFileDeleting(string path)
7087
sourceStream.CopyTo(compressStream);
7188
}
7289
}
90+
//only apply archive file limit if we are archiving to a non tokenised path (constant path)
91+
if (this.retainedFileCountLimit > 0 && !this.IsArchivePathTokenised)
92+
{
93+
RemoveExcessFiles(currentTargetDir);
94+
}
7395
}
7496
catch (Exception ex)
7597
{
7698
SelfLog.WriteLine("Error while archiving file {0}: {1}", path, ex);
7799
throw;
78100
}
79101
}
102+
103+
private bool IsArchivePathTokenised => this.targetDirectory is not null && TokenExpander.IsTokenised(this.targetDirectory);
104+
105+
private void RemoveExcessFiles(string folder)
106+
{
107+
var searchPattern = this.compressionLevel != CompressionLevel.NoCompression
108+
? "*.gz"
109+
: "*.*";
110+
111+
var filesToDelete = Directory.GetFiles(folder, searchPattern)
112+
.Select(f => new FileInfo(f))
113+
.OrderByDescending(f => f, LogFileComparer.Default)
114+
.Skip(this.retainedFileCountLimit)
115+
.ToList();
116+
foreach (var file in filesToDelete)
117+
{
118+
try
119+
{
120+
file.Delete();
121+
}
122+
catch (Exception ex)
123+
{
124+
SelfLog.WriteLine("Error while deleting file {0}: {1}", file.FullName, ex);
125+
}
126+
}
127+
}
128+
129+
private class LogFileComparer : IComparer<FileInfo>
130+
{
131+
public static IComparer<FileInfo> Default = new LogFileComparer();
132+
133+
//This will not work correctly when the file uses a date format where lexicographical order does not correspond to chronological order but frankly, if you
134+
//are using non ISO 8601 date formats in your files you should be shot.
135+
//It would be best if the file sink could expose a way to sort files chronologically because I think LastWriteTime is probably not determisitic enough.
136+
public int Compare(FileInfo x, FileInfo y)
137+
{
138+
if (x is null && y is null)
139+
return 0;
140+
if (x is null)
141+
return -1;
142+
if (y is null)
143+
return 1;
144+
return string.Compare(x.Name, y.Name, StringComparison.OrdinalIgnoreCase);
145+
}
146+
}
80147
}
81148
}

src/Serilog.Sinks.File.Archive/TokenExpander.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Text.RegularExpressions;
44
using Serilog.Debugging;
@@ -47,6 +47,10 @@ public static string Expand(string source)
4747

4848
return source;
4949
}
50+
public static bool IsTokenised(string source)
51+
{
52+
return TryFindNextToken(source, 0, out var _);
53+
}
5054

5155
private static bool TryFindNextToken(string source, int startIdx, out Token token)
5256
{

test/Serilog.Sinks.File.Archive.Test/RollingFileSinkTests.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,42 @@ public class RollingFileSinkTests
1717
Some.LogEvent()
1818
};
1919

20+
// Test for removing old archive files in the same directory
21+
[Fact]
22+
public void Should_remove_old_archives()
23+
{
24+
var retainedFiles = 1;
25+
var archiveWrapper = new ArchiveHooks(retainedFiles);
26+
27+
using (var temp = TempFolder.ForCaller())
28+
{
29+
var path = temp.AllocateFilename("log");
30+
31+
// Write events, such that we end up with 2 deleted files and 1 retained file
32+
WriteLogEvents(path, archiveWrapper, LogEvents);
33+
34+
// Get all the files in the test directory
35+
var files = Directory.GetFiles(temp.Path)
36+
.OrderBy(p => p, StringComparer.OrdinalIgnoreCase)
37+
.ToArray();
38+
39+
// We should have a single log file, and 'retainedFiles' gz files
40+
files.Count(x => x.EndsWith("log")).ShouldBe(1);
41+
files.Count(x => x.EndsWith("gz")).ShouldBe(retainedFiles);
42+
43+
// Ensure the data was GZip compressed, by decompressing and comparing against what we wrote
44+
int i = LogEvents.Length - retainedFiles - 1;
45+
foreach (var gzipFile in files.Where(x => x.EndsWith("gz")))
46+
{
47+
var lines = Utils.DecompressLines(gzipFile);
48+
49+
lines.Count.ShouldBe(1);
50+
lines[0].ShouldEndWith(LogEvents[i].MessageTemplate.Text);
51+
i++;
52+
}
53+
}
54+
}
55+
2056
// Test for compressing log files in the same directory
2157
[Fact]
2258
public void Should_compress_deleting_log_files_in_place()
@@ -174,7 +210,7 @@ private static void WriteLogEvents(string path, ArchiveHooks hooks, LogEvent[] l
174210
foreach (var logEvent in logEvents)
175211
{
176212
log.Write(logEvent);
177-
}
213+
}
178214
}
179215
}
180216
}

test/Serilog.Sinks.File.Archive.Test/Serilog.Sinks.File.Archive.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net452;netcoreapp2.2;netcoreapp3.1</TargetFrameworks>
4+
<TargetFrameworks>net452;netcoreapp3.1;net5.0</TargetFrameworks>
55
<AssemblyName>Serilog.Sinks.File.Archive.Tests</AssemblyName>
66
<RootNamespace>Serilog.Sinks.File.Archive.Tests</RootNamespace>
77
</PropertyGroup>

0 commit comments

Comments
 (0)