@@ -48,6 +48,7 @@ public static class FileLoggerConfigurationExtensions
48
48
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
49
49
/// is false.</param>
50
50
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
51
+ /// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
51
52
/// <returns>Configuration object allowing method chaining.</returns>
52
53
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
53
54
public static LoggerConfiguration File (
@@ -59,14 +60,15 @@ public static LoggerConfiguration File(
59
60
long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
60
61
LoggingLevelSwitch levelSwitch = null ,
61
62
bool buffered = false ,
62
- bool shared = false )
63
+ bool shared = false ,
64
+ TimeSpan ? flushToDiskInterval = null )
63
65
{
64
66
if ( sinkConfiguration == null ) throw new ArgumentNullException ( nameof ( sinkConfiguration ) ) ;
65
67
if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
66
68
if ( outputTemplate == null ) throw new ArgumentNullException ( nameof ( outputTemplate ) ) ;
67
69
68
70
var formatter = new MessageTemplateTextFormatter ( outputTemplate , formatProvider ) ;
69
- return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared ) ;
71
+ return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared , flushToDiskInterval : flushToDiskInterval ) ;
70
72
}
71
73
72
74
/// <summary>
@@ -75,7 +77,7 @@ public static LoggerConfiguration File(
75
77
/// <param name="sinkConfiguration">Logger sink configuration.</param>
76
78
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
77
79
/// text for the file. If control of regular text formatting is required, use the other
78
- /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool)"/>
80
+ /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan? )"/>
79
81
/// and specify the outputTemplate parameter instead.
80
82
/// </param>
81
83
/// <param name="path">Path to the file.</param>
@@ -89,6 +91,7 @@ public static LoggerConfiguration File(
89
91
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
90
92
/// is false.</param>
91
93
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
94
+ /// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
92
95
/// <returns>Configuration object allowing method chaining.</returns>
93
96
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
94
97
public static LoggerConfiguration File (
@@ -99,9 +102,10 @@ public static LoggerConfiguration File(
99
102
long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
100
103
LoggingLevelSwitch levelSwitch = null ,
101
104
bool buffered = false ,
102
- bool shared = false )
105
+ bool shared = false ,
106
+ TimeSpan ? flushToDiskInterval = null )
103
107
{
104
- return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared ) ;
108
+ return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared , flushToDiskInterval : flushToDiskInterval ) ;
105
109
}
106
110
107
111
/// <summary>
@@ -169,7 +173,8 @@ static LoggerConfiguration ConfigureFile(
169
173
LoggingLevelSwitch levelSwitch = null ,
170
174
bool buffered = false ,
171
175
bool propagateExceptions = false ,
172
- bool shared = false )
176
+ bool shared = false ,
177
+ TimeSpan ? flushToDiskInterval = null )
173
178
{
174
179
if ( addSink == null ) throw new ArgumentNullException ( nameof ( addSink ) ) ;
175
180
if ( formatter == null ) throw new ArgumentNullException ( nameof ( formatter ) ) ;
@@ -192,12 +197,28 @@ static LoggerConfiguration ConfigureFile(
192
197
#if ATOMIC_APPEND
193
198
if ( shared )
194
199
{
195
- sink = new SharedFileSink ( path , formatter , fileSizeLimitBytes ) ;
200
+ var sfs = new SharedFileSink ( path , formatter , fileSizeLimitBytes ) ;
201
+ if ( flushToDiskInterval . HasValue )
202
+ {
203
+ sink = new PeriodicFlushToDiskSink < SharedFileSink > ( sfs , flushToDiskInterval . Value ) ;
204
+ }
205
+ else
206
+ {
207
+ sink = sfs ;
208
+ }
196
209
}
197
210
else
198
211
{
199
212
#endif
200
- sink = new FileSink ( path , formatter , fileSizeLimitBytes , buffered : buffered ) ;
213
+ var fs = new FileSink ( path , formatter , fileSizeLimitBytes , buffered : buffered ) ;
214
+ if ( flushToDiskInterval . HasValue )
215
+ {
216
+ sink = new PeriodicFlushToDiskSink < FileSink > ( fs , flushToDiskInterval . Value ) ;
217
+ }
218
+ else
219
+ {
220
+ sink = fs ;
221
+ }
201
222
#if ATOMIC_APPEND
202
223
}
203
224
#endif
@@ -212,6 +233,7 @@ static LoggerConfiguration ConfigureFile(
212
233
return addSink ( new NullSink ( ) , LevelAlias . Maximum , null ) ;
213
234
}
214
235
236
+
215
237
return addSink ( sink , restrictedToMinimumLevel , levelSwitch ) ;
216
238
}
217
239
}
0 commit comments