@@ -50,6 +50,7 @@ public static class FileLoggerConfigurationExtensions
50
50
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
51
51
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
52
52
/// will be written in full even if it exceeds the limit.</param>
53
+ /// <param name="trimFileSizeBytes">The size a log file will trimmed to after it reaches fileSizeLimitBytes</param>
53
54
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
54
55
/// is false.</param>
55
56
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
@@ -64,13 +65,14 @@ public static LoggerConfiguration File(
64
65
string outputTemplate ,
65
66
IFormatProvider formatProvider ,
66
67
long ? fileSizeLimitBytes ,
68
+ int ? trimFileSizeBytes ,
67
69
LoggingLevelSwitch levelSwitch ,
68
70
bool buffered ,
69
71
bool shared ,
70
72
TimeSpan ? flushToDiskInterval )
71
73
{
72
74
// ReSharper disable once RedundantArgumentDefaultValue
73
- return File ( sinkConfiguration , path , restrictedToMinimumLevel , outputTemplate , formatProvider , fileSizeLimitBytes ,
75
+ return File ( sinkConfiguration , path , restrictedToMinimumLevel , outputTemplate , formatProvider , fileSizeLimitBytes , trimFileSizeBytes ,
74
76
levelSwitch , buffered , shared , flushToDiskInterval , RollingInterval . Infinite , false ,
75
77
null , null ) ;
76
78
}
@@ -81,7 +83,7 @@ public static LoggerConfiguration File(
81
83
/// <param name="sinkConfiguration">Logger sink configuration.</param>
82
84
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
83
85
/// text for the file. If control of regular text formatting is required, use the other
84
- /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?)"/>
86
+ /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, int?, LoggingLevelSwitch, bool, bool, TimeSpan?)"/>
85
87
/// and specify the outputTemplate parameter instead.
86
88
/// </param>
87
89
/// <param name="path">Path to the file.</param>
@@ -92,6 +94,7 @@ public static LoggerConfiguration File(
92
94
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
93
95
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
94
96
/// will be written in full even if it exceeds the limit.</param>
97
+ /// <param name="trimFileSizeBytes">The size a log file will trimmed to after it reaches fileSizeLimitBytes</param>
95
98
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
96
99
/// is false.</param>
97
100
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
@@ -105,13 +108,14 @@ public static LoggerConfiguration File(
105
108
string path ,
106
109
LogEventLevel restrictedToMinimumLevel ,
107
110
long ? fileSizeLimitBytes ,
111
+ int ? trimFileSizeBytes ,
108
112
LoggingLevelSwitch levelSwitch ,
109
113
bool buffered ,
110
114
bool shared ,
111
115
TimeSpan ? flushToDiskInterval )
112
116
{
113
117
// ReSharper disable once RedundantArgumentDefaultValue
114
- return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch ,
118
+ return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , trimFileSizeBytes , levelSwitch ,
115
119
buffered , shared , flushToDiskInterval , RollingInterval . Infinite , false , null , null ) ;
116
120
}
117
121
@@ -130,6 +134,7 @@ public static LoggerConfiguration File(
130
134
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
131
135
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
132
136
/// will be written in full even if it exceeds the limit.</param>
137
+ /// <param name="trimFileSizeBytes">The size a log file will trimmed to after it reaches fileSizeLimitBytes</param>
133
138
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
134
139
/// is false.</param>
135
140
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
@@ -149,6 +154,7 @@ public static LoggerConfiguration File(
149
154
string outputTemplate = DefaultOutputTemplate ,
150
155
IFormatProvider formatProvider = null ,
151
156
long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
157
+ int ? trimFileSizeBytes = null ,
152
158
LoggingLevelSwitch levelSwitch = null ,
153
159
bool buffered = false ,
154
160
bool shared = false ,
@@ -163,7 +169,7 @@ public static LoggerConfiguration File(
163
169
if ( outputTemplate == null ) throw new ArgumentNullException ( nameof ( outputTemplate ) ) ;
164
170
165
171
var formatter = new MessageTemplateTextFormatter ( outputTemplate , formatProvider ) ;
166
- return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes ,
172
+ return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , trimFileSizeBytes ,
167
173
levelSwitch , buffered , shared , flushToDiskInterval ,
168
174
rollingInterval , rollOnFileSizeLimit , retainedFileCountLimit , encoding ) ;
169
175
}
@@ -174,7 +180,7 @@ public static LoggerConfiguration File(
174
180
/// <param name="sinkConfiguration">Logger sink configuration.</param>
175
181
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
176
182
/// text for the file. If control of regular text formatting is required, use the other
177
- /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval, bool, int?, Encoding)"/>
183
+ /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, int?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval, bool, int?, Encoding)"/>
178
184
/// and specify the outputTemplate parameter instead.
179
185
/// </param>
180
186
/// <param name="path">Path to the file.</param>
@@ -185,6 +191,7 @@ public static LoggerConfiguration File(
185
191
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
186
192
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
187
193
/// will be written in full even if it exceeds the limit.</param>
194
+ /// <param name="trimFileSizeBytes">The size a log file will trimmed to after it reaches fileSizeLimitBytes</param>
188
195
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
189
196
/// is false.</param>
190
197
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
@@ -203,6 +210,7 @@ public static LoggerConfiguration File(
203
210
string path ,
204
211
LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
205
212
long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
213
+ int ? trimFileSizeBytes = null ,
206
214
LoggingLevelSwitch levelSwitch = null ,
207
215
bool buffered = false ,
208
216
bool shared = false ,
@@ -212,7 +220,7 @@ public static LoggerConfiguration File(
212
220
int ? retainedFileCountLimit = DefaultRetainedFileCountLimit ,
213
221
Encoding encoding = null )
214
222
{
215
- return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch ,
223
+ return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , trimFileSizeBytes , levelSwitch ,
216
224
buffered , false , shared , flushToDiskInterval , encoding , rollingInterval , rollOnFileSizeLimit , retainedFileCountLimit ) ;
217
225
}
218
226
@@ -269,7 +277,7 @@ public static LoggerConfiguration File(
269
277
LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
270
278
LoggingLevelSwitch levelSwitch = null )
271
279
{
272
- return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , null , levelSwitch , false , true ,
280
+ return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , null , null , levelSwitch , false , true ,
273
281
false , null , null , RollingInterval . Infinite , false , null ) ;
274
282
}
275
283
@@ -279,6 +287,7 @@ static LoggerConfiguration ConfigureFile(
279
287
string path ,
280
288
LogEventLevel restrictedToMinimumLevel ,
281
289
long ? fileSizeLimitBytes ,
290
+ int ? trimFileSizeBytes ,
282
291
LoggingLevelSwitch levelSwitch ,
283
292
bool buffered ,
284
293
bool propagateExceptions ,
@@ -293,6 +302,7 @@ static LoggerConfiguration ConfigureFile(
293
302
if ( formatter == null ) throw new ArgumentNullException ( nameof ( formatter ) ) ;
294
303
if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
295
304
if ( fileSizeLimitBytes . HasValue && fileSizeLimitBytes < 0 ) throw new ArgumentException ( "Negative value provided; file size limit must be non-negative." , nameof ( fileSizeLimitBytes ) ) ;
305
+ if ( trimFileSizeBytes . HasValue && trimFileSizeBytes < 0 ) throw new ArgumentException ( "Negative value provided; trim file size must be non-negative." , nameof ( trimFileSizeBytes ) ) ;
296
306
if ( retainedFileCountLimit . HasValue && retainedFileCountLimit < 1 ) throw new ArgumentException ( "At least one file must be retained." , nameof ( retainedFileCountLimit ) ) ;
297
307
if ( shared && buffered ) throw new ArgumentException ( "Buffered writes are not available when file sharing is enabled." , nameof ( buffered ) ) ;
298
308
@@ -313,7 +323,7 @@ static LoggerConfiguration ConfigureFile(
313
323
}
314
324
else
315
325
{
316
- sink = new FileSink ( path , formatter , fileSizeLimitBytes , buffered : buffered ) ;
326
+ sink = new FileSink ( path , formatter , fileSizeLimitBytes , trimFileSizeBytes , buffered : buffered ) ;
317
327
}
318
328
#pragma warning restore 618
319
329
}
0 commit comments