Skip to content

Commit 16c634d

Browse files
Revert "Allow the sink to flush for events of a minimum log level"
This reverts commit 9f05a09.
1 parent 9f05a09 commit 16c634d

File tree

4 files changed

+16
-122
lines changed

4 files changed

+16
-122
lines changed

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ public static LoggerConfiguration File(
306306
/// Must be greater than or equal to <see cref="TimeSpan.Zero"/>.
307307
/// Ignored if <paramref see="rollingInterval"/> is <see cref="RollingInterval.Infinite"/>.
308308
/// The default is to retain files indefinitely.</param>
309-
/// <param name="flushAtMinimumLevel">The minimum level for events to flush the sink. The default is <see cref="LevelAlias.Off"/>.</param>
310309
/// <returns>Configuration object allowing method chaining.</returns>
311310
/// <exception cref="ArgumentNullException">When <paramref name="sinkConfiguration"/> is <code>null</code></exception>
312311
/// <exception cref="ArgumentNullException">When <paramref name="formatter"/> is <code>null</code></exception>
@@ -332,16 +331,15 @@ public static LoggerConfiguration File(
332331
int? retainedFileCountLimit = DefaultRetainedFileCountLimit,
333332
Encoding? encoding = null,
334333
FileLifecycleHooks? hooks = null,
335-
TimeSpan? retainedFileTimeLimit = null,
336-
LogEventLevel flushAtMinimumLevel = LevelAlias.Off)
334+
TimeSpan? retainedFileTimeLimit = null)
337335
{
338336
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
339337
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
340338
if (path == null) throw new ArgumentNullException(nameof(path));
341339

342340
return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch,
343341
buffered, false, shared, flushToDiskInterval, encoding, rollingInterval, rollOnFileSizeLimit,
344-
retainedFileCountLimit, hooks, retainedFileTimeLimit, flushAtMinimumLevel);
342+
retainedFileCountLimit, hooks, retainedFileTimeLimit);
345343
}
346344

347345
/// <summary>
@@ -496,7 +494,7 @@ public static LoggerConfiguration File(
496494
if (path == null) throw new ArgumentNullException(nameof(path));
497495

498496
return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, null, levelSwitch, false, true,
499-
false, null, encoding, RollingInterval.Infinite, false, null, hooks, null, LevelAlias.Off);
497+
false, null, encoding, RollingInterval.Infinite, false, null, hooks, null);
500498
}
501499

502500
static LoggerConfiguration ConfigureFile(
@@ -515,8 +513,7 @@ static LoggerConfiguration ConfigureFile(
515513
bool rollOnFileSizeLimit,
516514
int? retainedFileCountLimit,
517515
FileLifecycleHooks? hooks,
518-
TimeSpan? retainedFileTimeLimit,
519-
LogEventLevel flushAtMinimumLevel)
516+
TimeSpan? retainedFileTimeLimit)
520517
{
521518
if (addSink == null) throw new ArgumentNullException(nameof(addSink));
522519
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
@@ -533,7 +530,7 @@ static LoggerConfiguration ConfigureFile(
533530
{
534531
if (rollOnFileSizeLimit || rollingInterval != RollingInterval.Infinite)
535532
{
536-
sink = new RollingFileSink(path, formatter, fileSizeLimitBytes, retainedFileCountLimit, encoding, buffered, shared, rollingInterval, rollOnFileSizeLimit, hooks, retainedFileTimeLimit, flushAtMinimumLevel);
533+
sink = new RollingFileSink(path, formatter, fileSizeLimitBytes, retainedFileCountLimit, encoding, buffered, shared, rollingInterval, rollOnFileSizeLimit, hooks, retainedFileTimeLimit);
537534
}
538535
else
539536
{
@@ -545,7 +542,7 @@ static LoggerConfiguration ConfigureFile(
545542
}
546543
else
547544
{
548-
sink = new FileSink(path, formatter, fileSizeLimitBytes, encoding, buffered, hooks, flushAtMinimumLevel);
545+
sink = new FileSink(path, formatter, fileSizeLimitBytes, encoding, buffered, hooks);
549546
}
550547

551548
}

src/Serilog.Sinks.File/Sinks/File/FileSink.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public sealed class FileSink : IFileSink, IDisposable, ISetLoggingFailureListene
3232
readonly bool _buffered;
3333
readonly object _syncRoot = new();
3434
readonly WriteCountingStream? _countingStreamWrapper;
35-
readonly LogEventLevel _flushAtMinimumLevel;
3635

3736
ILoggingFailureListener _failureListener = SelfLog.FailureListener;
3837

@@ -58,7 +57,7 @@ public sealed class FileSink : IFileSink, IDisposable, ISetLoggingFailureListene
5857
/// <exception cref="ArgumentException">Invalid <paramref name="path"/></exception>
5958
[Obsolete("This type and constructor will be removed from the public API in a future version; use `WriteTo.File()` instead.")]
6059
public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding? encoding = null, bool buffered = false)
61-
: this(path, textFormatter, fileSizeLimitBytes, encoding, buffered, null, LevelAlias.Off)
60+
: this(path, textFormatter, fileSizeLimitBytes, encoding, buffered, null)
6261
{
6362
}
6463

@@ -69,15 +68,13 @@ internal FileSink(
6968
long? fileSizeLimitBytes,
7069
Encoding? encoding,
7170
bool buffered,
72-
FileLifecycleHooks? hooks,
73-
LogEventLevel flushAtMinimumLevel)
71+
FileLifecycleHooks? hooks)
7472
{
7573
if (path == null) throw new ArgumentNullException(nameof(path));
7674
if (fileSizeLimitBytes is < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
7775
_textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter));
7876
_fileSizeLimitBytes = fileSizeLimitBytes;
7977
_buffered = buffered;
80-
_flushAtMinimumLevel = flushAtMinimumLevel;
8178

8279
var directory = Path.GetDirectoryName(path);
8380
if (!string.IsNullOrWhiteSpace(directory) && !Directory.Exists(directory))
@@ -127,8 +124,6 @@ bool IFileSink.EmitOrOverflow(LogEvent logEvent)
127124
_textFormatter.Format(logEvent, _output);
128125
if (!_buffered)
129126
_output.Flush();
130-
else if (logEvent.Level >= _flushAtMinimumLevel)
131-
FlushToDisk();
132127

133128
return true;
134129
}

src/Serilog.Sinks.File/Sinks/File/RollingFileSink.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ sealed class RollingFileSink : ILogEventSink, IFlushableFileSink, IDisposable, I
2727
readonly long? _fileSizeLimitBytes;
2828
readonly int? _retainedFileCountLimit;
2929
readonly TimeSpan? _retainedFileTimeLimit;
30-
readonly LogEventLevel _flushAtMinimumLevel;
3130
readonly Encoding? _encoding;
3231
readonly bool _buffered;
3332
readonly bool _shared;
@@ -52,8 +51,7 @@ public RollingFileSink(string path,
5251
RollingInterval rollingInterval,
5352
bool rollOnFileSizeLimit,
5453
FileLifecycleHooks? hooks,
55-
TimeSpan? retainedFileTimeLimit,
56-
LogEventLevel flushAtMinimumLevel)
54+
TimeSpan? retainedFileTimeLimit)
5755
{
5856
if (path == null) throw new ArgumentNullException(nameof(path));
5957
if (fileSizeLimitBytes is < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
@@ -65,7 +63,6 @@ public RollingFileSink(string path,
6563
_fileSizeLimitBytes = fileSizeLimitBytes;
6664
_retainedFileCountLimit = retainedFileCountLimit;
6765
_retainedFileTimeLimit = retainedFileTimeLimit;
68-
_flushAtMinimumLevel = flushAtMinimumLevel;
6966
_encoding = encoding;
7067
_buffered = buffered;
7168
_shared = shared;
@@ -179,7 +176,7 @@ void OpenFile(DateTime now, int? minSequence = null)
179176
new SharedFileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding)
180177
:
181178
#pragma warning restore 618
182-
new FileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding, _buffered, _hooks, _flushAtMinimumLevel);
179+
new FileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding, _buffered, _hooks);
183180

184181
_currentFileSequence = sequence;
185182

test/Serilog.Sinks.File.Tests/FileSinkTests.cs

Lines changed: 6 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System.IO.Compression;
1+
using System.IO.Compression;
22
using System.Text;
33
using Serilog.Core;
4-
using Serilog.Events;
54
using Xunit;
65
using Serilog.Formatting.Json;
76
using Serilog.Sinks.File.Tests.Support;
@@ -147,7 +146,7 @@ public void OnOpenedLifecycleHookCanWrapUnderlyingStream()
147146
var path = tmp.AllocateFilename("txt");
148147
var evt = Some.LogEvent("Hello, world!");
149148

150-
using (var sink = new FileSink(path, new JsonFormatter(), null, null, false, gzipWrapper, LevelAlias.Off))
149+
using (var sink = new FileSink(path, new JsonFormatter(), null, null, false, gzipWrapper))
151150
{
152151
sink.Emit(evt);
153152
sink.Emit(evt);
@@ -179,12 +178,12 @@ public static void OnOpenedLifecycleHookCanWriteFileHeader()
179178
var headerWriter = new FileHeaderWriter("This is the file header");
180179

181180
var path = tmp.AllocateFilename("txt");
182-
using (new FileSink(path, new JsonFormatter(), null, new UTF8Encoding(false), false, headerWriter, LevelAlias.Off))
181+
using (new FileSink(path, new JsonFormatter(), null, new UTF8Encoding(false), false, headerWriter))
183182
{
184183
// Open and write header
185184
}
186185

187-
using (var sink = new FileSink(path, new JsonFormatter(), null, new UTF8Encoding(false), false, headerWriter, LevelAlias.Off))
186+
using (var sink = new FileSink(path, new JsonFormatter(), null, new UTF8Encoding(false), false, headerWriter))
188187
{
189188
// Length check should prevent duplicate header here
190189
sink.Emit(Some.LogEvent());
@@ -204,7 +203,7 @@ public static void OnOpenedLifecycleHookCanCaptureFilePath()
204203
var capturePath = new CaptureFilePathHook();
205204

206205
var path = tmp.AllocateFilename("txt");
207-
using (new FileSink(path, new JsonFormatter(), null, new UTF8Encoding(false), false, capturePath, LevelAlias.Off))
206+
using (new FileSink(path, new JsonFormatter(), null, new UTF8Encoding(false), false, capturePath))
208207
{
209208
// Open and capture the log file path
210209
}
@@ -224,7 +223,7 @@ public static void OnOpenedLifecycleHookCanEmptyTheFileContents()
224223
sink.Emit(Some.LogEvent());
225224
}
226225

227-
using (var sink = new FileSink(path, new JsonFormatter(), fileSizeLimitBytes: null, encoding: new UTF8Encoding(false), buffered: false, hooks: emptyFileHook, LevelAlias.Off))
226+
using (var sink = new FileSink(path, new JsonFormatter(), fileSizeLimitBytes: null, encoding: new UTF8Encoding(false), buffered: false, hooks: emptyFileHook))
228227
{
229228
// Hook will clear the contents of the file before emitting the log events
230229
sink.Emit(Some.LogEvent());
@@ -236,83 +235,6 @@ public static void OnOpenedLifecycleHookCanEmptyTheFileContents()
236235
Assert.Equal('{', lines[0][0]);
237236
}
238237

239-
[Fact]
240-
public void WhenFlushAtMinimumLevelIsNotReachedLineIsNotFlushed()
241-
{
242-
using var tmp = TempFolder.ForCaller();
243-
var path = tmp.AllocateFilename("txt");
244-
var formatter = new JsonFormatter();
245-
246-
using (var sink = new FileSink(path, formatter, null, null, true, null, LogEventLevel.Fatal))
247-
{
248-
sink.Emit(Some.LogEvent(level: LogEventLevel.Information));
249-
250-
var lines = ReadAllLinesShared(path);
251-
Assert.Empty(lines);
252-
}
253-
254-
var savedLines = System.IO.File.ReadAllLines(path);
255-
Assert.Single(savedLines);
256-
}
257-
258-
[Fact]
259-
public void WhenFlushAtMinimumLevelIsReachedLineIsFlushed()
260-
{
261-
using var tmp = TempFolder.ForCaller();
262-
var path = tmp.AllocateFilename("txt");
263-
var formatter = new JsonFormatter();
264-
265-
using (var sink = new FileSink(path, formatter, null, null, true, null, LogEventLevel.Fatal))
266-
{
267-
sink.Emit(Some.LogEvent(level: LogEventLevel.Fatal));
268-
269-
var lines = ReadAllLinesShared(path);
270-
Assert.Single(lines);
271-
}
272-
273-
var savedLines = System.IO.File.ReadAllLines(path);
274-
Assert.Single(savedLines);
275-
}
276-
277-
[Fact]
278-
public void WhenFlushAtMinimumLevelIsOffLineIsNotFlushed()
279-
{
280-
using var tmp = TempFolder.ForCaller();
281-
var path = tmp.AllocateFilename("txt");
282-
var formatter = new JsonFormatter();
283-
284-
using (var sink = new FileSink(path, formatter, null, null, true, null, LevelAlias.Off))
285-
{
286-
sink.Emit(Some.LogEvent(level: LogEventLevel.Fatal));
287-
288-
var lines = ReadAllLinesShared(path);
289-
Assert.Empty(lines);
290-
}
291-
292-
var savedLines = System.IO.File.ReadAllLines(path);
293-
Assert.Single(savedLines);
294-
}
295-
296-
[Fact]
297-
public void WhenFlushAtMinimumLevelIsReachedMultipleLinesAreFlushed()
298-
{
299-
using var tmp = TempFolder.ForCaller();
300-
var path = tmp.AllocateFilename("txt");
301-
var formatter = new JsonFormatter();
302-
303-
using (var sink = new FileSink(path, formatter, null, null, true, null, LogEventLevel.Error))
304-
{
305-
sink.Emit(Some.LogEvent(level: LogEventLevel.Information));
306-
sink.Emit(Some.LogEvent(level: LogEventLevel.Fatal));
307-
308-
var lines = ReadAllLinesShared(path);
309-
Assert.Equal(2, lines.Length);
310-
}
311-
312-
var savedLines = System.IO.File.ReadAllLines(path);
313-
Assert.Equal(2, savedLines.Length);
314-
}
315-
316238
static void WriteTwoEventsAndCheckOutputFileLength(long? maxBytes, Encoding encoding)
317239
{
318240
using var tmp = TempFolder.ForCaller();
@@ -338,21 +260,4 @@ static void WriteTwoEventsAndCheckOutputFileLength(long? maxBytes, Encoding enco
338260
size = new FileInfo(path).Length;
339261
Assert.Equal(encoding.GetPreamble().Length + eventOuputLength * 2, size);
340262
}
341-
342-
private static string[] ReadAllLinesShared(string path)
343-
{
344-
// ReadAllLines cannot be used here, as it can't read files even if they are opened with FileShare.Read
345-
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
346-
using var reader = new StreamReader(fs);
347-
348-
string? line;
349-
List<string> lines = [];
350-
351-
while ((line = reader.ReadLine()) != null)
352-
{
353-
lines.Add(line);
354-
}
355-
356-
return [.. lines];
357-
}
358263
}

0 commit comments

Comments
 (0)