-
Notifications
You must be signed in to change notification settings - Fork 3
/
DeleteOlderLogRetentionPolicy.cs
47 lines (26 loc) · 1.21 KB
/
DeleteOlderLogRetentionPolicy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
namespace Gsemac.IO.Logging {
public class DeleteOlderLogRetentionPolicy :
ILogRetentionPolicy {
// Public members
public DeleteOlderLogRetentionPolicy(TimeSpan deleteOlderThan) {
this.deleteOlderThan = deleteOlderThan;
}
public void ExecutePolicy(string directoryPath, string searchPattern) {
if (string.IsNullOrEmpty(searchPattern))
return;
TimeSpan timeSinceLastExecution = DateTimeOffset.Now - lastExecutionTime;
if (timeSinceLastExecution > deleteOlderThan && System.IO.Directory.Exists(directoryPath)) {
lastExecutionTime = DateTimeOffset.Now;
foreach (string filePath in System.IO.Directory.GetFiles(directoryPath, searchPattern)) {
DateTime creationTime = System.IO.File.GetCreationTime(filePath);
if (DateTime.Now - creationTime > deleteOlderThan)
System.IO.File.Delete(filePath);
}
}
}
// Private members
private readonly TimeSpan deleteOlderThan;
private DateTimeOffset lastExecutionTime = DateTimeOffset.MinValue;
}
}