@@ -9,27 +9,35 @@ namespace Serilog.Sinks.File
9
9
/// <summary>
10
10
/// A sink wrapper that periodically flushes the wrapped sink to disk.
11
11
/// </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
15
13
{
16
- readonly TSink _sink ;
14
+ readonly ILogEventSink _sink ;
17
15
readonly Timer _timer ;
18
16
int _flushRequired ;
19
17
20
18
/// <summary>
21
- /// Construct a <see cref="PeriodicFlushToDiskSink{TSink} "/> that wraps
19
+ /// Construct a <see cref="PeriodicFlushToDiskSink"/> that wraps
22
20
/// <paramref name="sink"/> and flushes it at the specified <paramref name="flushInterval"/>.
23
21
/// </summary>
24
22
/// <param name="sink">The sink to wrap.</param>
25
23
/// <param name="flushInterval">The interval at which to flush the underlying sink.</param>
26
24
/// <exception cref="ArgumentNullException"></exception>
27
- public PeriodicFlushToDiskSink ( TSink sink , TimeSpan flushInterval )
25
+ public PeriodicFlushToDiskSink ( ILogEventSink sink , TimeSpan flushInterval )
28
26
{
29
27
if ( sink == null ) throw new ArgumentNullException ( nameof ( sink ) ) ;
30
28
31
29
_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
+ }
33
41
}
34
42
35
43
/// <inheritdoc />
@@ -46,20 +54,20 @@ public void Dispose()
46
54
( _sink as IDisposable ) ? . Dispose ( ) ;
47
55
}
48
56
49
- void FlushToDisk ( )
57
+ void FlushToDisk ( IFlushableFileSink flushable )
50
58
{
51
59
try
52
60
{
53
61
if ( Interlocked . CompareExchange ( ref _flushRequired , 0 , 1 ) == 1 )
54
62
{
55
63
// May throw ObjectDisposedException, since we're not trying to synchronize
56
64
// anything here in the wrapper.
57
- _sink . FlushToDisk ( ) ;
65
+ flushable . FlushToDisk ( ) ;
58
66
}
59
67
}
60
68
catch ( Exception ex )
61
69
{
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 ) ;
63
71
}
64
72
}
65
73
}
0 commit comments