11
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
-
14
+
15
15
using System ;
16
16
using System . Collections . Generic ;
17
17
using System . Globalization ;
@@ -25,52 +25,50 @@ namespace Serilog.Sinks.RollingFile
25
25
// Logs/log-{Date}.txt
26
26
//
27
27
class TemplatedPathRoller
28
- {
29
- const string OldStyleDateSpecifier = "{0}" ;
30
- const string DateSpecifier = "{Date}" ;
31
- const string DateFormat = "yyyyMMdd" ;
28
+ {
32
29
const string DefaultSeparator = "-" ;
33
30
31
+ const string SpecifierMatchGroup = "specifier" ;
32
+ const string SequenceNumberMatchGroup = "sequence" ;
33
+
34
34
readonly string _pathTemplate ;
35
35
readonly Regex _filenameMatcher ;
36
+ readonly Specifier _specifier = null ;
36
37
37
38
public TemplatedPathRoller ( string pathTemplate )
38
39
{
39
40
if ( pathTemplate == null ) throw new ArgumentNullException ( nameof ( pathTemplate ) ) ;
40
- if ( pathTemplate . Contains ( OldStyleDateSpecifier ) )
41
- throw new ArgumentException ( "The old-style date specifier " + OldStyleDateSpecifier +
42
- " is no longer supported, instead please use " + DateSpecifier ) ;
43
41
44
42
var directory = Path . GetDirectoryName ( pathTemplate ) ;
45
43
if ( string . IsNullOrEmpty ( directory ) )
46
- {
47
44
directory = Directory . GetCurrentDirectory ( ) ;
48
- }
49
45
50
- directory = Path . GetFullPath ( directory ) ;
46
+ Specifier directorySpecifier ;
47
+ if ( Specifier . TryGetSpecifier ( directory , out directorySpecifier ) )
48
+ throw new ArgumentException ( $ "The { directorySpecifier . Token } specifier cannot form part of the directory name.") ;
51
49
52
- if ( directory . Contains ( DateSpecifier ) )
53
- throw new ArgumentException ( "The date cannot form part of the directory name" ) ;
50
+ directory = Path . GetFullPath ( directory ) ;
54
51
55
52
var filenameTemplate = Path . GetFileName ( pathTemplate ) ;
56
- if ( ! filenameTemplate . Contains ( DateSpecifier ) )
53
+ if ( ! Specifier . TryGetSpecifier ( filenameTemplate , out _specifier ) )
57
54
{
55
+ _specifier = Specifier . Date ;
58
56
filenameTemplate = Path . GetFileNameWithoutExtension ( filenameTemplate ) + DefaultSeparator +
59
- DateSpecifier + Path . GetExtension ( filenameTemplate ) ;
57
+ _specifier . Token + Path . GetExtension ( filenameTemplate ) ;
60
58
}
61
59
62
- var indexOfSpecifier = filenameTemplate . IndexOf ( DateSpecifier , StringComparison . Ordinal ) ;
60
+ var indexOfSpecifier = filenameTemplate . IndexOf ( _specifier . Token , StringComparison . Ordinal ) ;
63
61
var prefix = filenameTemplate . Substring ( 0 , indexOfSpecifier ) ;
64
- var suffix = filenameTemplate . Substring ( indexOfSpecifier + DateSpecifier . Length ) ;
62
+ var suffix = filenameTemplate . Substring ( indexOfSpecifier + _specifier . Token . Length ) ;
65
63
_filenameMatcher = new Regex (
66
64
"^" +
67
65
Regex . Escape ( prefix ) +
68
- "(?<date >\\ d{" + DateFormat . Length + "})" +
69
- "(?<inc >_[0-9]{3,}){0,1}" +
66
+ "(?<" + SpecifierMatchGroup + " >\\ d{" + _specifier . Format . Length + "})" +
67
+ "(?<" + SequenceNumberMatchGroup + " >_[0-9]{3,}){0,1}" +
70
68
Regex . Escape ( suffix ) +
71
69
"$" ) ;
72
70
73
- DirectorySearchPattern = filenameTemplate . Replace ( DateSpecifier , "*" ) ;
71
+ DirectorySearchPattern = filenameTemplate . Replace ( _specifier . Token , "*" ) ;
74
72
LogFileDirectory = directory ;
75
73
_pathTemplate = Path . Combine ( LogFileDirectory , filenameTemplate ) ;
76
74
}
@@ -81,12 +79,14 @@ public TemplatedPathRoller(string pathTemplate)
81
79
82
80
public void GetLogFilePath ( DateTime date , int sequenceNumber , out string path )
83
81
{
84
- var tok = date . ToString ( DateFormat , CultureInfo . InvariantCulture ) ;
82
+ var currentCheckpoint = GetCurrentCheckpoint ( date ) ;
83
+
84
+ var tok = currentCheckpoint . ToString ( _specifier . Format , CultureInfo . InvariantCulture ) ;
85
85
86
86
if ( sequenceNumber != 0 )
87
87
tok += "_" + sequenceNumber . ToString ( "000" , CultureInfo . InvariantCulture ) ;
88
88
89
- path = _pathTemplate . Replace ( DateSpecifier , tok ) ;
89
+ path = _pathTemplate . Replace ( _specifier . Token , tok ) ;
90
90
}
91
91
92
92
public IEnumerable < RollingLogFile > SelectMatches ( IEnumerable < string > filenames )
@@ -97,26 +97,30 @@ public IEnumerable<RollingLogFile> SelectMatches(IEnumerable<string> filenames)
97
97
if ( match . Success )
98
98
{
99
99
var inc = 0 ;
100
- var incGroup = match . Groups [ "inc" ] ;
100
+ var incGroup = match . Groups [ SequenceNumberMatchGroup ] ;
101
101
if ( incGroup . Captures . Count != 0 )
102
102
{
103
103
var incPart = incGroup . Captures [ 0 ] . Value . Substring ( 1 ) ;
104
104
inc = int . Parse ( incPart , CultureInfo . InvariantCulture ) ;
105
105
}
106
106
107
- DateTime date ;
108
- var datePart = match . Groups [ "date" ] . Captures [ 0 ] . Value ;
107
+ DateTime dateTime ;
108
+ var dateTimePart = match . Groups [ SpecifierMatchGroup ] . Captures [ 0 ] . Value ;
109
109
if ( ! DateTime . TryParseExact (
110
- datePart ,
111
- DateFormat ,
110
+ dateTimePart ,
111
+ _specifier . Format ,
112
112
CultureInfo . InvariantCulture ,
113
113
DateTimeStyles . None ,
114
- out date ) )
114
+ out dateTime ) )
115
115
continue ;
116
116
117
- yield return new RollingLogFile ( filename , date , inc ) ;
117
+ yield return new RollingLogFile ( filename , dateTime , inc ) ;
118
118
}
119
119
}
120
120
}
121
+
122
+ public DateTime GetCurrentCheckpoint ( DateTime instant ) => _specifier . GetCurrentCheckpoint ( instant ) ;
123
+
124
+ public DateTime GetNextCheckpoint ( DateTime instant ) => _specifier . GetNextCheckpoint ( instant ) ;
121
125
}
122
- }
126
+ }
0 commit comments