Skip to content

Added ability to roll on specific day of week as described by #325 #326

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
39 changes: 37 additions & 2 deletions src/Serilog.Sinks.File/RollingInterval.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Serilog Contributors
// Copyright 2017 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,5 +47,40 @@ public enum RollingInterval
/// <summary>
/// Roll every minute. Filenames will have <code>yyyyMMddHHmm</code> appended.
/// </summary>
Minute
Minute,

/// <summary>
/// Roll every Sunday. Filenames will have <code>yyyyMMdd</code> appended.
/// </summary>
Sunday,

/// <summary>
/// Roll every Monday. Filenames will have <code>yyyyMMdd</code> appended.
/// </summary>
Monday,

/// <summary>
/// Roll every Tuesday. Filenames will have <code>yyyyMMdd</code> appended.
/// </summary>
Tuesday,

/// <summary>
/// Roll every Wednesday. Filenames will have <code>yyyyMMdd</code> appended.
/// </summary>
Wednesday,

/// <summary>
/// Roll every Thursday. Filenames will have <code>yyyyMMdd</code> appended.
/// </summary>
Thursday,

/// <summary>
/// Roll every Friday. Filenames will have <code>yyyyMMdd</code> appended.
/// </summary>
Friday,

/// <summary>
/// Roll every Saturday. Filenames will have <code>yyyyMMdd</code> appended.
/// </summary>
Saturday
}
44 changes: 41 additions & 3 deletions src/Serilog.Sinks.File/Sinks/File/RollingIntervalExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Serilog Contributors
// Copyright 2017 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@

namespace Serilog.Sinks.File;

static class RollingIntervalExtensions
internal static class RollingIntervalExtensions
{
public static string GetFormat(this RollingInterval interval)
{
Expand All @@ -23,7 +23,14 @@ public static string GetFormat(this RollingInterval interval)
RollingInterval.Infinite => "",
RollingInterval.Year => "yyyy",
RollingInterval.Month => "yyyyMM",
RollingInterval.Day => "yyyyMMdd",
RollingInterval.Day or
RollingInterval.Sunday or
RollingInterval.Monday or
RollingInterval.Tuesday or
RollingInterval.Wednesday or
RollingInterval.Thursday or
RollingInterval.Friday or
RollingInterval.Saturday => "yyyyMMdd",
RollingInterval.Hour => "yyyyMMddHH",
RollingInterval.Minute => "yyyyMMddHHmm",
_ => throw new ArgumentException("Invalid rolling interval.")
Expand All @@ -37,6 +44,13 @@ public static string GetFormat(this RollingInterval interval)
RollingInterval.Infinite => null,
RollingInterval.Year => new DateTime(instant.Year, 1, 1, 0, 0, 0, instant.Kind),
RollingInterval.Month => new DateTime(instant.Year, instant.Month, 1, 0, 0, 0, instant.Kind),
RollingInterval.Sunday => GetDateForRollOnDay(instant, DayOfWeek.Sunday),
RollingInterval.Monday => GetDateForRollOnDay(instant, DayOfWeek.Monday),
RollingInterval.Tuesday => GetDateForRollOnDay(instant, DayOfWeek.Tuesday),
RollingInterval.Wednesday => GetDateForRollOnDay(instant, DayOfWeek.Wednesday),
RollingInterval.Thursday => GetDateForRollOnDay(instant, DayOfWeek.Thursday),
RollingInterval.Friday => GetDateForRollOnDay(instant, DayOfWeek.Friday),
RollingInterval.Saturday => GetDateForRollOnDay(instant, DayOfWeek.Saturday),
RollingInterval.Day => new DateTime(instant.Year, instant.Month, instant.Day, 0, 0, 0, instant.Kind),
RollingInterval.Hour => new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, 0, 0, instant.Kind),
RollingInterval.Minute => new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, instant.Minute, 0, instant.Kind),
Expand All @@ -48,16 +62,40 @@ public static string GetFormat(this RollingInterval interval)
{
var current = GetCurrentCheckpoint(interval, instant);
if (current == null)
{
return null;
}

return interval switch
{
RollingInterval.Year => current.Value.AddYears(1),
RollingInterval.Month => current.Value.AddMonths(1),
RollingInterval.Sunday or
RollingInterval.Monday or
RollingInterval.Tuesday or
RollingInterval.Wednesday or
RollingInterval.Thursday or
RollingInterval.Friday or
RollingInterval.Saturday => current.Value.AddDays(7),
RollingInterval.Day => current.Value.AddDays(1),
RollingInterval.Hour => current.Value.AddHours(1),
RollingInterval.Minute => current.Value.AddMinutes(1),
_ => throw new ArgumentException("Invalid rolling interval.")
};
}

private static DateTime? GetDateForRollOnDay(DateTime instant, DayOfWeek rollOnDayOfWeek)
{
int delta = rollOnDayOfWeek - instant.DayOfWeek;

if (delta > 0)
{
// Adjust to get the previous roll date for DayOfWeek when the result is positive
delta -= 7;
}

var date = instant.Date.AddDays(delta);

return date;
}
}
38 changes: 26 additions & 12 deletions test/Serilog.Sinks.File.Tests/RollingIntervalExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
using Xunit;
using Xunit;

namespace Serilog.Sinks.File.Tests;

public class RollingIntervalExtensionsTests
{
public static object?[][] IntervalInstantCurrentNextCheckpoint =>
[
[RollingInterval.Infinite, new DateTime(2018, 01, 01), null, null],
[RollingInterval.Year, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2019, 01, 01)],
[RollingInterval.Year, new DateTime(2018, 06, 01), new DateTime(2018, 01, 01), new DateTime(2019, 01, 01)],
[RollingInterval.Month, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2018, 02, 01)],
[RollingInterval.Month, new DateTime(2018, 01, 14), new DateTime(2018, 01, 01), new DateTime(2018, 02, 01)],
[RollingInterval.Day, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2018, 01, 02)],
[RollingInterval.Day, new DateTime(2018, 01, 01, 12, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 02)],
[RollingInterval.Hour, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 1, 0, 0)],
[RollingInterval.Hour, new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 1, 0, 0)],
[RollingInterval.Minute, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 1, 0)],
[RollingInterval.Minute, new DateTime(2018, 01, 01, 0, 0, 30), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 1, 0)]
[RollingInterval.Infinite, new DateTime(2018, 01, 01), null, null],
[RollingInterval.Year, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2019, 01, 01)],
[RollingInterval.Year, new DateTime(2018, 06, 01), new DateTime(2018, 01, 01), new DateTime(2019, 01, 01)],
[RollingInterval.Month, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2018, 02, 01)],
[RollingInterval.Month, new DateTime(2018, 01, 14), new DateTime(2018, 01, 01), new DateTime(2018, 02, 01)],
[RollingInterval.Day, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2018, 01, 02)],
[RollingInterval.Day, new DateTime(2018, 01, 01, 12, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 02)],
[RollingInterval.Sunday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 06), new DateTime(2024, 10, 13)],
[RollingInterval.Sunday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 06), new DateTime(2024, 10, 13)],
[RollingInterval.Monday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 07), new DateTime(2024, 10, 14)],
[RollingInterval.Monday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 07), new DateTime(2024, 10, 14)],
[RollingInterval.Tuesday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 08), new DateTime(2024, 10, 15)],
[RollingInterval.Tuesday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 08), new DateTime(2024, 10, 15)],
[RollingInterval.Wednesday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 02), new DateTime(2024, 10, 09)],
[RollingInterval.Wednesday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 09), new DateTime(2024, 10, 16)],
[RollingInterval.Thursday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 03), new DateTime(2024, 10, 10)],
[RollingInterval.Thursday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 03), new DateTime(2024, 10, 10)],
[RollingInterval.Friday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 04), new DateTime(2024, 10, 11)],
[RollingInterval.Friday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 04), new DateTime(2024, 10, 11)],
[RollingInterval.Saturday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 05), new DateTime(2024, 10, 12)],
[RollingInterval.Saturday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 05), new DateTime(2024, 10, 12)],
[RollingInterval.Hour, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 1, 0, 0)],
[RollingInterval.Hour, new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 1, 0, 0)],
[RollingInterval.Minute, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 1, 0)],
[RollingInterval.Minute, new DateTime(2018, 01, 01, 0, 0, 30), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 1, 0)]
];

[Theory]
Expand Down