@@ -145,6 +145,116 @@ public void WhenRetentionCountAndArchivingHookIsSetOldFilesAreCopiedAndOriginalD
145
145
} ) ;
146
146
}
147
147
148
+ [ Fact ]
149
+ public void WhenFirstOpeningFailedWithLockRetryDelayedUntilNextCheckpoint ( )
150
+ {
151
+ var fileName = Some . String ( ) + ".txt" ;
152
+ using var temp = new TempFolder ( ) ;
153
+ using var log = new LoggerConfiguration ( )
154
+ . WriteTo . File ( Path . Combine ( temp . Path , fileName ) , rollOnFileSizeLimit : true , fileSizeLimitBytes : 1 , rollingInterval : RollingInterval . Minute , hooks : new FailOpeningHook ( true , 2 , 3 , 4 ) )
155
+ . CreateLogger ( ) ;
156
+ LogEvent e1 = Some . InformationEvent ( new DateTime ( 2012 , 10 , 28 ) ) ,
157
+ e2 = Some . InformationEvent ( e1 . Timestamp . AddMilliseconds ( 1 ) ) ,
158
+ e3 = Some . InformationEvent ( e2 . Timestamp . AddMinutes ( 29 ) ) ,
159
+ e4 = Some . InformationEvent ( e3 . Timestamp . AddMinutes ( 1 ) ) ;
160
+ LogEvent [ ] logEvents = new [ ] { e1 , e2 , e3 , e4 } ;
161
+
162
+ foreach ( var logEvent in logEvents )
163
+ {
164
+ Clock . SetTestDateTimeNow ( logEvent . Timestamp . DateTime ) ;
165
+ log . Write ( logEvent ) ;
166
+ }
167
+
168
+ var files = Directory . GetFiles ( temp . Path )
169
+ . OrderBy ( p => p , StringComparer . OrdinalIgnoreCase )
170
+ . ToArray ( ) ;
171
+ var pattern = "yyyyMMddHHmm" ;
172
+
173
+ Assert . Equal ( 6 , files . Length ) ;
174
+ // Successful write of e1:
175
+ Assert . True ( files [ 0 ] . EndsWith ( ExpectedFileName ( fileName , e1 . Timestamp , pattern ) ) , files [ 0 ] ) ;
176
+ // Failing writes for e2, will be dropped and logged to SelfLog:
177
+ Assert . True ( files [ 1 ] . EndsWith ( "_001.txt" ) , files [ 1 ] ) ;
178
+ Assert . True ( files [ 2 ] . EndsWith ( "_002.txt" ) , files [ 2 ] ) ;
179
+ Assert . True ( files [ 3 ] . EndsWith ( "_003.txt" ) , files [ 3 ] ) ;
180
+ // Successful write of e3:
181
+ Assert . True ( files [ 4 ] . EndsWith ( ExpectedFileName ( fileName , e3 . Timestamp , pattern ) ) , files [ 4 ] ) ;
182
+ // Successful write of e4:
183
+ Assert . True ( files [ 5 ] . EndsWith ( ExpectedFileName ( fileName , e4 . Timestamp , pattern ) ) , files [ 5 ] ) ;
184
+ }
185
+
186
+ [ Fact ]
187
+ public void WhenFirstOpeningFailedWithLockRetryDelayed30Minutes ( )
188
+ {
189
+ var fileName = Some . String ( ) + ".txt" ;
190
+ using var temp = new TempFolder ( ) ;
191
+ using var log = new LoggerConfiguration ( )
192
+ . WriteTo . File ( Path . Combine ( temp . Path , fileName ) , rollOnFileSizeLimit : true , fileSizeLimitBytes : 1 , rollingInterval : RollingInterval . Hour , hooks : new FailOpeningHook ( true , 2 , 3 , 4 ) )
193
+ . CreateLogger ( ) ;
194
+ LogEvent e1 = Some . InformationEvent ( new DateTime ( 2012 , 10 , 28 ) ) ,
195
+ e2 = Some . InformationEvent ( e1 . Timestamp . AddMilliseconds ( 1 ) ) ,
196
+ e3 = Some . InformationEvent ( e2 . Timestamp . AddMinutes ( 29 ) ) ,
197
+ e4 = Some . InformationEvent ( e3 . Timestamp . AddMinutes ( 1 ) ) ;
198
+ LogEvent [ ] logEvents = new [ ] { e1 , e2 , e3 , e4 } ;
199
+
200
+ foreach ( var logEvent in logEvents )
201
+ {
202
+ Clock . SetTestDateTimeNow ( logEvent . Timestamp . DateTime ) ;
203
+ log . Write ( logEvent ) ;
204
+ }
205
+
206
+ var files = Directory . GetFiles ( temp . Path )
207
+ . OrderBy ( p => p , StringComparer . OrdinalIgnoreCase )
208
+ . ToArray ( ) ;
209
+ var pattern = "yyyyMMddHH" ;
210
+
211
+ Assert . Equal ( 5 , files . Length ) ;
212
+ // Successful write of e1:
213
+ Assert . True ( files [ 0 ] . EndsWith ( ExpectedFileName ( fileName , e1 . Timestamp , pattern ) ) , files [ 0 ] ) ;
214
+ // Failing writes for e2, will be dropped and logged to SelfLog; on lock it will try it three times:
215
+ Assert . True ( files [ 1 ] . EndsWith ( "_001.txt" ) , files [ 1 ] ) ;
216
+ Assert . True ( files [ 2 ] . EndsWith ( "_002.txt" ) , files [ 2 ] ) ;
217
+ Assert . True ( files [ 3 ] . EndsWith ( "_003.txt" ) , files [ 3 ] ) ;
218
+ /* e3 will be dropped and logged to SelfLog without new file as it's in the 30 minutes cooldown and roller only starts on next hour! */
219
+ // Successful write of e4:
220
+ Assert . True ( files [ 4 ] . EndsWith ( "_004.txt" ) , files [ 4 ] ) ;
221
+ }
222
+
223
+ [ Fact ]
224
+ public void WhenFirstOpeningFailedWithoutLockRetryDelayed30Minutes ( )
225
+ {
226
+ var fileName = Some . String ( ) + ".txt" ;
227
+ using var temp = new TempFolder ( ) ;
228
+ using var log = new LoggerConfiguration ( )
229
+ . WriteTo . File ( Path . Combine ( temp . Path , fileName ) , rollOnFileSizeLimit : true , fileSizeLimitBytes : 1 , rollingInterval : RollingInterval . Hour , hooks : new FailOpeningHook ( false , 2 ) )
230
+ . CreateLogger ( ) ;
231
+ LogEvent e1 = Some . InformationEvent ( new DateTime ( 2012 , 10 , 28 ) ) ,
232
+ e2 = Some . InformationEvent ( e1 . Timestamp . AddMilliseconds ( 1 ) ) ,
233
+ e3 = Some . InformationEvent ( e2 . Timestamp . AddMinutes ( 29 ) ) ,
234
+ e4 = Some . InformationEvent ( e3 . Timestamp . AddMinutes ( 1 ) ) ;
235
+ LogEvent [ ] logEvents = new [ ] { e1 , e2 , e3 , e4 } ;
236
+
237
+ foreach ( var logEvent in logEvents )
238
+ {
239
+ Clock . SetTestDateTimeNow ( logEvent . Timestamp . DateTime ) ;
240
+ log . Write ( logEvent ) ;
241
+ }
242
+
243
+ var files = Directory . GetFiles ( temp . Path )
244
+ . OrderBy ( p => p , StringComparer . OrdinalIgnoreCase )
245
+ . ToArray ( ) ;
246
+ var pattern = "yyyyMMddHH" ;
247
+
248
+ Assert . Equal ( 3 , files . Length ) ;
249
+ // Successful write of e1:
250
+ Assert . True ( files [ 0 ] . EndsWith ( ExpectedFileName ( fileName , e1 . Timestamp , pattern ) ) , files [ 0 ] ) ;
251
+ // Failing writes for e2, will be dropped and logged to SelfLog; on non-lock it will try it once:
252
+ Assert . True ( files [ 1 ] . EndsWith ( "_001.txt" ) , files [ 1 ] ) ;
253
+ /* e3 will be dropped and logged to SelfLog without new file as it's in the 30 minutes cooldown and roller only starts on next hour! */
254
+ // Successful write of e4:
255
+ Assert . True ( files [ 2 ] . EndsWith ( "_002.txt" ) , files [ 2 ] ) ;
256
+ }
257
+
148
258
[ Fact ]
149
259
public void WhenSizeLimitIsBreachedNewFilesCreated ( )
150
260
{
@@ -279,7 +389,7 @@ static void TestRollingEventSequence(
279
389
Clock . SetTestDateTimeNow ( @event . Timestamp . DateTime ) ;
280
390
log . Write ( @event ) ;
281
391
282
- var expected = pathFormat . Replace ( ".txt" , @event . Timestamp . ToString ( "yyyyMMdd" ) + ".txt ") ;
392
+ var expected = ExpectedFileName ( pathFormat , @event . Timestamp , "yyyyMMdd ") ;
283
393
Assert . True ( System . IO . File . Exists ( expected ) ) ;
284
394
285
395
verified . Add ( expected ) ;
@@ -292,4 +402,9 @@ static void TestRollingEventSequence(
292
402
Directory . Delete ( folder , true ) ;
293
403
}
294
404
}
405
+
406
+ static string ExpectedFileName ( string fileName , DateTimeOffset timestamp , string pattern )
407
+ {
408
+ return fileName . Replace ( ".txt" , timestamp . ToString ( pattern ) + ".txt" ) ;
409
+ }
295
410
}
0 commit comments