13
13
// limitations under the License.
14
14
15
15
using System ;
16
+ using System . ComponentModel ;
16
17
using Serilog . Configuration ;
17
18
using Serilog . Core ;
18
19
using Serilog . Debugging ;
22
23
using Serilog . Formatting . Json ;
23
24
using Serilog . Sinks . File ;
24
25
26
+ // ReSharper disable MethodOverloadWithOptionalParameter
27
+
25
28
namespace Serilog
26
29
{
27
30
/// <summary>Extends <see cref="LoggerConfiguration"/> with methods to add file sinks.</summary>
28
31
public static class FileLoggerConfigurationExtensions
29
32
{
30
33
const long DefaultFileSizeLimitBytes = 1L * 1024 * 1024 * 1024 ;
31
- const string DefaultOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [ {Level}] {Message}{NewLine}{Exception}" ;
34
+ const string DefaultOutputTemplate = "[ {Timestamp:o} {Level:u3 }] {Message:lj} {Properties }{NewLine}{Exception}" ;
32
35
33
36
/// <summary>
34
37
/// Write log events to the specified file.
@@ -41,7 +44,7 @@ public static class FileLoggerConfigurationExtensions
41
44
/// to be changed at runtime.</param>
42
45
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
43
46
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
44
- /// the default is "{Timestamp} [ {Level}] {Message}{NewLine}{Exception}".</param>
47
+ /// the default is "[ {Timestamp:o} {Level:u3 }] {Message:lj} {Properties }{NewLine}{Exception}".</param>
45
48
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
46
49
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
47
50
/// will be written in full even if it exceeds the limit.</param>
@@ -51,6 +54,89 @@ public static class FileLoggerConfigurationExtensions
51
54
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
52
55
/// <returns>Configuration object allowing method chaining.</returns>
53
56
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
57
+ [ Obsolete ( "New code should not be compiled against this obsolete overload" ) , EditorBrowsable ( EditorBrowsableState . Never ) ]
58
+ public static LoggerConfiguration File (
59
+ this LoggerSinkConfiguration sinkConfiguration ,
60
+ string path ,
61
+ LogEventLevel restrictedToMinimumLevel ,
62
+ string outputTemplate ,
63
+ IFormatProvider formatProvider ,
64
+ long ? fileSizeLimitBytes ,
65
+ LoggingLevelSwitch levelSwitch ,
66
+ bool buffered ,
67
+ bool shared ,
68
+ TimeSpan ? flushToDiskInterval )
69
+ {
70
+ if ( sinkConfiguration == null ) throw new ArgumentNullException ( nameof ( sinkConfiguration ) ) ;
71
+ if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
72
+ if ( outputTemplate == null ) throw new ArgumentNullException ( nameof ( outputTemplate ) ) ;
73
+
74
+ var formatter = new MessageTemplateTextFormatter ( outputTemplate , formatProvider ) ;
75
+ return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared , flushToDiskInterval : flushToDiskInterval ) ;
76
+ }
77
+
78
+ /// <summary>
79
+ /// Write log events to the specified file.
80
+ /// </summary>
81
+ /// <param name="sinkConfiguration">Logger sink configuration.</param>
82
+ /// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
83
+ /// 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?)"/>
85
+ /// and specify the outputTemplate parameter instead.
86
+ /// </param>
87
+ /// <param name="path">Path to the file.</param>
88
+ /// <param name="restrictedToMinimumLevel">The minimum level for
89
+ /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
90
+ /// <param name="levelSwitch">A switch allowing the pass-through minimum level
91
+ /// to be changed at runtime.</param>
92
+ /// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
93
+ /// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
94
+ /// will be written in full even if it exceeds the limit.</param>
95
+ /// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
96
+ /// is false.</param>
97
+ /// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
98
+ /// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
99
+ /// <returns>Configuration object allowing method chaining.</returns>
100
+ /// <remarks>The file will be written using the UTF-8 character set.</remarks>
101
+ [ Obsolete ( "New code should not be compiled against this obsolete overload" ) , EditorBrowsable ( EditorBrowsableState . Never ) ]
102
+ public static LoggerConfiguration File (
103
+ this LoggerSinkConfiguration sinkConfiguration ,
104
+ ITextFormatter formatter ,
105
+ string path ,
106
+ LogEventLevel restrictedToMinimumLevel ,
107
+ long ? fileSizeLimitBytes ,
108
+ LoggingLevelSwitch levelSwitch ,
109
+ bool buffered ,
110
+ bool shared ,
111
+ TimeSpan ? flushToDiskInterval )
112
+ {
113
+ return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared , flushToDiskInterval : flushToDiskInterval ) ;
114
+ }
115
+
116
+ /// <summary>
117
+ /// Write log events to the specified file.
118
+ /// </summary>
119
+ /// <param name="sinkConfiguration">Logger sink configuration.</param>
120
+ /// <param name="path">Path to the file.</param>
121
+ /// <param name="restrictedToMinimumLevel">The minimum level for
122
+ /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
123
+ /// <param name="levelSwitch">A switch allowing the pass-through minimum level
124
+ /// to be changed at runtime.</param>
125
+ /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
126
+ /// <param name="outputTemplate">A message template describing the format used to write to the sink.
127
+ /// the default is "[{Timestamp:o} {Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}".</param>
128
+ /// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
129
+ /// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
130
+ /// will be written in full even if it exceeds the limit.</param>
131
+ /// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
132
+ /// is false.</param>
133
+ /// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
134
+ /// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
135
+ /// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
136
+ /// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
137
+ /// will have a number appended in the format <code>_NNNNN</code>, with the first filename given no number.</param>
138
+ /// <returns>Configuration object allowing method chaining.</returns>
139
+ /// <remarks>The file will be written using the UTF-8 character set.</remarks>
54
140
public static LoggerConfiguration File (
55
141
this LoggerSinkConfiguration sinkConfiguration ,
56
142
string path ,
@@ -61,14 +147,16 @@ public static LoggerConfiguration File(
61
147
LoggingLevelSwitch levelSwitch = null ,
62
148
bool buffered = false ,
63
149
bool shared = false ,
64
- TimeSpan ? flushToDiskInterval = null )
150
+ TimeSpan ? flushToDiskInterval = null ,
151
+ RollingInterval rollingInterval = RollingInterval . Infinite ,
152
+ bool rollOnFileSizeLimit = false )
65
153
{
66
154
if ( sinkConfiguration == null ) throw new ArgumentNullException ( nameof ( sinkConfiguration ) ) ;
67
155
if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
68
156
if ( outputTemplate == null ) throw new ArgumentNullException ( nameof ( outputTemplate ) ) ;
69
157
70
158
var formatter = new MessageTemplateTextFormatter ( outputTemplate , formatProvider ) ;
71
- return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared , flushToDiskInterval : flushToDiskInterval ) ;
159
+ return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared , flushToDiskInterval : flushToDiskInterval , rollingInterval : rollingInterval , rollOnFileSizeLimit : rollOnFileSizeLimit ) ;
72
160
}
73
161
74
162
/// <summary>
@@ -77,7 +165,7 @@ public static LoggerConfiguration File(
77
165
/// <param name="sinkConfiguration">Logger sink configuration.</param>
78
166
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
79
167
/// text for the file. If control of regular text formatting is required, use the other
80
- /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?)"/>
168
+ /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval, bool )"/>
81
169
/// and specify the outputTemplate parameter instead.
82
170
/// </param>
83
171
/// <param name="path">Path to the file.</param>
@@ -92,6 +180,9 @@ public static LoggerConfiguration File(
92
180
/// is false.</param>
93
181
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
94
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>_NNNNN</code>, with the first filename given no number.</param>
95
186
/// <returns>Configuration object allowing method chaining.</returns>
96
187
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
97
188
public static LoggerConfiguration File (
@@ -103,9 +194,11 @@ public static LoggerConfiguration File(
103
194
LoggingLevelSwitch levelSwitch = null ,
104
195
bool buffered = false ,
105
196
bool shared = false ,
106
- TimeSpan ? flushToDiskInterval = null )
197
+ TimeSpan ? flushToDiskInterval = null ,
198
+ RollingInterval rollingInterval = RollingInterval . Infinite ,
199
+ bool rollOnFileSizeLimit = false )
107
200
{
108
- return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared , flushToDiskInterval : flushToDiskInterval ) ;
201
+ return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared , flushToDiskInterval : flushToDiskInterval , rollingInterval : rollingInterval , rollOnFileSizeLimit : rollOnFileSizeLimit ) ;
109
202
}
110
203
111
204
/// <summary>
@@ -119,7 +212,7 @@ public static LoggerConfiguration File(
119
212
/// to be changed at runtime.</param>
120
213
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
121
214
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
122
- /// the default is "{Timestamp} [ {Level}] {Message}{NewLine}{Exception}".</param>
215
+ /// the default is "[ {Timestamp:o} {Level:u3 }] {Message:lj} {Properties }{NewLine}{Exception}".</param>
123
216
/// <returns>Configuration object allowing method chaining.</returns>
124
217
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
125
218
public static LoggerConfiguration File (
@@ -174,7 +267,9 @@ static LoggerConfiguration ConfigureFile(
174
267
bool buffered = false ,
175
268
bool propagateExceptions = false ,
176
269
bool shared = false ,
177
- TimeSpan ? flushToDiskInterval = null )
270
+ TimeSpan ? flushToDiskInterval = null ,
271
+ RollingInterval rollingInterval = RollingInterval . Infinite ,
272
+ bool rollOnFileSizeLimit = false )
178
273
{
179
274
if ( addSink == null ) throw new ArgumentNullException ( nameof ( addSink ) ) ;
180
275
if ( formatter == null ) throw new ArgumentNullException ( nameof ( formatter ) ) ;
@@ -212,5 +307,5 @@ static LoggerConfiguration ConfigureFile(
212
307
213
308
return addSink ( sink , restrictedToMinimumLevel , levelSwitch ) ;
214
309
}
215
- }
310
+ }
216
311
}
0 commit comments