Skip to content

Added intervalMultiplier #263

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
29 changes: 19 additions & 10 deletions src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static LoggerConfiguration File(
TimeSpan? flushToDiskInterval)
{
return File(sinkConfiguration, path, restrictedToMinimumLevel, outputTemplate, formatProvider, fileSizeLimitBytes,
levelSwitch, buffered, shared, flushToDiskInterval, RollingInterval.Infinite, false, null, null, null);
levelSwitch, buffered, shared, flushToDiskInterval, RollingInterval.Infinite, 1,false, null, null, null);
}

/// <summary>
Expand Down Expand Up @@ -110,7 +110,7 @@ public static LoggerConfiguration File(
TimeSpan? flushToDiskInterval)
{
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch,
buffered, shared, flushToDiskInterval, RollingInterval.Infinite, false, null, null, null);
buffered, shared, flushToDiskInterval, RollingInterval.Infinite,1, false, null, null, null);
}

/// <summary>
Expand All @@ -133,6 +133,7 @@ public static LoggerConfiguration File(
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
/// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
/// <param name="intervalMultiplier">The number is multiplied by the interval.</param>
/// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
/// will have a number appended in the format <code>_NNN</code>, with the first filename given no number.</param>
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
Expand All @@ -152,12 +153,13 @@ public static LoggerConfiguration File(
bool shared,
TimeSpan? flushToDiskInterval,
RollingInterval rollingInterval,
int intervalMultiplier,
bool rollOnFileSizeLimit,
int? retainedFileCountLimit,
Encoding encoding)
{
return File(sinkConfiguration, path, restrictedToMinimumLevel, outputTemplate, formatProvider, fileSizeLimitBytes, levelSwitch, buffered,
shared, flushToDiskInterval, rollingInterval, rollOnFileSizeLimit, retainedFileCountLimit, encoding, null);
shared, flushToDiskInterval, rollingInterval, intervalMultiplier,rollOnFileSizeLimit, retainedFileCountLimit, encoding, null);
}

/// <summary>
Expand All @@ -166,7 +168,7 @@ public static LoggerConfiguration File(
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
/// text for the file. If control of regular text formatting is required, use the other
/// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval, bool, int?, Encoding, FileLifecycleHooks, TimeSpan?)"/>
/// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval, int, bool, int?, Encoding, FileLifecycleHooks, TimeSpan?)"/>
/// and specify the outputTemplate parameter instead.
/// </param>
/// <param name="path">Path to the file.</param>
Expand All @@ -182,6 +184,7 @@ public static LoggerConfiguration File(
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
/// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
/// <param name="intervalMultiplier">The number is multiplied by the interval.</param>
/// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
/// will have a number appended in the format <code>_NNN</code>, with the first filename given no number.</param>
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
Expand All @@ -200,12 +203,13 @@ public static LoggerConfiguration File(
bool shared,
TimeSpan? flushToDiskInterval,
RollingInterval rollingInterval,
int intervalMultiplier,
bool rollOnFileSizeLimit,
int? retainedFileCountLimit,
Encoding encoding)
{
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered,
shared, flushToDiskInterval, rollingInterval, rollOnFileSizeLimit, retainedFileCountLimit, encoding, null);
shared, flushToDiskInterval, rollingInterval, intervalMultiplier, rollOnFileSizeLimit, retainedFileCountLimit, encoding, null);
}

/// <summary>
Expand All @@ -228,6 +232,7 @@ public static LoggerConfiguration File(
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
/// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
/// <param name="intervalMultiplier">The number is multiplied by the interval.</param>
/// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
/// will have a number appended in the format <code>_NNN</code>, with the first filename given no number.</param>
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
Expand Down Expand Up @@ -260,6 +265,7 @@ public static LoggerConfiguration File(
bool shared = false,
TimeSpan? flushToDiskInterval = null,
RollingInterval rollingInterval = RollingInterval.Infinite,
int intervalMultiplier = 1,
bool rollOnFileSizeLimit = false,
int? retainedFileCountLimit = DefaultRetainedFileCountLimit,
Encoding? encoding = null,
Expand All @@ -273,7 +279,7 @@ public static LoggerConfiguration File(
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes,
levelSwitch, buffered, shared, flushToDiskInterval,
rollingInterval, rollOnFileSizeLimit, retainedFileCountLimit, encoding, hooks, retainedFileTimeLimit);
rollingInterval, intervalMultiplier, rollOnFileSizeLimit, retainedFileCountLimit, encoding, hooks, retainedFileTimeLimit);
}

/// <summary>
Expand All @@ -282,7 +288,7 @@ public static LoggerConfiguration File(
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
/// text for the file. If control of regular text formatting is required, use the other
/// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval, bool, int?, Encoding, FileLifecycleHooks, TimeSpan?)"/>
/// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval,int, bool, int?, Encoding, FileLifecycleHooks, TimeSpan?)"/>
/// and specify the outputTemplate parameter instead.
/// </param>
/// <param name="path">Path to the file.</param>
Expand All @@ -298,6 +304,7 @@ public static LoggerConfiguration File(
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
/// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
/// <param name="intervalMultiplier">The number is multiplied by the interval.</param>
/// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
/// will have a number appended in the format <code>_NNN</code>, with the first filename given no number.</param>
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
Expand Down Expand Up @@ -329,6 +336,7 @@ public static LoggerConfiguration File(
bool shared = false,
TimeSpan? flushToDiskInterval = null,
RollingInterval rollingInterval = RollingInterval.Infinite,
int intervalMultiplier = 1,
bool rollOnFileSizeLimit = false,
int? retainedFileCountLimit = DefaultRetainedFileCountLimit,
Encoding? encoding = null,
Expand All @@ -340,7 +348,7 @@ public static LoggerConfiguration File(
if (path == null) throw new ArgumentNullException(nameof(path));

return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch,
buffered, false, shared, flushToDiskInterval, encoding, rollingInterval, rollOnFileSizeLimit,
buffered, false, shared, flushToDiskInterval, encoding, rollingInterval, intervalMultiplier, rollOnFileSizeLimit,
retainedFileCountLimit, hooks, retainedFileTimeLimit);
}

Expand Down Expand Up @@ -496,7 +504,7 @@ public static LoggerConfiguration File(
if (path == null) throw new ArgumentNullException(nameof(path));

return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, null, levelSwitch, false, true,
false, null, encoding, RollingInterval.Infinite, false, null, hooks, null);
false, null, encoding, RollingInterval.Infinite, 1, false, null, hooks, null);
}

static LoggerConfiguration ConfigureFile(
Expand All @@ -512,6 +520,7 @@ static LoggerConfiguration ConfigureFile(
TimeSpan? flushToDiskInterval,
Encoding? encoding,
RollingInterval rollingInterval,
int intervalMultiplier,
bool rollOnFileSizeLimit,
int? retainedFileCountLimit,
FileLifecycleHooks? hooks,
Expand All @@ -532,7 +541,7 @@ static LoggerConfiguration ConfigureFile(
{
if (rollOnFileSizeLimit || rollingInterval != RollingInterval.Infinite)
{
sink = new RollingFileSink(path, formatter, fileSizeLimitBytes, retainedFileCountLimit, encoding, buffered, shared, rollingInterval, rollOnFileSizeLimit, hooks, retainedFileTimeLimit);
sink = new RollingFileSink(path, formatter, fileSizeLimitBytes, retainedFileCountLimit, encoding, buffered, shared, rollingInterval, intervalMultiplier, rollOnFileSizeLimit, hooks, retainedFileTimeLimit);
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions src/Serilog.Sinks.File/Sinks/File/PathRoller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ class PathRoller
readonly Regex _filenameMatcher;

readonly RollingInterval _interval;
private readonly int _intervalMultiplier;
readonly string _periodFormat;

public PathRoller(string path, RollingInterval interval)
public PathRoller(string path, RollingInterval interval, int intervalMultiplier)
{
if (path == null) throw new ArgumentNullException(nameof(path));
_interval = interval;
_intervalMultiplier = intervalMultiplier;
_periodFormat = interval.GetFormat();

var pathDirectory = Path.GetDirectoryName(path);
Expand Down Expand Up @@ -112,6 +114,6 @@ public IEnumerable<RollingLogFile> SelectMatches(IEnumerable<string> filenames)

public DateTime? GetCurrentCheckpoint(DateTime instant) => _interval.GetCurrentCheckpoint(instant);

public DateTime? GetNextCheckpoint(DateTime instant) => _interval.GetNextCheckpoint(instant);
public DateTime? GetNextCheckpoint(DateTime instant) => _interval.GetNextCheckpoint(_intervalMultiplier, instant);
}
}
4 changes: 3 additions & 1 deletion src/Serilog.Sinks.File/Sinks/File/RollingFileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public RollingFileSink(string path,
bool buffered,
bool shared,
RollingInterval rollingInterval,
int intervalMultiplier,
bool rollOnFileSizeLimit,
FileLifecycleHooks? hooks,
TimeSpan? retainedFileTimeLimit)
Expand All @@ -58,8 +59,9 @@ public RollingFileSink(string path,
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1");
if (retainedFileTimeLimit.HasValue && retainedFileTimeLimit < TimeSpan.Zero) throw new ArgumentException("Negative value provided; retained file time limit must be non-negative.", nameof(retainedFileTimeLimit));
if (intervalMultiplier < 1) intervalMultiplier = 1;

_roller = new PathRoller(path, rollingInterval);
_roller = new PathRoller(path, rollingInterval, intervalMultiplier);
_textFormatter = textFormatter;
_fileSizeLimitBytes = fileSizeLimitBytes;
_retainedFileCountLimit = retainedFileCountLimit;
Expand Down
12 changes: 6 additions & 6 deletions src/Serilog.Sinks.File/Sinks/File/RollingIntervalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static string GetFormat(this RollingInterval interval)
}
}

public static DateTime? GetNextCheckpoint(this RollingInterval interval, DateTime instant)
public static DateTime? GetNextCheckpoint(this RollingInterval interval, int intervalMultiiplier, DateTime instant)
{
var current = GetCurrentCheckpoint(interval, instant);
if (current == null)
Expand All @@ -69,15 +69,15 @@ public static string GetFormat(this RollingInterval interval)
switch (interval)
{
case RollingInterval.Year:
return current.Value.AddYears(1);
return current.Value.AddYears(intervalMultiiplier);
case RollingInterval.Month:
return current.Value.AddMonths(1);
return current.Value.AddMonths(intervalMultiiplier);
case RollingInterval.Day:
return current.Value.AddDays(1);
return current.Value.AddDays(intervalMultiiplier);
case RollingInterval.Hour:
return current.Value.AddHours(1);
return current.Value.AddHours(intervalMultiiplier);
case RollingInterval.Minute:
return current.Value.AddMinutes(1);
return current.Value.AddMinutes(intervalMultiiplier);
default:
throw new ArgumentException("Invalid rolling interval");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void NextIntervalTests(RollingInterval interval, DateTime instant, DateTi
var current = interval.GetCurrentCheckpoint(instant);
Assert.Equal(currentCheckpoint, current);

var next = interval.GetNextCheckpoint(instant);
var next = interval.GetNextCheckpoint(1, instant);
Assert.Equal(nextCheckpoint, next);
}
}
Expand Down
Loading