Skip to content

#225 RollingInterval more extendable #226

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 2 commits 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
21 changes: 11 additions & 10 deletions src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Serilog.Formatting;
using Serilog.Formatting.Display;
using Serilog.Formatting.Json;
using Serilog.Interval;
using Serilog.Sinks.File;

// ReSharper disable RedundantArgumentDefaultValue, MethodOverloadWithOptionalParameter
Expand Down Expand Up @@ -151,7 +152,7 @@ public static LoggerConfiguration File(
bool buffered,
bool shared,
TimeSpan? flushToDiskInterval,
RollingInterval rollingInterval,
Interval.RollingInterval rollingInterval,
bool rollOnFileSizeLimit,
int? retainedFileCountLimit,
Encoding encoding)
Expand All @@ -166,7 +167,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?, Interval.RollingInterval, bool, int?, Encoding, FileLifecycleHooks, TimeSpan?)"/>
/// and specify the outputTemplate parameter instead.
/// </param>
/// <param name="path">Path to the file.</param>
Expand Down Expand Up @@ -199,7 +200,7 @@ public static LoggerConfiguration File(
bool buffered,
bool shared,
TimeSpan? flushToDiskInterval,
RollingInterval rollingInterval,
Interval.RollingInterval rollingInterval,
bool rollOnFileSizeLimit,
int? retainedFileCountLimit,
Encoding encoding)
Expand Down Expand Up @@ -259,7 +260,7 @@ public static LoggerConfiguration File(
bool buffered = false,
bool shared = false,
TimeSpan? flushToDiskInterval = null,
RollingInterval rollingInterval = RollingInterval.Infinite,
Interval.RollingInterval rollingInterval = null,
bool rollOnFileSizeLimit = false,
int? retainedFileCountLimit = DefaultRetainedFileCountLimit,
Encoding? encoding = null,
Expand All @@ -282,7 +283,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?, Interval.RollingInterval, bool, int?, Encoding, FileLifecycleHooks, TimeSpan?)"/>
/// and specify the outputTemplate parameter instead.
/// </param>
/// <param name="path">Path to the file.</param>
Expand Down Expand Up @@ -328,7 +329,7 @@ public static LoggerConfiguration File(
bool buffered = false,
bool shared = false,
TimeSpan? flushToDiskInterval = null,
RollingInterval rollingInterval = RollingInterval.Infinite,
Interval.RollingInterval rollingInterval = null,
bool rollOnFileSizeLimit = false,
int? retainedFileCountLimit = DefaultRetainedFileCountLimit,
Encoding? encoding = null,
Expand All @@ -340,7 +341,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 ?? new InfiniteRollingInterval(), rollOnFileSizeLimit,
retainedFileCountLimit, hooks, retainedFileTimeLimit);
}

Expand Down Expand Up @@ -510,8 +511,8 @@ static LoggerConfiguration ConfigureFile(
bool propagateExceptions,
bool shared,
TimeSpan? flushToDiskInterval,
Encoding? encoding,
RollingInterval rollingInterval,
Encoding encoding,
Interval.RollingInterval rollingInterval,
bool rollOnFileSizeLimit,
int? retainedFileCountLimit,
FileLifecycleHooks? hooks,
Expand All @@ -530,7 +531,7 @@ static LoggerConfiguration ConfigureFile(

try
{
if (rollOnFileSizeLimit || rollingInterval != RollingInterval.Infinite)
if (rollOnFileSizeLimit || !(rollingInterval is InfiniteRollingInterval))
{
sink = new RollingFileSink(path, formatter, fileSizeLimitBytes, retainedFileCountLimit, encoding, buffered, shared, rollingInterval, rollOnFileSizeLimit, hooks, retainedFileTimeLimit);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Serilog.Sinks.File/Interval/DayRollingInterval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Serilog.Interval
{
/// <summary>
/// Day Rolling Interval
/// </summary>
public class DayRollingInterval : RollingInterval
{
/// <summary>
/// Format of rolling file name
/// </summary>
public override string Format => "yyyyMMdd";

/// <summary>
/// Normalize time to current checkpoint
/// </summary>
public override DateTime? CurrentCheckpoint(DateTime instant) => Normalize(instant);

/// <summary>
/// Calculate next checkpoint from time
/// </summary>
public override DateTime? NextCheckpoint(DateTime instant) => Normalize(instant).AddDays(1);

private static DateTime Normalize(DateTime instant) => new DateTime(instant.Year, instant.Month, instant.Day, 0, 0, 0, instant.Kind);
}
}
27 changes: 27 additions & 0 deletions src/Serilog.Sinks.File/Interval/HourRollingInterval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Serilog.Interval
{
/// <summary>
/// Hour Rolling Interval
/// </summary>
public class HourRollingInterval : RollingInterval
{
/// <summary>
/// Format of rolling file name
/// </summary>
public override string Format => "yyyyMMddHH";

/// <summary>
/// Normalize time to current checkpoint
/// </summary>
public override DateTime? CurrentCheckpoint(DateTime instant) => Normalize(instant);

/// <summary>
/// Calculate next checkpoint from time
/// </summary>
public override DateTime? NextCheckpoint(DateTime instant) => Normalize(instant).AddHours(1);

private static DateTime Normalize(DateTime instant) => new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, 0, 0, instant.Kind);
}
}
25 changes: 25 additions & 0 deletions src/Serilog.Sinks.File/Interval/InfiniteRollingInterval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace Serilog.Interval
{
/// <summary>
/// Infinite Rolling Interval
/// </summary>
public class InfiniteRollingInterval : RollingInterval
{
/// <summary>
/// Format of rolling file name
/// </summary>
public override string Format => string.Empty;

/// <summary>
/// Normalize time to current checkpoint
/// </summary>
public override DateTime? CurrentCheckpoint(DateTime instant) => null;

/// <summary>
/// Calculate next checkpoint from time
/// </summary>
public override DateTime? NextCheckpoint(DateTime instant) => null;
}
}
27 changes: 27 additions & 0 deletions src/Serilog.Sinks.File/Interval/MinuteRollingInterval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Serilog.Interval
{
/// <summary>
/// Minute Rolling Interval
/// </summary>
public class MinuteRollingInterval : RollingInterval
{
/// <summary>
/// Format of rolling file name
/// </summary>
public override string Format => "yyyyMMddHHmm";

/// <summary>
/// Normalize time to current checkpoint
/// </summary>
public override DateTime? CurrentCheckpoint(DateTime instant) => Normalize(instant);

/// <summary>
/// Calculate next checkpoint from time
/// </summary>
public override DateTime? NextCheckpoint(DateTime instant) => Normalize(instant).AddMinutes(1);

private static DateTime Normalize(DateTime instant) => new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, instant.Minute, 0, instant.Kind);
}
}
27 changes: 27 additions & 0 deletions src/Serilog.Sinks.File/Interval/MonthRollingInterval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Serilog.Interval
{
/// <summary>
/// Month Rolling Interval
/// </summary>
public class MonthRollingInterval : RollingInterval
{
/// <summary>
/// Format of rolling file name
/// </summary>
public override string Format => "yyyyMM";

/// <summary>
/// Normalize time to current checkpoint
/// </summary>
public override DateTime? CurrentCheckpoint(DateTime instant) => Normalize(instant);

/// <summary>
/// Calculate next checkpoint from time
/// </summary>
public override DateTime? NextCheckpoint(DateTime instant) => Normalize(instant).AddMonths(1);

private static DateTime Normalize(DateTime instant) => new DateTime(instant.Year, instant.Month, 1, 0, 0, 0, instant.Kind);
}
}
55 changes: 55 additions & 0 deletions src/Serilog.Sinks.File/Interval/RollingInterval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;

namespace Serilog.Interval
{
/// <summary>
/// Specifies the frequency at which the log file should roll.
/// </summary>
public abstract class RollingInterval
{
/// <summary>
/// Format of rolling file name
/// </summary>
public abstract string Format { get; }

/// <summary>
/// Normalize time to current checkpoint (year, month and etc)
/// </summary>
/// <param name="instant"></param>
/// <returns></returns>
public abstract DateTime? CurrentCheckpoint(DateTime instant);

/// <summary>
/// Calculate next checkpoint from time
/// </summary>
/// <param name="instant"></param>
/// <returns></returns>
public abstract DateTime? NextCheckpoint(DateTime instant);

/// <summary>
/// Create RollInterval
/// </summary>
/// <param name="intervalType"></param>
/// <returns></returns>
public static implicit operator RollingInterval(Serilog.RollingInterval intervalType)
{
switch (intervalType)
{
case Serilog.RollingInterval.Infinite:
return new InfiniteRollingInterval();
case Serilog.RollingInterval.Year:
return new YearRollingInterval();
case Serilog.RollingInterval.Month:
return new MonthRollingInterval();
case Serilog.RollingInterval.Day:
return new DayRollingInterval();
case Serilog.RollingInterval.Hour:
return new HourRollingInterval();
case Serilog.RollingInterval.Minute:
return new MinuteRollingInterval();
default:
throw new ArgumentException("Invalid rolling interval");
}
}
}
}
27 changes: 27 additions & 0 deletions src/Serilog.Sinks.File/Interval/YearRollingInterval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Serilog.Interval
{
/// <summary>
/// Year Rolling Interval
/// </summary>
public class YearRollingInterval : RollingInterval
{
/// <summary>
/// Format of rolling file name
/// </summary>
public override string Format => "yyyy";

/// <summary>
/// Normalize time to current checkpoint
/// </summary>
public override DateTime? CurrentCheckpoint(DateTime instant) => Normalize(instant);

/// <summary>
/// Calculate next checkpoint from time
/// </summary>
public override DateTime? NextCheckpoint(DateTime instant) => Normalize(instant).AddYears(1);

private static DateTime Normalize(DateTime instant) => new DateTime(instant.Year, 1, 1, 0, 0, 0, instant.Kind);
}
}
10 changes: 5 additions & 5 deletions src/Serilog.Sinks.File/Sinks/File/PathRoller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class PathRoller
readonly string _filenameSuffix;
readonly Regex _filenameMatcher;

readonly RollingInterval _interval;
readonly Interval.RollingInterval _interval;
readonly string _periodFormat;

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

var pathDirectory = Path.GetDirectoryName(path);
if (string.IsNullOrEmpty(pathDirectory))
Expand Down Expand Up @@ -110,8 +110,8 @@ public IEnumerable<RollingLogFile> SelectMatches(IEnumerable<string> filenames)
}
}

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

public DateTime? GetNextCheckpoint(DateTime instant) => _interval.GetNextCheckpoint(instant);
public DateTime? GetNextCheckpoint(DateTime instant) => _interval.NextCheckpoint(instant);
}
}
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.File/Sinks/File/RollingFileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public RollingFileSink(string path,
Encoding? encoding,
bool buffered,
bool shared,
RollingInterval rollingInterval,
Interval.RollingInterval rollingInterval,
bool rollOnFileSizeLimit,
FileLifecycleHooks? hooks,
TimeSpan? retainedFileTimeLimit)
Expand Down
Loading