Skip to content

Commit 5fd5494

Browse files
committed
Made PeriodicFlushToDiskSink non-generic
1 parent 3fbaaa1 commit 5fd5494

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

+6-18
Original file line numberDiff line numberDiff line change
@@ -197,28 +197,12 @@ static LoggerConfiguration ConfigureFile(
197197
#if ATOMIC_APPEND
198198
if (shared)
199199
{
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-
}
200+
sink = new SharedFileSink(path, formatter, fileSizeLimitBytes);
209201
}
210202
else
211203
{
212204
#endif
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-
}
205+
sink = new FileSink(path, formatter, fileSizeLimitBytes, buffered: buffered);
222206
#if ATOMIC_APPEND
223207
}
224208
#endif
@@ -233,6 +217,10 @@ static LoggerConfiguration ConfigureFile(
233217
return addSink(new NullSink(), LevelAlias.Maximum, null);
234218
}
235219

220+
if (flushToDiskInterval.HasValue)
221+
{
222+
sink = new PeriodicFlushToDiskSink(sink, flushToDiskInterval.Value);
223+
}
236224

237225
return addSink(sink, restrictedToMinimumLevel, levelSwitch);
238226
}

src/Serilog.Sinks.File/Sinks/File/PeriodicFlushToDiskSink`1.cs renamed to src/Serilog.Sinks.File/Sinks/File/PeriodicFlushToDiskSink.cs

+18-10
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)