Skip to content

Commit 55fcb2b

Browse files
committed
Add backwards-compatible configuration overloads
1 parent 0ae234e commit 55fcb2b

File tree

1 file changed

+131
-45
lines changed

1 file changed

+131
-45
lines changed

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

Lines changed: 131 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
using Serilog.Formatting.Json;
2525
using Serilog.Sinks.File;
2626

27-
// ReSharper disable MethodOverloadWithOptionalParameter
27+
// ReSharper disable RedundantArgumentDefaultValue, MethodOverloadWithOptionalParameter
2828

2929
namespace Serilog
3030
{
@@ -69,10 +69,8 @@ public static LoggerConfiguration File(
6969
bool shared,
7070
TimeSpan? flushToDiskInterval)
7171
{
72-
// ReSharper disable once RedundantArgumentDefaultValue
7372
return File(sinkConfiguration, path, restrictedToMinimumLevel, outputTemplate, formatProvider, fileSizeLimitBytes,
74-
levelSwitch, buffered, shared, flushToDiskInterval, RollingInterval.Infinite, false,
75-
null, null);
73+
levelSwitch, buffered, shared, flushToDiskInterval, RollingInterval.Infinite, false, null, null, null);
7674
}
7775

7876
/// <summary>
@@ -110,9 +108,103 @@ public static LoggerConfiguration File(
110108
bool shared,
111109
TimeSpan? flushToDiskInterval)
112110
{
113-
// ReSharper disable once RedundantArgumentDefaultValue
114111
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch,
115-
buffered, shared, flushToDiskInterval, RollingInterval.Infinite, false, null, null);
112+
buffered, shared, flushToDiskInterval, RollingInterval.Infinite, false, null, null, null);
113+
}
114+
115+
/// <summary>
116+
/// Write log events to the specified file.
117+
/// </summary>
118+
/// <param name="sinkConfiguration">Logger sink configuration.</param>
119+
/// <param name="path">Path to the file.</param>
120+
/// <param name="restrictedToMinimumLevel">The minimum level for
121+
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
122+
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
123+
/// to be changed at runtime.</param>
124+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
125+
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
126+
/// the default is "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}".</param>
127+
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
128+
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
129+
/// will be written in full even if it exceeds the limit.</param>
130+
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
131+
/// is false.</param>
132+
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
133+
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
134+
/// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
135+
/// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
136+
/// will have a number appended in the format <code>_NNN</code>, with the first filename given no number.</param>
137+
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
138+
/// including the current log file. For unlimited retention, pass null. The default is 31.</param>
139+
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param>
140+
/// <returns>Configuration object allowing method chaining.</returns>
141+
[Obsolete("New code should not be compiled against this obsolete overload"), EditorBrowsable(EditorBrowsableState.Never)]
142+
public static LoggerConfiguration File(
143+
this LoggerSinkConfiguration sinkConfiguration,
144+
string path,
145+
LogEventLevel restrictedToMinimumLevel,
146+
string outputTemplate,
147+
IFormatProvider formatProvider,
148+
long? fileSizeLimitBytes,
149+
LoggingLevelSwitch levelSwitch,
150+
bool buffered,
151+
bool shared,
152+
TimeSpan? flushToDiskInterval,
153+
RollingInterval rollingInterval,
154+
bool rollOnFileSizeLimit,
155+
int? retainedFileCountLimit,
156+
Encoding encoding)
157+
{
158+
return File(sinkConfiguration, path, restrictedToMinimumLevel, outputTemplate, formatProvider, fileSizeLimitBytes, levelSwitch, buffered,
159+
shared, flushToDiskInterval, rollingInterval, rollOnFileSizeLimit, retainedFileCountLimit, encoding, null);
160+
}
161+
162+
/// <summary>
163+
/// Write log events to the specified file.
164+
/// </summary>
165+
/// <param name="sinkConfiguration">Logger sink configuration.</param>
166+
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
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)"/>
169+
/// and specify the outputTemplate parameter instead.
170+
/// </param>
171+
/// <param name="path">Path to the file.</param>
172+
/// <param name="restrictedToMinimumLevel">The minimum level for
173+
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
174+
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
175+
/// to be changed at runtime.</param>
176+
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
177+
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
178+
/// will be written in full even if it exceeds the limit.</param>
179+
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
180+
/// is false.</param>
181+
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
182+
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
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
185+
/// will have a number appended in the format <code>_NNN</code>, with the first filename given no number.</param>
186+
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
187+
/// including the current log file. For unlimited retention, pass null. The default is 31.</param>
188+
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param>
189+
/// <returns>Configuration object allowing method chaining.</returns>
190+
[Obsolete("New code should not be compiled against this obsolete overload"), EditorBrowsable(EditorBrowsableState.Never)]
191+
public static LoggerConfiguration File(
192+
this LoggerSinkConfiguration sinkConfiguration,
193+
ITextFormatter formatter,
194+
string path,
195+
LogEventLevel restrictedToMinimumLevel,
196+
long? fileSizeLimitBytes,
197+
LoggingLevelSwitch levelSwitch,
198+
bool buffered,
199+
bool shared,
200+
TimeSpan? flushToDiskInterval,
201+
RollingInterval rollingInterval,
202+
bool rollOnFileSizeLimit,
203+
int? retainedFileCountLimit,
204+
Encoding encoding)
205+
{
206+
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered,
207+
shared, flushToDiskInterval, rollingInterval, rollOnFileSizeLimit, retainedFileCountLimit, encoding, null);
116208
}
117209

118210
/// <summary>
@@ -142,7 +234,6 @@ public static LoggerConfiguration File(
142234
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param>
143235
/// <param name="hooks">Optionally enables hooking into log file lifecycle events.</param>
144236
/// <returns>Configuration object allowing method chaining.</returns>
145-
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
146237
public static LoggerConfiguration File(
147238
this LoggerSinkConfiguration sinkConfiguration,
148239
string path,
@@ -215,6 +306,10 @@ public static LoggerConfiguration File(
215306
Encoding encoding = null,
216307
FileLifecycleHooks hooks = null)
217308
{
309+
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
310+
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
311+
if (path == null) throw new ArgumentNullException(nameof(path));
312+
218313
return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch,
219314
buffered, false, shared, flushToDiskInterval, encoding, rollingInterval, rollOnFileSizeLimit,
220315
retainedFileCountLimit, hooks);
@@ -243,80 +338,71 @@ public static LoggerConfiguration File(
243338
IFormatProvider formatProvider,
244339
LoggingLevelSwitch levelSwitch)
245340
{
246-
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
247-
if (path == null) throw new ArgumentNullException(nameof(path));
248-
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
249-
250-
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
251-
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, levelSwitch, null);
341+
return File(sinkConfiguration, path, restrictedToMinimumLevel, outputTemplate, formatProvider, levelSwitch, null, null);
252342
}
253343

254344
/// <summary>
255345
/// Write log events to the specified file.
256346
/// </summary>
257347
/// <param name="sinkConfiguration">Logger sink configuration.</param>
348+
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
349+
/// text for the file. If control of regular text formatting is required, use the other
350+
/// overload of <see cref="File(LoggerAuditSinkConfiguration, string, LogEventLevel, string, IFormatProvider, LoggingLevelSwitch)"/>
351+
/// and specify the outputTemplate parameter instead.
352+
/// </param>
258353
/// <param name="path">Path to the file.</param>
259354
/// <param name="restrictedToMinimumLevel">The minimum level for
260355
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
261356
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
262357
/// to be changed at runtime.</param>
263-
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
264-
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
265-
/// the default is "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}".</param>
266-
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param>
267-
/// <param name="hooks">Optionally enables hooking into log file lifecycle events.</param>
268358
/// <returns>Configuration object allowing method chaining.</returns>
359+
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
360+
[Obsolete("New code should not be compiled against this obsolete overload"), EditorBrowsable(EditorBrowsableState.Never)]
269361
public static LoggerConfiguration File(
270362
this LoggerAuditSinkConfiguration sinkConfiguration,
363+
ITextFormatter formatter,
271364
string path,
272-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
273-
string outputTemplate = DefaultOutputTemplate,
274-
IFormatProvider formatProvider = null,
275-
LoggingLevelSwitch levelSwitch = null,
276-
Encoding encoding = null,
277-
FileLifecycleHooks hooks = null)
365+
LogEventLevel restrictedToMinimumLevel,
366+
LoggingLevelSwitch levelSwitch)
278367
{
279-
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
280-
if (path == null) throw new ArgumentNullException(nameof(path));
281-
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
282-
283-
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
284-
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, levelSwitch, encoding, hooks);
368+
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, levelSwitch, null, null);
285369
}
286-
370+
287371
/// <summary>
288-
/// Write log events to the specified file.
372+
/// Write audit log events to the specified file.
289373
/// </summary>
290374
/// <param name="sinkConfiguration">Logger sink configuration.</param>
291-
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
292-
/// text for the file. If control of regular text formatting is required, use the other
293-
/// overload of <see cref="File(LoggerAuditSinkConfiguration, string, LogEventLevel, string, IFormatProvider, LoggingLevelSwitch)"/>
294-
/// and specify the outputTemplate parameter instead.
295-
/// </param>
296375
/// <param name="path">Path to the file.</param>
297376
/// <param name="restrictedToMinimumLevel">The minimum level for
298377
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
299378
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
300379
/// to be changed at runtime.</param>
380+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
381+
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
382+
/// the default is "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}".</param>
383+
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param>
384+
/// <param name="hooks">Optionally enables hooking into log file lifecycle events.</param>
301385
/// <returns>Configuration object allowing method chaining.</returns>
302-
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
303-
[Obsolete("New code should not be compiled against this obsolete overload"), EditorBrowsable(EditorBrowsableState.Never)]
304386
public static LoggerConfiguration File(
305387
this LoggerAuditSinkConfiguration sinkConfiguration,
306-
ITextFormatter formatter,
307388
string path,
308-
LogEventLevel restrictedToMinimumLevel,
309-
LoggingLevelSwitch levelSwitch)
389+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
390+
string outputTemplate = DefaultOutputTemplate,
391+
IFormatProvider formatProvider = null,
392+
LoggingLevelSwitch levelSwitch = null,
393+
Encoding encoding = null,
394+
FileLifecycleHooks hooks = null)
310395
{
311396
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
312-
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
313397
if (path == null) throw new ArgumentNullException(nameof(path));
398+
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
314399

315-
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, levelSwitch, null);
400+
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
401+
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, levelSwitch, encoding, hooks);
316402
}
317403

318404
/// <summary>
319-
/// Write log events to the specified file.
405+
/// Write audit log events to the specified file.
320406
/// </summary>
321407
/// <param name="sinkConfiguration">Logger sink configuration.</param>
322408
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into

0 commit comments

Comments
 (0)