18
18
using Serilog . Events ;
19
19
using Serilog . Formatting ;
20
20
using Serilog . Formatting . Display ;
21
+ using Serilog . Sinks . File ;
21
22
using Serilog . Sinks . RollingFile ;
22
23
23
24
namespace Serilog
@@ -54,6 +55,7 @@ public static class RollingFileLoggerConfigurationExtensions
54
55
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
55
56
/// is false.</param>
56
57
/// <param name="shared">Allow the log files to be shared by multiple processes. The default is false.</param>
58
+ /// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
57
59
/// <returns>Configuration object allowing method chaining.</returns>
58
60
/// <remarks>The file will be written using the UTF-8 encoding without a byte-order mark.</remarks>
59
61
public static LoggerConfiguration RollingFile (
@@ -66,11 +68,12 @@ public static LoggerConfiguration RollingFile(
66
68
int ? retainedFileCountLimit = DefaultRetainedFileCountLimit ,
67
69
LoggingLevelSwitch levelSwitch = null ,
68
70
bool buffered = false ,
69
- bool shared = false )
71
+ bool shared = false ,
72
+ TimeSpan ? flushToDiskInterval = null )
70
73
{
71
74
var formatter = new MessageTemplateTextFormatter ( outputTemplate , formatProvider ) ;
72
75
return RollingFile ( sinkConfiguration , formatter , pathFormat , restrictedToMinimumLevel , fileSizeLimitBytes ,
73
- retainedFileCountLimit , levelSwitch , buffered , shared ) ;
76
+ retainedFileCountLimit , levelSwitch , buffered , shared , flushToDiskInterval ) ;
74
77
}
75
78
76
79
/// <summary>
@@ -95,6 +98,7 @@ public static LoggerConfiguration RollingFile(
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 files to be shared by multiple processes. The default is false.</param>
101
+ /// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
98
102
/// <returns>Configuration object allowing method chaining.</returns>
99
103
/// <remarks>The file will be written using the UTF-8 encoding without a byte-order mark.</remarks>
100
104
public static LoggerConfiguration RollingFile (
@@ -106,16 +110,23 @@ public static LoggerConfiguration RollingFile(
106
110
int ? retainedFileCountLimit = DefaultRetainedFileCountLimit ,
107
111
LoggingLevelSwitch levelSwitch = null ,
108
112
bool buffered = false ,
109
- bool shared = false )
113
+ bool shared = false ,
114
+ TimeSpan ? flushToDiskInterval = null )
110
115
{
111
116
if ( sinkConfiguration == null ) throw new ArgumentNullException ( nameof ( sinkConfiguration ) ) ;
112
117
if ( formatter == null ) throw new ArgumentNullException ( nameof ( formatter ) ) ;
113
118
114
119
if ( shared && buffered )
115
120
throw new ArgumentException ( "Buffered writes are not available when file sharing is enabled." , nameof ( buffered ) ) ;
116
121
117
- var sink = new RollingFileSink ( pathFormat , formatter , fileSizeLimitBytes , retainedFileCountLimit , buffered : buffered , shared : shared ) ;
122
+ ILogEventSink sink = new RollingFileSink ( pathFormat , formatter , fileSizeLimitBytes , retainedFileCountLimit , buffered : buffered , shared : shared ) ;
123
+
124
+ if ( flushToDiskInterval . HasValue )
125
+ {
126
+ sink = new PeriodicFlushToDiskSink ( sink , flushToDiskInterval . Value ) ;
127
+ }
128
+
118
129
return sinkConfiguration . Sink ( sink , restrictedToMinimumLevel , levelSwitch ) ;
119
130
}
120
131
}
121
- }
132
+ }
0 commit comments