Skip to content

More granular rolling intervals #205

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 5 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
27 changes: 26 additions & 1 deletion src/Serilog.Sinks.File/RollingInterval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public enum RollingInterval
/// </summary>
Month,

/// <summary>
/// Roll every calendar week. Filenames will have <code>yyyyMMdd</code> appended.
/// </summary>
Week,

/// <summary>
/// Roll every day. Filenames will have <code>yyyyMMdd</code> appended.
/// </summary>
Expand All @@ -47,6 +52,26 @@ public enum RollingInterval
/// <summary>
/// Roll every minute. Filenames will have <code>yyyyMMddHHmm</code> appended.
/// </summary>
Minute
Minute,

/// <summary>
/// Roll every five minute. Filenames will have <code>yyyyMMddHHmm</code> appended with minutes limited to multiples of five.
/// </summary>
FiveMinutes,

/// <summary>
/// Roll every ten minute. Filenames will have <code>yyyyMMddHHmm</code> appended with minutes limited to multiples of ten.
/// </summary>
TenMinutes,

/// <summary>
/// Roll every fifteen minute. Filenames will have <code>yyyyMMddHHmm</code> appended with minutes limited to multiples of fifteen.
/// </summary>
QuarterHour,

/// <summary>
/// Roll every half hour. Filenames will have <code>yyyyMMddHHmm</code> appended with minutes limited to multiples of third.
/// </summary>
HalfHour
}
}
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.File/Serilog.Sinks.File.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageTags>serilog;file</PackageTags>
<PackageIcon>images\icon.png</PackageIcon>
<PackageIconUrl>https://serilog.net/images/serilog-sink-nuget.png</PackageIconUrl>
<PackageProjectUrl>http://serilog.net</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageLicense>http://www.apache.org/licenses/LICENSE-2.0</PackageLicense>
<RepositoryUrl>https://github.com/serilog/serilog-sinks-file</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
Expand Down
28 changes: 27 additions & 1 deletion 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 Down Expand Up @@ -28,11 +28,17 @@ public static string GetFormat(this RollingInterval interval)
return "yyyy";
case RollingInterval.Month:
return "yyyyMM";
case RollingInterval.Week:
return "yyyyMMdd";
case RollingInterval.Day:
return "yyyyMMdd";
case RollingInterval.Hour:
return "yyyyMMddHH";
case RollingInterval.Minute:
case RollingInterval.FiveMinutes:
case RollingInterval.TenMinutes:
case RollingInterval.QuarterHour:
case RollingInterval.HalfHour:
return "yyyyMMddHHmm";
default:
throw new ArgumentException("Invalid rolling interval");
Expand All @@ -49,12 +55,22 @@ public static string GetFormat(this RollingInterval interval)
return new DateTime(instant.Year, 1, 1, 0, 0, 0, instant.Kind);
case RollingInterval.Month:
return new DateTime(instant.Year, instant.Month, 1, 0, 0, 0, instant.Kind);
case RollingInterval.Week:
return new DateTime(instant.Year, instant.Month, instant.Day, 0, 0, 0, instant.Kind);
case RollingInterval.Day:
return new DateTime(instant.Year, instant.Month, instant.Day, 0, 0, 0, instant.Kind);
case RollingInterval.Hour:
return new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, 0, 0, instant.Kind);
case RollingInterval.Minute:
return new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, instant.Minute, 0, instant.Kind);
case RollingInterval.FiveMinutes:
return new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, (int)(instant.Minute / 5) * 5, 0, instant.Kind);
case RollingInterval.TenMinutes:
return new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, (int)(instant.Minute / 10) * 10, 0, instant.Kind);
case RollingInterval.QuarterHour:
return new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, (int)(instant.Minute / 15) * 15, 0, instant.Kind);
case RollingInterval.HalfHour:
return new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, (int)(instant.Minute / 30) * 30, 0, instant.Kind);
default:
throw new ArgumentException("Invalid rolling interval");
}
Expand All @@ -72,12 +88,22 @@ public static string GetFormat(this RollingInterval interval)
return current.Value.AddYears(1);
case RollingInterval.Month:
return current.Value.AddMonths(1);
case RollingInterval.Week:
return current.Value.AddDays(7);
case RollingInterval.Day:
return current.Value.AddDays(1);
case RollingInterval.Hour:
return current.Value.AddHours(1);
case RollingInterval.Minute:
return current.Value.AddMinutes(1);
case RollingInterval.FiveMinutes:
return current.Value.AddMinutes(5);
case RollingInterval.TenMinutes:
return current.Value.AddMinutes(10);
case RollingInterval.QuarterHour:
return current.Value.AddMinutes(15);
case RollingInterval.HalfHour:
return current.Value.AddMinutes(30);
default:
throw new ArgumentException("Invalid rolling interval");
}
Expand Down
20 changes: 20 additions & 0 deletions test/Serilog.Sinks.File.Tests/RollingIntervalExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,30 @@ public class RollingIntervalExtensionsTests
new object[]{ RollingInterval.Year, new DateTime(2018, 06, 01), new DateTime(2018, 01, 01), new DateTime(2019, 01, 01) },
new object[]{ RollingInterval.Month, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2018, 02, 01) },
new object[]{ RollingInterval.Month, new DateTime(2018, 01, 14), new DateTime(2018, 01, 01), new DateTime(2018, 02, 01) },
new object[]{ RollingInterval.Week, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2018, 01, 08) },
new object[]{ RollingInterval.Week, new DateTime(2018, 01, 01, 12, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 08) },
new object[]{ RollingInterval.Day, new DateTime(2018, 01, 01), new DateTime(2018, 01, 01), new DateTime(2018, 01, 02) },
new object[]{ RollingInterval.Day, new DateTime(2018, 01, 01, 12, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 02) },
new object[]{ RollingInterval.Hour, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 1, 0, 0) },
new object[]{ RollingInterval.Hour, new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 1, 0, 0) },
new object[]{ RollingInterval.HalfHour, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 30, 0) },
new object[]{ RollingInterval.HalfHour, new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01, 1, 00, 0) },
new object[]{ RollingInterval.HalfHour, new DateTime(2018, 01, 01, 0, 45, 0), new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01, 1, 0, 0) },
new object[]{ RollingInterval.QuarterHour, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 15, 0) },
new object[]{ RollingInterval.QuarterHour, new DateTime(2018, 01, 01, 0, 10, 0), new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01, 0, 15, 0) },
new object[]{ RollingInterval.QuarterHour, new DateTime(2018, 01, 01, 0, 15, 0), new DateTime(2018, 01, 01, 0, 15, 0), new DateTime(2018, 01, 01, 0, 30, 0) },
new object[]{ RollingInterval.QuarterHour, new DateTime(2018, 01, 01, 0, 20, 0), new DateTime(2018, 01, 01, 0, 15, 0), new DateTime(2018, 01, 01, 0, 30, 0) },
new object[]{ RollingInterval.QuarterHour, new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01, 0, 45, 0) },
new object[]{ RollingInterval.QuarterHour, new DateTime(2018, 01, 01, 0, 45, 0), new DateTime(2018, 01, 01, 0, 45, 0), new DateTime(2018, 01, 01, 1, 0, 0) },
new object[]{ RollingInterval.TenMinutes, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 10, 0) },
new object[]{ RollingInterval.TenMinutes, new DateTime(2018, 01, 01, 0, 10, 0), new DateTime(2018, 01, 01, 0, 10, 0), new DateTime(2018, 01, 01, 0, 20, 0) },
new object[]{ RollingInterval.TenMinutes, new DateTime(2018, 01, 01, 0, 15, 0), new DateTime(2018, 01, 01, 0, 10, 0), new DateTime(2018, 01, 01, 0, 20, 0) },
new object[]{ RollingInterval.TenMinutes, new DateTime(2018, 01, 01, 0, 20, 0), new DateTime(2018, 01, 01, 0, 20, 0), new DateTime(2018, 01, 01, 0, 30, 0) },
new object[]{ RollingInterval.TenMinutes, new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01, 0, 30, 0), new DateTime(2018, 01, 01, 0, 40, 0) },
new object[]{ RollingInterval.TenMinutes, new DateTime(2018, 01, 01, 0, 45, 0), new DateTime(2018, 01, 01, 0, 40, 0), new DateTime(2018, 01, 01, 0, 50, 0) },
new object[]{ RollingInterval.FiveMinutes, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01, 0, 5, 0) },
new object[]{ RollingInterval.FiveMinutes, new DateTime(2018, 01, 01, 0, 4, 0), new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01, 0, 5, 0) },
new object[]{ RollingInterval.FiveMinutes, new DateTime(2018, 01, 01, 0, 6, 0), new DateTime(2018, 01, 01, 0, 5, 0), new DateTime(2018, 01, 01, 0, 10, 0) },
new object[]{ RollingInterval.Minute, new DateTime(2018, 01, 01, 0, 0, 0), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 1, 0) },
new object[]{ RollingInterval.Minute, new DateTime(2018, 01, 01, 0, 0, 30), new DateTime(2018, 01, 01), new DateTime(2018, 01, 01, 0, 1, 0) }
};
Expand Down
2 changes: 2 additions & 0 deletions test/Serilog.Sinks.File.Tests/Serilog.Sinks.File.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="System.Runtime" Version="4.3.0" />
<PackageReference Include="xunit.extensibility.core" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>
Expand Down