@@ -9,27 +9,35 @@ namespace Serilog.Sinks.File
99 /// <summary>
1010 /// A sink wrapper that periodically flushes the wrapped sink to disk.
1111 /// </summary>
12- /// <typeparam name="TSink">The type of the wrapped sink.</typeparam>
13- public class PeriodicFlushToDiskSink < TSink > : ILogEventSink , IDisposable
14- where TSink : ILogEventSink , IFlushableFileSink
12+ public class PeriodicFlushToDiskSink : ILogEventSink , IDisposable
1513 {
16- readonly TSink _sink ;
14+ readonly ILogEventSink _sink ;
1715 readonly Timer _timer ;
1816 int _flushRequired ;
1917
2018 /// <summary>
21- /// Construct a <see cref="PeriodicFlushToDiskSink{TSink} "/> that wraps
19+ /// Construct a <see cref="PeriodicFlushToDiskSink"/> that wraps
2220 /// <paramref name="sink"/> and flushes it at the specified <paramref name="flushInterval"/>.
2321 /// </summary>
2422 /// <param name="sink">The sink to wrap.</param>
2523 /// <param name="flushInterval">The interval at which to flush the underlying sink.</param>
2624 /// <exception cref="ArgumentNullException"></exception>
27- public PeriodicFlushToDiskSink ( TSink sink , TimeSpan flushInterval )
25+ public PeriodicFlushToDiskSink ( ILogEventSink sink , TimeSpan flushInterval )
2826 {
2927 if ( sink == null ) throw new ArgumentNullException ( nameof ( sink ) ) ;
3028
3129 _sink = sink ;
32- _timer = new Timer ( _ => FlushToDisk ( ) , null , flushInterval , flushInterval ) ;
30+
31+ var flushable = sink as IFlushableFileSink ;
32+ if ( flushable != null )
33+ {
34+ _timer = new Timer ( _ => FlushToDisk ( flushable ) , null , flushInterval , flushInterval ) ;
35+ }
36+ else
37+ {
38+ _timer = new Timer ( _ => { } , null , Timeout . InfiniteTimeSpan , Timeout . InfiniteTimeSpan ) ;
39+ SelfLog . WriteLine ( "{0} configured to flush {1}, but {2} not implemented" , typeof ( PeriodicFlushToDiskSink ) , sink , nameof ( IFlushableFileSink ) ) ;
40+ }
3341 }
3442
3543 /// <inheritdoc />
@@ -46,20 +54,20 @@ public void Dispose()
4654 ( _sink as IDisposable ) ? . Dispose ( ) ;
4755 }
4856
49- void FlushToDisk ( )
57+ void FlushToDisk ( IFlushableFileSink flushable )
5058 {
5159 try
5260 {
5361 if ( Interlocked . CompareExchange ( ref _flushRequired , 0 , 1 ) == 1 )
5462 {
5563 // May throw ObjectDisposedException, since we're not trying to synchronize
5664 // anything here in the wrapper.
57- _sink . FlushToDisk ( ) ;
65+ flushable . FlushToDisk ( ) ;
5866 }
5967 }
6068 catch ( Exception ex )
6169 {
62- SelfLog . WriteLine ( "Could not flush the underlying sink to disk: {0}" , ex ) ;
70+ SelfLog . WriteLine ( "{0} could not flush the underlying sink to disk: {1}" , typeof ( PeriodicFlushToDiskSink ) , ex ) ;
6371 }
6472 }
6573 }
0 commit comments