Skip to content

Commit 1b863ff

Browse files
authored
Remove explicit private modifier; name tweak; reorganize some conditionals
1 parent b213a7a commit 1b863ff

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/Serilog.Sinks.File/Sinks/File/RollingFileSink.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ void OpenFile(DateTime now, int? minSequence = null)
171171
throw;
172172
}
173173

174-
ApplyRetentionPolicy(path);
174+
ApplyRetentionPolicy(path, now);
175175
return;
176176
}
177177
}
178178

179-
void ApplyRetentionPolicy(string currentFilePath)
179+
void ApplyRetentionPolicy(string currentFilePath, DateTime now)
180180
{
181181
if (_retainedFileCountLimit == null && _retainedFileTimeLimit == null) return;
182182

@@ -195,7 +195,7 @@ void ApplyRetentionPolicy(string currentFilePath)
195195

196196
var toRemove = newestFirst
197197
.Where(n => StringComparer.OrdinalIgnoreCase.Compare(currentFileName, n.Filename) != 0)
198-
.SkipWhile(FilterFiles)
198+
.SkipWhile((f, i) => ShouldRetainFile(f, i, now))
199199
.Select(x => x.Filename)
200200
.ToList();
201201

@@ -213,15 +213,18 @@ void ApplyRetentionPolicy(string currentFilePath)
213213
}
214214
}
215215

216-
private bool FilterFiles(RollingLogFile file, int index)
216+
bool ShouldRetainFile(RollingLogFile file, int index, DateTime now)
217217
{
218-
var isInCountLimit = index < (_retainedFileCountLimit - 1 ?? 0);
218+
if (_retainedFileCountLimit.HasValue && index >= _retainedFileCountLimit.Value)
219+
return false;
219220

220-
var isInTimeLimit = !_retainedFileTimeLimit.HasValue;
221-
if (_retainedFileTimeLimit.HasValue && file.DateTime.HasValue)
222-
isInTimeLimit = DateTime.Now.Subtract(_retainedFileTimeLimit.Value).CompareTo(file.DateTime.Value) <= 0;
223-
224-
return isInCountLimit && isInTimeLimit;
221+
if (_retainedFileTimeLimit.HasValue && file.DateTime.HasValue &&
222+
file.DateTime.Value < now.Subtract(_retainedFileTimeLimit.Value))
223+
{
224+
return false;
225+
}
226+
227+
return true;
225228
}
226229

227230
public void Dispose()

0 commit comments

Comments
 (0)