@@ -165,7 +165,7 @@ public static LoggerConfiguration File(
165
165
/// <param name="sinkConfiguration">Logger sink configuration.</param>
166
166
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
167
167
/// text for the file. If control of regular text formatting is required, use the other
168
- /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval, bool, int?, Encoding, FileLifecycleHooks)"/>
168
+ /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval, bool, int?, Encoding, FileLifecycleHooks, TimeSpan? )"/>
169
169
/// and specify the outputTemplate parameter instead.
170
170
/// </param>
171
171
/// <param name="path">Path to the file.</param>
@@ -181,7 +181,7 @@ public static LoggerConfiguration File(
181
181
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
182
182
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
183
183
/// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
184
- /// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
184
+ /// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
185
185
/// will have a number appended in the format <code>_NNN</code>, with the first filename given no number.</param>
186
186
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
187
187
/// including the current log file. For unlimited retention, pass null. The default is 31.</param>
@@ -227,12 +227,16 @@ public static LoggerConfiguration File(
227
227
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
228
228
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
229
229
/// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
230
- /// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
230
+ /// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
231
231
/// will have a number appended in the format <code>_NNN</code>, with the first filename given no number.</param>
232
232
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
233
233
/// including the current log file. For unlimited retention, pass null. The default is 31.</param>
234
234
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param>
235
235
/// <param name="hooks">Optionally enables hooking into log file lifecycle events.</param>
236
+ /// <param name="retainedFileTimeLimit">The maximum time after the end of an interval that a rolling log file will be retained.
237
+ /// Must be greater than or equal to <see cref="TimeSpan.Zero"/>.
238
+ /// Ignored if <paramref see="rollingInterval"/> is <see cref="RollingInterval.Infinite"/>.
239
+ /// The default is to retain files indefinitely.</param>
236
240
/// <returns>Configuration object allowing method chaining.</returns>
237
241
public static LoggerConfiguration File (
238
242
this LoggerSinkConfiguration sinkConfiguration ,
@@ -249,7 +253,8 @@ public static LoggerConfiguration File(
249
253
bool rollOnFileSizeLimit = false ,
250
254
int ? retainedFileCountLimit = DefaultRetainedFileCountLimit ,
251
255
Encoding encoding = null ,
252
- FileLifecycleHooks hooks = null )
256
+ FileLifecycleHooks hooks = null ,
257
+ TimeSpan ? retainedFileTimeLimit = null )
253
258
{
254
259
if ( sinkConfiguration == null ) throw new ArgumentNullException ( nameof ( sinkConfiguration ) ) ;
255
260
if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
@@ -258,7 +263,7 @@ public static LoggerConfiguration File(
258
263
var formatter = new MessageTemplateTextFormatter ( outputTemplate , formatProvider ) ;
259
264
return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes ,
260
265
levelSwitch , buffered , shared , flushToDiskInterval ,
261
- rollingInterval , rollOnFileSizeLimit , retainedFileCountLimit , encoding , hooks ) ;
266
+ rollingInterval , rollOnFileSizeLimit , retainedFileCountLimit , encoding , hooks , retainedFileTimeLimit ) ;
262
267
}
263
268
264
269
/// <summary>
@@ -267,7 +272,7 @@ public static LoggerConfiguration File(
267
272
/// <param name="sinkConfiguration">Logger sink configuration.</param>
268
273
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
269
274
/// text for the file. If control of regular text formatting is required, use the other
270
- /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval, bool, int?, Encoding, FileLifecycleHooks)"/>
275
+ /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval, bool, int?, Encoding, FileLifecycleHooks, TimeSpan? )"/>
271
276
/// and specify the outputTemplate parameter instead.
272
277
/// </param>
273
278
/// <param name="path">Path to the file.</param>
@@ -289,6 +294,10 @@ public static LoggerConfiguration File(
289
294
/// including the current log file. For unlimited retention, pass null. The default is 31.</param>
290
295
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param>
291
296
/// <param name="hooks">Optionally enables hooking into log file lifecycle events.</param>
297
+ /// <param name="retainedFileTimeLimit">The maximum time after the end of an interval that a rolling log file will be retained.
298
+ /// Must be greater than or equal to <see cref="TimeSpan.Zero"/>.
299
+ /// Ignored if <paramref see="rollingInterval"/> is <see cref="RollingInterval.Infinite"/>.
300
+ /// The default is to retain files indefinitely.</param>
292
301
/// <returns>Configuration object allowing method chaining.</returns>
293
302
public static LoggerConfiguration File (
294
303
this LoggerSinkConfiguration sinkConfiguration ,
@@ -304,15 +313,16 @@ public static LoggerConfiguration File(
304
313
bool rollOnFileSizeLimit = false ,
305
314
int ? retainedFileCountLimit = DefaultRetainedFileCountLimit ,
306
315
Encoding encoding = null ,
307
- FileLifecycleHooks hooks = null )
316
+ FileLifecycleHooks hooks = null ,
317
+ TimeSpan ? retainedFileTimeLimit = null )
308
318
{
309
319
if ( sinkConfiguration == null ) throw new ArgumentNullException ( nameof ( sinkConfiguration ) ) ;
310
320
if ( formatter == null ) throw new ArgumentNullException ( nameof ( formatter ) ) ;
311
321
if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
312
322
313
323
return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch ,
314
324
buffered , false , shared , flushToDiskInterval , encoding , rollingInterval , rollOnFileSizeLimit ,
315
- retainedFileCountLimit , hooks ) ;
325
+ retainedFileCountLimit , hooks , retainedFileTimeLimit ) ;
316
326
}
317
327
318
328
/// <summary>
@@ -432,7 +442,7 @@ public static LoggerConfiguration File(
432
442
if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
433
443
434
444
return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , null , levelSwitch , false , true ,
435
- false , null , encoding , RollingInterval . Infinite , false , null , hooks ) ;
445
+ false , null , encoding , RollingInterval . Infinite , false , null , hooks , null ) ;
436
446
}
437
447
438
448
static LoggerConfiguration ConfigureFile (
@@ -450,21 +460,23 @@ static LoggerConfiguration ConfigureFile(
450
460
RollingInterval rollingInterval ,
451
461
bool rollOnFileSizeLimit ,
452
462
int ? retainedFileCountLimit ,
453
- FileLifecycleHooks hooks )
463
+ FileLifecycleHooks hooks ,
464
+ TimeSpan ? retainedFileTimeLimit )
454
465
{
455
466
if ( addSink == null ) throw new ArgumentNullException ( nameof ( addSink ) ) ;
456
467
if ( formatter == null ) throw new ArgumentNullException ( nameof ( formatter ) ) ;
457
468
if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
458
469
if ( fileSizeLimitBytes . HasValue && fileSizeLimitBytes < 0 ) throw new ArgumentException ( "Negative value provided; file size limit must be non-negative." , nameof ( fileSizeLimitBytes ) ) ;
459
470
if ( retainedFileCountLimit . HasValue && retainedFileCountLimit < 1 ) throw new ArgumentException ( "At least one file must be retained." , nameof ( retainedFileCountLimit ) ) ;
471
+ if ( retainedFileTimeLimit . HasValue && retainedFileTimeLimit < TimeSpan . Zero ) throw new ArgumentException ( "Negative value provided; retained file time limit must be non-negative." , nameof ( retainedFileTimeLimit ) ) ;
460
472
if ( shared && buffered ) throw new ArgumentException ( "Buffered writes are not available when file sharing is enabled." , nameof ( buffered ) ) ;
461
473
if ( shared && hooks != null ) throw new ArgumentException ( "File lifecycle hooks are not currently supported for shared log files." , nameof ( hooks ) ) ;
462
474
463
475
ILogEventSink sink ;
464
476
465
477
if ( rollOnFileSizeLimit || rollingInterval != RollingInterval . Infinite )
466
478
{
467
- sink = new RollingFileSink ( path , formatter , fileSizeLimitBytes , retainedFileCountLimit , encoding , buffered , shared , rollingInterval , rollOnFileSizeLimit , hooks ) ;
479
+ sink = new RollingFileSink ( path , formatter , fileSizeLimitBytes , retainedFileCountLimit , encoding , buffered , shared , rollingInterval , rollOnFileSizeLimit , hooks , retainedFileTimeLimit ) ;
468
480
}
469
481
else
470
482
{
0 commit comments