Skip to content

Preparations for https://github.com/serilog/serilog-sinks-rollingfile… #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/Serilog.Sinks.File/Sinks/File/FileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Serilog.Sinks.File
/// <summary>
/// Write log events to a disk file.
/// </summary>
public sealed class FileSink : ILogEventSink, IFlushableFileSink, IDisposable
public sealed class FileSink : ILogEventSink, IFlushableFileSink, ISizeLimitedFileSink, IDisposable
{
readonly TextWriter _output;
readonly FileStream _underlyingStream;
Expand Down Expand Up @@ -71,6 +71,16 @@ public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBy
_output = new StreamWriter(outputStream, encoding ?? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
}

/// <inheritdoc />
public bool SizeLimitReached
{
get
{
if (_fileSizeLimitBytes == null) return false;
return _countingStreamWrapper.CountedLength >= _fileSizeLimitBytes.Value;
}
}

/// <summary>
/// Emit the provided log event to the sink.
/// </summary>
Expand All @@ -80,11 +90,7 @@ public void Emit(LogEvent logEvent)
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
lock (_syncRoot)
{
if (_fileSizeLimitBytes != null)
{
if (_countingStreamWrapper.CountedLength >= _fileSizeLimitBytes.Value)
return;
}
if (SizeLimitReached) return;

_textFormatter.Format(logEvent, _output);
if (!_buffered)
Expand Down
16 changes: 16 additions & 0 deletions src/Serilog.Sinks.File/Sinks/File/ISizeLimitedFileSink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Serilog.Sinks.File
{
/// <summary>
/// Supported by (file-based) sinks that has a limit that could be reached
/// </summary>
public interface ISizeLimitedFileSink
{
/// <summary>
/// Gets a value indicating whether size limit reached.
/// </summary>
/// <value>
/// <c>true</c> if size limit reached; otherwise, <c>false</c>.
/// </value>
bool SizeLimitReached { get; }
}
}
16 changes: 15 additions & 1 deletion src/Serilog.Sinks.File/Sinks/File/PeriodicFlushToDiskSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Serilog.Sinks.File
/// <summary>
/// A sink wrapper that periodically flushes the wrapped sink to disk.
/// </summary>
public class PeriodicFlushToDiskSink : ILogEventSink, IDisposable
public class PeriodicFlushToDiskSink : ILogEventSink, ISizeLimitedFileSink, IDisposable
{
readonly ILogEventSink _sink;
readonly Timer _timer;
Expand Down Expand Up @@ -40,6 +40,20 @@ public PeriodicFlushToDiskSink(ILogEventSink sink, TimeSpan flushInterval)
}
}

/// <inheritdoc />
public bool SizeLimitReached
{
get
{
var sizeLimitedFileSink = _sink as ISizeLimitedFileSink;
if (sizeLimitedFileSink != null)
{
return sizeLimitedFileSink.SizeLimitReached;
}
return false;
}
}

/// <inheritdoc />
public void Emit(LogEvent logEvent)
{
Expand Down
30 changes: 20 additions & 10 deletions src/Serilog.Sinks.File/Sinks/File/SharedFileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Serilog.Sinks.File
/// <summary>
/// Write log events to a disk file.
/// </summary>
public sealed class SharedFileSink : ILogEventSink, IFlushableFileSink, IDisposable
public sealed class SharedFileSink : ILogEventSink, IFlushableFileSink, ISizeLimitedFileSink, IDisposable
{
readonly MemoryStream _writeBuffer;
readonly string _path;
Expand Down Expand Up @@ -117,15 +117,7 @@ public void Emit(LogEvent logEvent)
oldOutput.Dispose();
}

if (_fileSizeLimitBytes != null)
{
try
{
if (_fileOutput.Length >= _fileSizeLimitBytes.Value)
return;
}
catch (FileNotFoundException) { } // Cheaper and more reliable than checking existence
}
if(SizeLimitReached) return;

_fileOutput.Write(bytes, 0, length);
_fileOutput.Flush();
Expand All @@ -145,6 +137,24 @@ public void Emit(LogEvent logEvent)
}


/// <inheritdoc />
public bool SizeLimitReached
{
get
{
if (_fileSizeLimitBytes != null)
{
try
{
if (_fileOutput.Length >= _fileSizeLimitBytes.Value)
return true;
}
catch (FileNotFoundException) { } // Cheaper and more reliable than checking existence
}
return false;
}
}

/// <inheritdoc />
public void Dispose()
{
Expand Down