From 77e52aa46991913a32684c3bea6cff0e816cf54c Mon Sep 17 00:00:00 2001 From: stypulaw Date: Tue, 8 Oct 2024 13:45:31 -0400 Subject: [PATCH] Added ability to roll on specific day of week as described by #325 --- src/Serilog.Sinks.File/RollingInterval.cs | 39 +++++++++++++++- .../Sinks/File/RollingIntervalExtensions.cs | 44 +++++++++++++++++-- .../RollingIntervalExtensionsTests.cs | 38 +++++++++++----- 3 files changed, 104 insertions(+), 17 deletions(-) diff --git a/src/Serilog.Sinks.File/RollingInterval.cs b/src/Serilog.Sinks.File/RollingInterval.cs index 55582ec..62fe699 100644 --- a/src/Serilog.Sinks.File/RollingInterval.cs +++ b/src/Serilog.Sinks.File/RollingInterval.cs @@ -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. @@ -47,5 +47,40 @@ public enum RollingInterval /// /// Roll every minute. Filenames will have yyyyMMddHHmm appended. /// - Minute + Minute, + + /// + /// Roll every Sunday. Filenames will have yyyyMMdd appended. + /// + Sunday, + + /// + /// Roll every Monday. Filenames will have yyyyMMdd appended. + /// + Monday, + + /// + /// Roll every Tuesday. Filenames will have yyyyMMdd appended. + /// + Tuesday, + + /// + /// Roll every Wednesday. Filenames will have yyyyMMdd appended. + /// + Wednesday, + + /// + /// Roll every Thursday. Filenames will have yyyyMMdd appended. + /// + Thursday, + + /// + /// Roll every Friday. Filenames will have yyyyMMdd appended. + /// + Friday, + + /// + /// Roll every Saturday. Filenames will have yyyyMMdd appended. + /// + Saturday } diff --git a/src/Serilog.Sinks.File/Sinks/File/RollingIntervalExtensions.cs b/src/Serilog.Sinks.File/Sinks/File/RollingIntervalExtensions.cs index a469abe..c29db9d 100644 --- a/src/Serilog.Sinks.File/Sinks/File/RollingIntervalExtensions.cs +++ b/src/Serilog.Sinks.File/Sinks/File/RollingIntervalExtensions.cs @@ -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. @@ -14,7 +14,7 @@ namespace Serilog.Sinks.File; -static class RollingIntervalExtensions +internal static class RollingIntervalExtensions { public static string GetFormat(this RollingInterval interval) { @@ -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.") @@ -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), @@ -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; + } } diff --git a/test/Serilog.Sinks.File.Tests/RollingIntervalExtensionsTests.cs b/test/Serilog.Sinks.File.Tests/RollingIntervalExtensionsTests.cs index 7572067..be0ea6a 100644 --- a/test/Serilog.Sinks.File.Tests/RollingIntervalExtensionsTests.cs +++ b/test/Serilog.Sinks.File.Tests/RollingIntervalExtensionsTests.cs @@ -1,4 +1,4 @@ -using Xunit; +using Xunit; namespace Serilog.Sinks.File.Tests; @@ -6,17 +6,31 @@ 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]