Skip to content

Commit

Permalink
Merge pull request serilog#656 from Pvlerick/issue-650
Browse files Browse the repository at this point in the history
Adding buffered boolean param to FileSink and RollingFileSink
  • Loading branch information
nblumhardt committed Feb 11, 2016
2 parents a601e96 + 1f674c3 commit 946f0c9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static class FileLoggerConfigurationExtensions
/// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
/// <param name="fileSizeLimitBytes">The maximum size, in bytes, to which a log file will be allowed to grow.
/// For unrestricted growth, pass null. The default is 1 GB.</param>
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
/// is false.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
public static LoggerConfiguration File(
Expand All @@ -50,7 +52,8 @@ public static LoggerConfiguration File(
string outputTemplate = DefaultOutputTemplate,
IFormatProvider formatProvider = null,
long? fileSizeLimitBytes = DefaultFileSizeLimitBytes,
LoggingLevelSwitch levelSwitch = null)
LoggingLevelSwitch levelSwitch = null,
bool buffered = false)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
Expand All @@ -59,7 +62,7 @@ public static LoggerConfiguration File(
FileSink sink;
try
{
sink = new FileSink(path, formatter, fileSizeLimitBytes);
sink = new FileSink(path, formatter, fileSizeLimitBytes, buffered: buffered);
}
catch (ArgumentException)
{
Expand Down
10 changes: 8 additions & 2 deletions src/Serilog.Sinks.File/Sinks/File/FileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public sealed class FileSink : ILogEventSink, IDisposable
const int BytesPerCharacterApproximate = 1;
readonly TextWriter _output;
readonly ITextFormatter _textFormatter;
readonly bool _buffered;
readonly object _syncRoot = new object();

/// <summary>Construct a <see cref="FileSink"/>.</summary>
Expand All @@ -38,16 +39,20 @@ public sealed class FileSink : ILogEventSink, IDisposable
/// <param name="fileSizeLimitBytes">The maximum size, in bytes, to which a log file will be allowed to grow.
/// For unrestricted growth, pass null. The default is 1 GB.</param>
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8.</param>
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
/// is false.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
/// <exception cref="IOException"></exception>
public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null)
public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null,
bool buffered = false)
{
if (path == null) throw new ArgumentNullException(nameof(path));
if (textFormatter == null) throw new ArgumentNullException(nameof(textFormatter));
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) throw new ArgumentException("Negative value provided; file size limit must be non-negative");

_textFormatter = textFormatter;
_buffered = buffered;

TryCreateDirectory(path);

Expand Down Expand Up @@ -91,7 +96,8 @@ public void Emit(LogEvent logEvent)
lock (_syncRoot)
{
_textFormatter.Format(logEvent, _output);
_output.Flush();
if (!_buffered)
_output.Flush();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static class RollingFileLoggerConfigurationExtensions
/// For unrestricted growth, pass null. The default is 1 GB.</param>
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
/// including the current log file. For unlimited retention, pass null. The default is 31.</param>
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
/// is false.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
public static LoggerConfiguration RollingFile(
Expand All @@ -56,12 +58,13 @@ public static LoggerConfiguration RollingFile(
IFormatProvider formatProvider = null,
long? fileSizeLimitBytes = DefaultFileSizeLimitBytes,
int? retainedFileCountLimit = DefaultRetainedFileCountLimit,
LoggingLevelSwitch levelSwitch = null)
LoggingLevelSwitch levelSwitch = null,
bool buffered = false)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
var sink = new RollingFileSink(pathFormat, formatter, fileSizeLimitBytes, retainedFileCountLimit);
var sink = new RollingFileSink(pathFormat, formatter, fileSizeLimitBytes, retainedFileCountLimit, buffered: buffered);
return sinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public sealed class RollingFileSink : ILogEventSink, IDisposable
readonly long? _fileSizeLimitBytes;
readonly int? _retainedFileCountLimit;
readonly Encoding _encoding;
readonly bool _buffered;
readonly object _syncRoot = new object();

bool _isDisposed;
Expand All @@ -54,13 +55,16 @@ public sealed class RollingFileSink : ILogEventSink, IDisposable
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
/// including the current log file. For unlimited retention, pass null. The default is 31.</param>
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8.</param>
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
/// is false.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
public RollingFileSink(string pathFormat,
ITextFormatter textFormatter,
long? fileSizeLimitBytes,
int? retainedFileCountLimit,
Encoding encoding = null)
Encoding encoding = null,
bool buffered = false)
{
if (pathFormat == null) throw new ArgumentNullException(nameof(pathFormat));
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) throw new ArgumentException("Negative value provided; file size limit must be non-negative");
Expand All @@ -71,6 +75,7 @@ public RollingFileSink(string pathFormat,
_fileSizeLimitBytes = fileSizeLimitBytes;
_retainedFileCountLimit = retainedFileCountLimit;
_encoding = encoding ?? Encoding.UTF8;
_buffered = buffered;
}

/// <summary>
Expand Down Expand Up @@ -143,7 +148,7 @@ void OpenFile(DateTime now)

try
{
_currentFile = new FileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding);
_currentFile = new FileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding, _buffered);
}
catch (IOException ex)
{
Expand Down

0 comments on commit 946f0c9

Please sign in to comment.