-
Notifications
You must be signed in to change notification settings - Fork 33
Add option to retain files based on age #17
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b2441f5
Implemented file age retention policy
Wedvich de27e77
Updated readme with new configuration parameter
Wedvich bad6950
Added validation of age limit
Wedvich e2c2535
Adjusted namespace for age retention policy
Wedvich 5ff9d1e
Merged with upstream branch
Wedvich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/Serilog.Sinks.RollingFile/Sinks/RollingFile/RetentionPolicies/FileAgeRetentionPolicy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2013-2016 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using Serilog.Debugging; | ||
|
||
namespace Serilog.Sinks.RollingFile.RetentionPolicies | ||
{ | ||
internal class FileAgeRetentionPolicy : IRetentionPolicy | ||
{ | ||
private readonly TemplatedPathRoller _roller; | ||
private readonly TimeSpan _retainedFileAgeLimit; | ||
|
||
public FileAgeRetentionPolicy(TemplatedPathRoller roller, TimeSpan retainedFileAgeLimit) | ||
{ | ||
if (roller == null) | ||
throw new ArgumentNullException(nameof(roller)); | ||
|
||
if (retainedFileAgeLimit <= TimeSpan.Zero) | ||
throw new ArgumentException("Zero or negative value provided; retained file age limit must be a positive timespan"); | ||
|
||
_roller = roller; | ||
_retainedFileAgeLimit = retainedFileAgeLimit; | ||
} | ||
|
||
public void Apply(string currentFilePath) | ||
{ | ||
var currentFileName = Path.GetFileName(currentFilePath); | ||
|
||
// We consider the current file to exist, even if nothing's been written yet, | ||
// because files are only opened on response to an event being processed. | ||
var potentialMatches = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern) | ||
.Select(Path.GetFileName) | ||
.Union(new[] { currentFileName }); | ||
|
||
var newestFirst = _roller | ||
.SelectMatches(potentialMatches) | ||
.OrderByDescending(m => m.Date) | ||
.ThenByDescending(m => m.SequenceNumber) | ||
.Select(m => new { m.Filename, m.Date }); | ||
|
||
var maxAge = DateTimeOffset.Now - _retainedFileAgeLimit; | ||
|
||
var toRemove = newestFirst | ||
.Where(f => StringComparer.OrdinalIgnoreCase.Compare(currentFileName, f.Filename) != 0 | ||
&& f.Date < maxAge) | ||
.Select(f => f.Filename) | ||
.ToList(); | ||
|
||
foreach (var obsolete in toRemove) | ||
{ | ||
var fullPath = Path.Combine(_roller.LogFileDirectory, obsolete); | ||
try | ||
{ | ||
System.IO.File.Delete(fullPath); | ||
} | ||
catch (Exception ex) | ||
{ | ||
SelfLog.WriteLine("Error {0} while removing obsolete file {1}", ex, fullPath); | ||
} | ||
} | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...Serilog.Sinks.RollingFile/Sinks/RollingFile/RetentionPolicies/FileCountRetentionPolicy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2013-2016 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Serilog.Debugging; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace Serilog.Sinks.RollingFile.RetentionPolicies | ||
{ | ||
internal class FileCountRetentionPolicy : IRetentionPolicy | ||
{ | ||
private readonly TemplatedPathRoller _roller; | ||
private readonly int? _retainedFileCountLimit; | ||
|
||
public FileCountRetentionPolicy(TemplatedPathRoller roller, int? retainedFileCountLimit) | ||
{ | ||
if (roller == null) | ||
throw new ArgumentNullException(nameof(roller)); | ||
|
||
if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) | ||
throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1"); | ||
|
||
_roller = roller; | ||
_retainedFileCountLimit = retainedFileCountLimit; | ||
} | ||
|
||
public void Apply(string currentFilePath) | ||
{ | ||
if (_retainedFileCountLimit == null) return; | ||
|
||
var currentFileName = Path.GetFileName(currentFilePath); | ||
|
||
// We consider the current file to exist, even if nothing's been written yet, | ||
// because files are only opened on response to an event being processed. | ||
var potentialMatches = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern) | ||
.Select(Path.GetFileName) | ||
.Union(new[] { currentFileName }); | ||
|
||
var newestFirst = _roller | ||
.SelectMatches(potentialMatches) | ||
.OrderByDescending(m => m.Date) | ||
.ThenByDescending(m => m.SequenceNumber) | ||
.Select(m => m.Filename); | ||
|
||
var toRemove = newestFirst | ||
.Where(n => StringComparer.OrdinalIgnoreCase.Compare(currentFileName, n) != 0) | ||
.Skip(_retainedFileCountLimit.Value - 1) | ||
.ToList(); | ||
|
||
foreach (var obsolete in toRemove) | ||
{ | ||
var fullPath = Path.Combine(_roller.LogFileDirectory, obsolete); | ||
try | ||
{ | ||
System.IO.File.Delete(fullPath); | ||
} | ||
catch (Exception ex) | ||
{ | ||
SelfLog.WriteLine("Error {0} while removing obsolete file {1}", ex, fullPath); | ||
} | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Serilog.Sinks.RollingFile/Sinks/RollingFile/RetentionPolicies/IRetentionPolicy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2013-2016 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace Serilog.Sinks.RollingFile.RetentionPolicies | ||
{ | ||
internal interface IRetentionPolicy | ||
{ | ||
/// <summary> | ||
/// Applies the retention policy to the current file path. | ||
/// </summary> | ||
/// <param name="currentFilePath">Path to the file the policy will be applied to.</param> | ||
void Apply(string currentFilePath); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The age limit is currently added as an optional parameter at the end of the
RollingFileSink
constructor and the corresponding extension methods, to avoid breaking existing code (though it does break binary compatibility).Maybe a more elegant alternative could be to add an overloaded constructor that takes in one or more
IRetentionPolicy
parameters instead ofretainedFileCountLimit
andretainedFileAgeLimit
, and matching extension methods? I'm not sure if this would play nicely with the XML/JSON configuration wiring though. Would that require changes to the Serilog configuration infrastructure? Is it possible to inject that behavior somehow instead?