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