@@ -171,12 +171,12 @@ void OpenFile(DateTime now, int? minSequence = null)
171
171
throw ;
172
172
}
173
173
174
- ApplyRetentionPolicy ( path ) ;
174
+ ApplyRetentionPolicy ( path , now ) ;
175
175
return ;
176
176
}
177
177
}
178
178
179
- void ApplyRetentionPolicy ( string currentFilePath )
179
+ void ApplyRetentionPolicy ( string currentFilePath , DateTime now )
180
180
{
181
181
if ( _retainedFileCountLimit == null && _retainedFileTimeLimit == null ) return ;
182
182
@@ -195,7 +195,7 @@ void ApplyRetentionPolicy(string currentFilePath)
195
195
196
196
var toRemove = newestFirst
197
197
. Where ( n => StringComparer . OrdinalIgnoreCase . Compare ( currentFileName , n . Filename ) != 0 )
198
- . SkipWhile ( FilterFiles )
198
+ . SkipWhile ( ( f , i ) => ShouldRetainFile ( f , i , now ) )
199
199
. Select ( x => x . Filename )
200
200
. ToList ( ) ;
201
201
@@ -213,15 +213,18 @@ void ApplyRetentionPolicy(string currentFilePath)
213
213
}
214
214
}
215
215
216
- private bool FilterFiles ( RollingLogFile file , int index )
216
+ bool ShouldRetainFile ( RollingLogFile file , int index , DateTime now )
217
217
{
218
- var isInCountLimit = index < ( _retainedFileCountLimit - 1 ?? 0 ) ;
218
+ if ( _retainedFileCountLimit . HasValue && index >= _retainedFileCountLimit . Value )
219
+ return false ;
219
220
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 ;
225
228
}
226
229
227
230
public void Dispose ( )
0 commit comments