@@ -24,6 +24,8 @@ public class FileLoggerProcessorTests
2424 private string _messageTwo = "Message two" ;
2525 private string _messageThree = "Message three" ;
2626
27+ private DateTime _today = new DateTime ( 2021 , 01 , 01 , 12 , 00 , 00 ) ;
28+
2729 public FileLoggerProcessorTests ( )
2830 {
2931 TempPath = Path . GetTempFileName ( ) + "_" ;
@@ -33,11 +35,10 @@ public FileLoggerProcessorTests()
3335
3436 [ Fact ]
3537 public async Task WritesToTextFile ( )
36- {
37- var today = new DateTime ( 2021 , 01 , 01 , 12 , 00 , 00 ) ;
38+ {
3839 var mockSystemDateTime = new MockSystemDateTime
3940 {
40- Now = today
41+ Now = _today
4142 } ;
4243 var path = Path . Combine ( TempPath , Path . GetRandomFileName ( ) ) ;
4344
@@ -52,7 +53,7 @@ public async Task WritesToTextFile()
5253 {
5354 logger . SystemDateTime = mockSystemDateTime ;
5455 logger . EnqueueMessage ( _messageOne ) ;
55- fileName = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0000.txt") ) ;
56+ fileName = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0000.txt") ) ;
5657 // Pause for a bit before disposing so logger can finish logging
5758 await WaitForFile ( fileName , _messageOne . Length ) . DefaultTimeout ( ) ;
5859 }
@@ -66,20 +67,67 @@ public async Task WritesToTextFile()
6667 }
6768 }
6869
70+ [ Fact ]
71+ public async Task RollsTextFilesBasedOnDate ( )
72+ {
73+ var mockSystemDateTime = new MockSystemDateTime
74+ {
75+ Now = _today
76+ } ;
77+ var tomorrow = _today . AddDays ( 1 ) ;
78+
79+ var path = Path . Combine ( TempPath , Path . GetRandomFileName ( ) ) ;
80+ var options = new W3CLoggerOptions ( )
81+ {
82+ LogDirectory = path
83+ } ;
84+
85+ try
86+ {
87+ string fileNameToday ;
88+ string fileNameTomorrow ;
89+
90+ await using ( var logger = new FileLoggerProcessor ( new OptionsWrapperMonitor < W3CLoggerOptions > ( options ) , new HostingEnvironment ( ) , NullLoggerFactory . Instance ) )
91+ {
92+ logger . SystemDateTime = mockSystemDateTime ;
93+ logger . EnqueueMessage ( _messageOne ) ;
94+
95+ fileNameToday = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0000.txt") ) ;
96+
97+ await WaitForFile ( fileNameToday , _messageOne . Length ) . DefaultTimeout ( ) ;
98+
99+ mockSystemDateTime . Now = tomorrow ;
100+ logger . EnqueueMessage ( _messageTwo ) ;
101+
102+ fileNameTomorrow = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { tomorrow . Year : 0000} { tomorrow . Month : 00} { tomorrow . Day : 00} .0000.txt") ) ;
103+
104+ await WaitForFile ( fileNameTomorrow , _messageTwo . Length ) . DefaultTimeout ( ) ;
105+ }
106+
107+ Assert . True ( File . Exists ( fileNameToday ) ) ;
108+ Assert . Equal ( _messageOne + Environment . NewLine , File . ReadAllText ( fileNameToday ) ) ;
109+ Assert . True ( File . Exists ( fileNameTomorrow ) ) ;
110+ Assert . Equal ( _messageTwo + Environment . NewLine , File . ReadAllText ( fileNameTomorrow ) ) ;
111+ }
112+ finally
113+ {
114+ Helpers . DisposeDirectory ( path ) ;
115+ }
116+ }
117+
69118 [ QuarantinedTest ( "https://github.com/dotnet/aspnetcore/issues/34284" ) ]
70119 [ Fact ]
71- public async Task RollsTextFiles ( )
120+ public async Task RollsTextFilesBasedOnSize ( )
72121 {
73122 var path = Path . Combine ( TempPath , Path . GetRandomFileName ( ) ) ;
74123
75124 try
76125 {
77126 string fileName1 ;
78- string fileName2 ;
79- var today = new DateTime ( 2021 , 01 , 01 , 12 , 00 , 00 ) ;
127+ string fileName2 ;
80128 var mockSystemDateTime = new MockSystemDateTime
81129 {
82- Now = today
130+ Now = _today
83131 } ;
84132 var options = new W3CLoggerOptions ( )
85133 {
@@ -91,8 +139,8 @@ public async Task RollsTextFiles()
91139 logger . SystemDateTime = mockSystemDateTime ;
92140 logger . EnqueueMessage ( _messageOne ) ;
93141 logger . EnqueueMessage ( _messageTwo ) ;
94- fileName1 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0000.txt") ) ;
95- fileName2 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0001.txt") ) ;
142+ fileName1 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0000.txt") ) ;
143+ fileName2 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0001.txt") ) ;
96144 // Pause for a bit before disposing so logger can finish logging
97145 await WaitForFile ( fileName2 , _messageTwo . Length ) . DefaultTimeout ( ) ;
98146 }
@@ -113,11 +161,10 @@ public async Task RespectsMaxFileCount()
113161 {
114162 var path = Path . Combine ( TempPath , Path . GetRandomFileName ( ) ) ;
115163 Directory . CreateDirectory ( path ) ;
116- File . WriteAllText ( Path . Combine ( path , "randomFile.txt" ) , "Text" ) ;
117- var today = new DateTime ( 2021 , 01 , 01 , 12 , 00 , 00 ) ;
164+ File . WriteAllText ( Path . Combine ( path , "randomFile.txt" ) , "Text" ) ;
118165 var mockSystemDateTime = new MockSystemDateTime
119166 {
120- Now = today
167+ Now = _today
121168 } ;
122169
123170 try
@@ -136,12 +183,12 @@ public async Task RespectsMaxFileCount()
136183 {
137184 logger . EnqueueMessage ( _messageOne ) ;
138185 }
139- lastFileName = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0009.txt") ) ;
186+ lastFileName = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0009.txt") ) ;
140187 // Pause for a bit before disposing so logger can finish logging
141188 await WaitForFile ( lastFileName , _messageOne . Length ) . DefaultTimeout ( ) ;
142189 for ( int i = 0 ; i < 6 ; i ++ )
143190 {
144- await WaitForRoll ( Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .{ i : 0000} .txt") ) ) . DefaultTimeout ( ) ;
191+ await WaitForRoll ( Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .{ i : 0000} .txt") ) ) . DefaultTimeout ( ) ;
145192 }
146193 }
147194
@@ -155,7 +202,7 @@ public async Task RespectsMaxFileCount()
155202 Assert . Equal ( "randomFile.txt" , actualFiles [ 0 ] ) ;
156203 for ( int i = 1 ; i < 4 ; i ++ )
157204 {
158- Assert . True ( ( actualFiles [ i ] . StartsWith ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} ", StringComparison . InvariantCulture ) ) ) ;
205+ Assert . True ( ( actualFiles [ i ] . StartsWith ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} ", StringComparison . InvariantCulture ) ) ) ;
159206 }
160207 }
161208 finally
@@ -166,11 +213,10 @@ public async Task RespectsMaxFileCount()
166213
167214 [ Fact ]
168215 public async Task InstancesWriteToSameDirectory ( )
169- {
170- var today = new DateTime ( 2021 , 01 , 01 , 12 , 00 , 00 ) ;
216+ {
171217 var mockSystemDateTime = new MockSystemDateTime
172218 {
173- Now = today
219+ Now = _today
174220 } ;
175221
176222 var path = Path . Combine ( TempPath , Path . GetRandomFileName ( ) ) ;
@@ -191,7 +237,7 @@ public async Task InstancesWriteToSameDirectory()
191237 {
192238 logger . EnqueueMessage ( _messageOne ) ;
193239 }
194- var filePath = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0002.txt") ) ;
240+ var filePath = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0002.txt") ) ;
195241 // Pause for a bit before disposing so logger can finish logging
196242 await WaitForFile ( filePath , _messageOne . Length ) . DefaultTimeout ( ) ;
197243 }
@@ -204,7 +250,7 @@ public async Task InstancesWriteToSameDirectory()
204250 {
205251 logger . EnqueueMessage ( _messageOne ) ;
206252 }
207- var filePath = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0005.txt") ) ;
253+ var filePath = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0005.txt") ) ;
208254 // Pause for a bit before disposing so logger can finish logging
209255 await WaitForFile ( filePath , _messageOne . Length ) . DefaultTimeout ( ) ;
210256 }
@@ -218,7 +264,7 @@ public async Task InstancesWriteToSameDirectory()
218264 Assert . Equal ( 6 , actualFiles1 . Length ) ;
219265 for ( int i = 0 ; i < 6 ; i ++ )
220266 {
221- Assert . Contains ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .{ i : 0000} .txt", actualFiles1 [ i ] ) ;
267+ Assert . Contains ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .{ i : 0000} .txt", actualFiles1 [ i ] ) ;
222268 }
223269
224270 // Third instance should roll to 5 most recent files
@@ -228,9 +274,9 @@ public async Task InstancesWriteToSameDirectory()
228274 logger . SystemDateTime = mockSystemDateTime ;
229275 logger . EnqueueMessage ( _messageOne ) ;
230276 // Pause for a bit before disposing so logger can finish logging
231- await WaitForFile ( Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0006.txt") ) , _messageOne . Length ) . DefaultTimeout ( ) ;
232- await WaitForRoll ( Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0000.txt") ) ) . DefaultTimeout ( ) ;
233- await WaitForRoll ( Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0001.txt") ) ) . DefaultTimeout ( ) ;
277+ await WaitForFile ( Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0006.txt") ) , _messageOne . Length ) . DefaultTimeout ( ) ;
278+ await WaitForRoll ( Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0000.txt") ) ) . DefaultTimeout ( ) ;
279+ await WaitForRoll ( Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0001.txt") ) ) . DefaultTimeout ( ) ;
234280 }
235281
236282 var actualFiles2 = new DirectoryInfo ( path )
@@ -242,7 +288,7 @@ public async Task InstancesWriteToSameDirectory()
242288 Assert . Equal ( 5 , actualFiles2 . Length ) ;
243289 for ( int i = 0 ; i < 5 ; i ++ )
244290 {
245- Assert . Equal ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .{ i + 2 : 0000} .txt", actualFiles2 [ i ] ) ;
291+ Assert . Equal ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .{ i + 2 : 0000} .txt", actualFiles2 [ i ] ) ;
246292 }
247293 }
248294 finally
@@ -254,10 +300,9 @@ public async Task InstancesWriteToSameDirectory()
254300 [ Fact ]
255301 public async Task WritesToNewFileOnNewInstance ( )
256302 {
257- var today = new DateTime ( 2021 , 01 , 01 , 12 , 00 , 00 ) ;
258303 var mockSystemDateTime = new MockSystemDateTime
259304 {
260- Now = today
305+ Now = _today
261306 } ;
262307
263308 var path = Path . Combine ( TempPath , Path . GetRandomFileName ( ) ) ;
@@ -270,9 +315,9 @@ public async Task WritesToNewFileOnNewInstance()
270315 LogDirectory = path ,
271316 FileSizeLimit = 5
272317 } ;
273- var fileName1 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0000.txt") ) ;
274- var fileName2 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0001.txt") ) ;
275- var fileName3 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0002.txt") ) ;
318+ var fileName1 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0000.txt") ) ;
319+ var fileName2 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0001.txt") ) ;
320+ var fileName3 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0002.txt") ) ;
276321
277322 await using ( var logger = new FileLoggerProcessor ( new OptionsWrapperMonitor < W3CLoggerOptions > ( options ) , new HostingEnvironment ( ) , NullLoggerFactory . Instance ) )
278323 {
@@ -319,10 +364,9 @@ public async Task WritesToNewFileOnNewInstance()
319364 [ Fact ]
320365 public async Task WritesToNewFileOnOptionsChange ( )
321366 {
322- var today = new DateTime ( 2021 , 01 , 01 , 12 , 00 , 00 ) ;
323367 var mockSystemDateTime = new MockSystemDateTime
324368 {
325- Now = today
369+ Now = _today
326370 } ;
327371
328372 var path = Path . Combine ( TempPath , Path . GetRandomFileName ( ) ) ;
@@ -336,8 +380,8 @@ public async Task WritesToNewFileOnOptionsChange()
336380 LoggingFields = W3CLoggingFields . Time ,
337381 FileSizeLimit = 10000
338382 } ;
339- var fileName1 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0000.txt") ) ;
340- var fileName2 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { today . Year : 0000} { today . Month : 00} { today . Day : 00} .0001.txt") ) ;
383+ var fileName1 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0000.txt") ) ;
384+ var fileName2 = Path . Combine ( path , FormattableString . Invariant ( $ "{ options . FileName } { _today . Year : 0000} { _today . Month : 00} { _today . Day : 00} .0001.txt") ) ;
341385 var monitor = new OptionsWrapperMonitor < W3CLoggerOptions > ( options ) ;
342386
343387 await using ( var logger = new FileLoggerProcessor ( monitor , new HostingEnvironment ( ) , NullLoggerFactory . Instance ) )
0 commit comments