|
16 | 16 | using Serilog.Configuration;
|
17 | 17 | using Serilog.Core;
|
18 | 18 | using Serilog.Events;
|
| 19 | +using Serilog.Formatting; |
19 | 20 | using Serilog.Formatting.Display;
|
20 | 21 | using Serilog.Sinks.RollingFile;
|
21 | 22 |
|
22 | 23 | namespace Serilog
|
23 | 24 | {
|
| 25 | + /// <summary> |
| 26 | + /// Extends <see cref="LoggerSinkConfiguration"/> with rolling file configuration methods. |
| 27 | + /// </summary> |
24 | 28 | public static class RollingFileLoggerConfigurationExtensions
|
25 | 29 | {
|
26 | 30 | const int DefaultRetainedFileCountLimit = 31; // A long month of logs
|
@@ -62,9 +66,46 @@ public static LoggerConfiguration RollingFile(
|
62 | 66 | LoggingLevelSwitch levelSwitch = null,
|
63 | 67 | bool buffered = false)
|
64 | 68 | {
|
65 |
| - if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration)); |
66 |
| - if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate)); |
67 | 69 | var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
|
| 70 | + return RollingFile(sinkConfiguration, formatter, pathFormat, restrictedToMinimumLevel, fileSizeLimitBytes, |
| 71 | + retainedFileCountLimit, levelSwitch, buffered); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Write log events to a series of files. Each file will be named according to |
| 76 | + /// the date of the first log entry written to it. Only simple date-based rolling is |
| 77 | + /// currently supported. |
| 78 | + /// </summary> |
| 79 | + /// <param name="sinkConfiguration">Logger sink configuration.</param> |
| 80 | + /// <param name="formatter">Formatter to control how events are rendered into the file. To control |
| 81 | + /// plain text formatting, use the overload that accepts an output template instead.</param> |
| 82 | + /// <param name="pathFormat">String describing the location of the log files, |
| 83 | + /// with {Date} in the place of the file date. E.g. "Logs\myapp-{Date}.log" will result in log |
| 84 | + /// files such as "Logs\myapp-2013-10-20.log", "Logs\myapp-2013-10-21.log" and so on.</param> |
| 85 | + /// <param name="restrictedToMinimumLevel">The minimum level for |
| 86 | + /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param> |
| 87 | + /// <param name="levelSwitch">A switch allowing the pass-through minimum level |
| 88 | + /// to be changed at runtime.</param> |
| 89 | + /// <param name="fileSizeLimitBytes">The maximum size, in bytes, to which any single log file will be allowed to grow. |
| 90 | + /// For unrestricted growth, pass null. The default is 1 GB.</param> |
| 91 | + /// <param name="retainedFileCountLimit">The maximum number of log files that will be retained, |
| 92 | + /// including the current log file. For unlimited retention, pass null. The default is 31.</param> |
| 93 | + /// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default |
| 94 | + /// is false.</param> |
| 95 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 96 | + /// <remarks>The file will be written using the UTF-8 character set.</remarks> |
| 97 | + public static LoggerConfiguration RollingFile( |
| 98 | + this LoggerSinkConfiguration sinkConfiguration, |
| 99 | + ITextFormatter formatter, |
| 100 | + string pathFormat, |
| 101 | + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, |
| 102 | + long? fileSizeLimitBytes = DefaultFileSizeLimitBytes, |
| 103 | + int? retainedFileCountLimit = DefaultRetainedFileCountLimit, |
| 104 | + LoggingLevelSwitch levelSwitch = null, |
| 105 | + bool buffered = false) |
| 106 | + { |
| 107 | + if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration)); |
| 108 | + if (formatter == null) throw new ArgumentNullException(nameof(formatter)); |
68 | 109 | var sink = new RollingFileSink(pathFormat, formatter, fileSizeLimitBytes, retainedFileCountLimit, buffered: buffered);
|
69 | 110 | return sinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);
|
70 | 111 | }
|
|
0 commit comments