This repository was archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileLogComparer.cs
72 lines (63 loc) · 2.47 KB
/
FileLogComparer.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
namespace IisLogRotator
{
public class FileLogComparer : IComparer<FileLogInfo>
{
public enum ComparisonMethod
{
ByNameOrdinal,
ByNameOrdinalIgnoringUtf8Prefix,
ByCreationTime,
ByIisLogDate
}
public FileLogComparer(Folder folder)
{
this.Folder = folder;
// if rotation is size-based, we order by creation date
// other naming syntaxes are sortable patterns
if (folder.Period == IisPeriodType.MaxSize)
{
this.Method = FileLogComparer.ComparisonMethod.ByCreationTime;
}
else
{
this.Method = FileLogComparer.ComparisonMethod.ByIisLogDate;
}
}
public Folder Folder { get; private set; }
public ComparisonMethod Method { get; private set; }
public int Compare(FileLogInfo x, FileLogInfo y)
{
switch (this.Method)
{
case ComparisonMethod.ByNameOrdinal:
return StringComparer.Ordinal.Compare(
x.File.Name,
y.File.Name
);
case ComparisonMethod.ByNameOrdinalIgnoringUtf8Prefix:
// TODO convert YY to YYYY to compare centuries
// ex.: ex990101.log must be before ex000101.log
// parse first two digits to integer then pass to Calendar.ToFourDigitYear(...)
return StringComparer.Ordinal.Compare(
x.File.Name.StripeUtf8Prefix(),
y.File.Name.StripeUtf8Prefix()
);
case ComparisonMethod.ByCreationTime:
return DateTime.Compare(x.File.CreationTime, y.File.CreationTime);
case ComparisonMethod.ByIisLogDate:
if (x.IsChild && y.IsChild)
return DateTime.Compare(x.Date, y.Date);
else if (x.IsChild)
return 1;
else if (y.IsChild)
return -1;
else
return 0;
default:
throw new NotImplementedException("This comparison method is not yet implemented");
}
}
}
}