Skip to content

Commit e712ec2

Browse files
authored
Merge pull request #147 from serilog/fix/file-size-limit
fix: #146, file size limit
2 parents 865b9f2 + 2be5531 commit e712ec2

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace Serilog
3232
public static class FileLoggerConfigurationExtensions
3333
{
3434
const int DefaultRetainedFileCountLimit = 31; // A long month of logs
35-
const long DefaultFileSizeLimitBytes = 1L * 1024 * 1024 * 1024;
35+
const long DefaultFileSizeLimitBytes = 1L * 1024 * 1024 * 1024; // 1GB
3636
const string DefaultOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}";
3737

3838
/// <summary>
@@ -181,7 +181,7 @@ public static LoggerConfiguration File(
181181
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
182182
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
183183
/// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
184-
/// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
184+
/// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
185185
/// will have a number appended in the format <code>_NNN</code>, with the first filename given no number.</param>
186186
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
187187
/// including the current log file. For unlimited retention, pass null. The default is 31.</param>
@@ -227,7 +227,7 @@ public static LoggerConfiguration File(
227227
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
228228
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
229229
/// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
230-
/// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
230+
/// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
231231
/// will have a number appended in the format <code>_NNN</code>, with the first filename given no number.</param>
232232
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
233233
/// including the current log file. For unlimited retention, pass null. The default is 31.</param>
@@ -377,7 +377,7 @@ public static LoggerConfiguration File(
377377
{
378378
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, levelSwitch, null, null);
379379
}
380-
380+
381381
/// <summary>
382382
/// Write audit log events to the specified file.
383383
/// </summary>
@@ -466,7 +466,7 @@ static LoggerConfiguration ConfigureFile(
466466
if (addSink == null) throw new ArgumentNullException(nameof(addSink));
467467
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
468468
if (path == null) throw new ArgumentNullException(nameof(path));
469-
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) throw new ArgumentException("Negative value provided; file size limit must be non-negative.", nameof(fileSizeLimitBytes));
469+
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.", nameof(fileSizeLimitBytes));
470470
if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) throw new ArgumentException("At least one file must be retained.", nameof(retainedFileCountLimit));
471471
if (retainedFileTimeLimit.HasValue && retainedFileTimeLimit < TimeSpan.Zero) throw new ArgumentException("Negative value provided; retained file time limit must be non-negative.", nameof(retainedFileTimeLimit));
472472
if (shared && buffered) throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered));

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal FileSink(
6161
FileLifecycleHooks hooks)
6262
{
6363
if (path == null) throw new ArgumentNullException(nameof(path));
64-
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) throw new ArgumentException("Negative value provided; file size limit must be non-negative.");
64+
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
6565
_textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter));
6666
_fileSizeLimitBytes = fileSizeLimitBytes;
6767
_buffered = buffered;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public RollingFileSink(string path,
5555
TimeSpan? retainedFileTimeLimit)
5656
{
5757
if (path == null) throw new ArgumentNullException(nameof(path));
58-
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) throw new ArgumentException("Negative value provided; file size limit must be non-negative");
58+
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
5959
if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1");
6060
if (retainedFileTimeLimit.HasValue && retainedFileTimeLimit < TimeSpan.Zero) throw new ArgumentException("Negative value provided; retained file time limit must be non-negative.", nameof(retainedFileTimeLimit));
6161

@@ -223,7 +223,7 @@ bool ShouldRetainFile(RollingLogFile file, int index, DateTime now)
223223
{
224224
return false;
225225
}
226-
226+
227227
return true;
228228
}
229229

src/Serilog.Sinks.File/Sinks/File/SharedFileSink.AtomicAppend.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public sealed class SharedFileSink : IFileSink, IDisposable
5353
/// <exception cref="IOException"></exception>
5454
public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null)
5555
{
56-
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0)
57-
throw new ArgumentException("Negative value provided; file size limit must be non-negative");
56+
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1)
57+
throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null");
5858

5959
_path = path ?? throw new ArgumentNullException(nameof(path));
6060
_textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter));

src/Serilog.Sinks.File/Sinks/File/SharedFileSink.OSMutex.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public sealed class SharedFileSink : IFileSink, IDisposable
5353
public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null)
5454
{
5555
if (path == null) throw new ArgumentNullException(nameof(path));
56-
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0)
57-
throw new ArgumentException("Negative value provided; file size limit must be non-negative");
56+
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1)
57+
throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
5858
_textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter));
5959
_fileSizeLimitBytes = fileSizeLimitBytes;
6060

0 commit comments

Comments
 (0)