Skip to content

Commit 77e52aa

Browse files
author
stypulaw
committed
Added ability to roll on specific day of week as described by serilog#325
1 parent 5e3ff41 commit 77e52aa

File tree

3 files changed

+104
-17
lines changed

3 files changed

+104
-17
lines changed

src/Serilog.Sinks.File/RollingInterval.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 Serilog Contributors
1+
// Copyright 2017 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -47,5 +47,40 @@ public enum RollingInterval
4747
/// <summary>
4848
/// Roll every minute. Filenames will have <code>yyyyMMddHHmm</code> appended.
4949
/// </summary>
50-
Minute
50+
Minute,
51+
52+
/// <summary>
53+
/// Roll every Sunday. Filenames will have <code>yyyyMMdd</code> appended.
54+
/// </summary>
55+
Sunday,
56+
57+
/// <summary>
58+
/// Roll every Monday. Filenames will have <code>yyyyMMdd</code> appended.
59+
/// </summary>
60+
Monday,
61+
62+
/// <summary>
63+
/// Roll every Tuesday. Filenames will have <code>yyyyMMdd</code> appended.
64+
/// </summary>
65+
Tuesday,
66+
67+
/// <summary>
68+
/// Roll every Wednesday. Filenames will have <code>yyyyMMdd</code> appended.
69+
/// </summary>
70+
Wednesday,
71+
72+
/// <summary>
73+
/// Roll every Thursday. Filenames will have <code>yyyyMMdd</code> appended.
74+
/// </summary>
75+
Thursday,
76+
77+
/// <summary>
78+
/// Roll every Friday. Filenames will have <code>yyyyMMdd</code> appended.
79+
/// </summary>
80+
Friday,
81+
82+
/// <summary>
83+
/// Roll every Saturday. Filenames will have <code>yyyyMMdd</code> appended.
84+
/// </summary>
85+
Saturday
5186
}

src/Serilog.Sinks.File/Sinks/File/RollingIntervalExtensions.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 Serilog Contributors
1+
// Copyright 2017 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414

1515
namespace Serilog.Sinks.File;
1616

17-
static class RollingIntervalExtensions
17+
internal static class RollingIntervalExtensions
1818
{
1919
public static string GetFormat(this RollingInterval interval)
2020
{
@@ -23,7 +23,14 @@ public static string GetFormat(this RollingInterval interval)
2323
RollingInterval.Infinite => "",
2424
RollingInterval.Year => "yyyy",
2525
RollingInterval.Month => "yyyyMM",
26-
RollingInterval.Day => "yyyyMMdd",
26+
RollingInterval.Day or
27+
RollingInterval.Sunday or
28+
RollingInterval.Monday or
29+
RollingInterval.Tuesday or
30+
RollingInterval.Wednesday or
31+
RollingInterval.Thursday or
32+
RollingInterval.Friday or
33+
RollingInterval.Saturday => "yyyyMMdd",
2734
RollingInterval.Hour => "yyyyMMddHH",
2835
RollingInterval.Minute => "yyyyMMddHHmm",
2936
_ => throw new ArgumentException("Invalid rolling interval.")
@@ -37,6 +44,13 @@ public static string GetFormat(this RollingInterval interval)
3744
RollingInterval.Infinite => null,
3845
RollingInterval.Year => new DateTime(instant.Year, 1, 1, 0, 0, 0, instant.Kind),
3946
RollingInterval.Month => new DateTime(instant.Year, instant.Month, 1, 0, 0, 0, instant.Kind),
47+
RollingInterval.Sunday => GetDateForRollOnDay(instant, DayOfWeek.Sunday),
48+
RollingInterval.Monday => GetDateForRollOnDay(instant, DayOfWeek.Monday),
49+
RollingInterval.Tuesday => GetDateForRollOnDay(instant, DayOfWeek.Tuesday),
50+
RollingInterval.Wednesday => GetDateForRollOnDay(instant, DayOfWeek.Wednesday),
51+
RollingInterval.Thursday => GetDateForRollOnDay(instant, DayOfWeek.Thursday),
52+
RollingInterval.Friday => GetDateForRollOnDay(instant, DayOfWeek.Friday),
53+
RollingInterval.Saturday => GetDateForRollOnDay(instant, DayOfWeek.Saturday),
4054
RollingInterval.Day => new DateTime(instant.Year, instant.Month, instant.Day, 0, 0, 0, instant.Kind),
4155
RollingInterval.Hour => new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, 0, 0, instant.Kind),
4256
RollingInterval.Minute => new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, instant.Minute, 0, instant.Kind),
@@ -48,16 +62,40 @@ public static string GetFormat(this RollingInterval interval)
4862
{
4963
var current = GetCurrentCheckpoint(interval, instant);
5064
if (current == null)
65+
{
5166
return null;
67+
}
5268

5369
return interval switch
5470
{
5571
RollingInterval.Year => current.Value.AddYears(1),
5672
RollingInterval.Month => current.Value.AddMonths(1),
73+
RollingInterval.Sunday or
74+
RollingInterval.Monday or
75+
RollingInterval.Tuesday or
76+
RollingInterval.Wednesday or
77+
RollingInterval.Thursday or
78+
RollingInterval.Friday or
79+
RollingInterval.Saturday => current.Value.AddDays(7),
5780
RollingInterval.Day => current.Value.AddDays(1),
5881
RollingInterval.Hour => current.Value.AddHours(1),
5982
RollingInterval.Minute => current.Value.AddMinutes(1),
6083
_ => throw new ArgumentException("Invalid rolling interval.")
6184
};
6285
}
86+
87+
private static DateTime? GetDateForRollOnDay(DateTime instant, DayOfWeek rollOnDayOfWeek)
88+
{
89+
int delta = rollOnDayOfWeek - instant.DayOfWeek;
90+
91+
if (delta > 0)
92+
{
93+
// Adjust to get the previous roll date for DayOfWeek when the result is positive
94+
delta -= 7;
95+
}
96+
97+
var date = instant.Date.AddDays(delta);
98+
99+
return date;
100+
}
63101
}

test/Serilog.Sinks.File.Tests/RollingIntervalExtensionsTests.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
using Xunit;
1+
using Xunit;
22

33
namespace Serilog.Sinks.File.Tests;
44

55
public class RollingIntervalExtensionsTests
66
{
77
public static object?[][] IntervalInstantCurrentNextCheckpoint =>
88
[
9-
[RollingInterval.Infinite, new DateTime(2018, 01, 01), null, null],
10-
[RollingInterval.Year, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2019, 01, 01)],
11-
[RollingInterval.Year, new DateTime(2018, 06, 01), new DateTime(2018, 01, 01), new DateTime(2019, 01, 01)],
12-
[RollingInterval.Month, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2018, 02, 01)],
13-
[RollingInterval.Month, new DateTime(2018, 01, 14), new DateTime(2018, 01, 01), new DateTime(2018, 02, 01)],
14-
[RollingInterval.Day, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2018, 01, 02)],
15-
[RollingInterval.Day, new DateTime(2018, 01, 01, 12, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 02)],
16-
[RollingInterval.Hour, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 1, 0, 0)],
17-
[RollingInterval.Hour, new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 1, 0, 0)],
18-
[RollingInterval.Minute, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 1, 0)],
19-
[RollingInterval.Minute, new DateTime(2018, 01, 01, 0, 0, 30), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 1, 0)]
9+
[RollingInterval.Infinite, new DateTime(2018, 01, 01), null, null],
10+
[RollingInterval.Year, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2019, 01, 01)],
11+
[RollingInterval.Year, new DateTime(2018, 06, 01), new DateTime(2018, 01, 01), new DateTime(2019, 01, 01)],
12+
[RollingInterval.Month, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2018, 02, 01)],
13+
[RollingInterval.Month, new DateTime(2018, 01, 14), new DateTime(2018, 01, 01), new DateTime(2018, 02, 01)],
14+
[RollingInterval.Day, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2018, 01, 02)],
15+
[RollingInterval.Day, new DateTime(2018, 01, 01, 12, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 02)],
16+
[RollingInterval.Sunday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 06), new DateTime(2024, 10, 13)],
17+
[RollingInterval.Sunday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 06), new DateTime(2024, 10, 13)],
18+
[RollingInterval.Monday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 07), new DateTime(2024, 10, 14)],
19+
[RollingInterval.Monday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 07), new DateTime(2024, 10, 14)],
20+
[RollingInterval.Tuesday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 08), new DateTime(2024, 10, 15)],
21+
[RollingInterval.Tuesday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 08), new DateTime(2024, 10, 15)],
22+
[RollingInterval.Wednesday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 02), new DateTime(2024, 10, 09)],
23+
[RollingInterval.Wednesday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 09), new DateTime(2024, 10, 16)],
24+
[RollingInterval.Thursday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 03), new DateTime(2024, 10, 10)],
25+
[RollingInterval.Thursday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 03), new DateTime(2024, 10, 10)],
26+
[RollingInterval.Friday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 04), new DateTime(2024, 10, 11)],
27+
[RollingInterval.Friday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 04), new DateTime(2024, 10, 11)],
28+
[RollingInterval.Saturday, new DateTime(2024, 10, 08), new DateTime(2024, 10, 05), new DateTime(2024, 10, 12)],
29+
[RollingInterval.Saturday, new DateTime(2024, 10, 09, 12, 0, 0), new DateTime(2024, 10, 05), new DateTime(2024, 10, 12)],
30+
[RollingInterval.Hour, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 1, 0, 0)],
31+
[RollingInterval.Hour, new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 1, 0, 0)],
32+
[RollingInterval.Minute, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 1, 0)],
33+
[RollingInterval.Minute, new DateTime(2018, 01, 01, 0, 0, 30), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 1, 0)]
2034
];
2135

2236
[Theory]

0 commit comments

Comments
 (0)