private readonly ILogCleaner _self; // for unit testing private calls by the instance to itself /// <summary> /// </summary> public LogCleaner() { _self = this; _dateTimeProvider = new UniversalDateTime(); DirectoryCleaner = new DirectoryCleaner(new FileSystemOperations(new FileSystem()), _dateTimeProvider); TaskRunner = new TaskRunner(); }
/// <summary> /// call this to explicitly trigger a cleaning of the log directory /// </summary> public Task Cleanup() { var now = _dateTimeProvider.Now; var cutoffDate = MaxFileAgeDays.HasValue ? (DateTime?)now.AddDays(-MaxFileAgeDays.Value) : null; return(TaskRunner.Run(() => DirectoryCleaner.Clean(BasePath, FileExtension, cutoffDate, MaxDirectorySize), WaitType)); }
/// <summary> /// true if the next logging call will trigger a cleaning of the log directory /// </summary> public bool IsDueForCleaning(DateTime now) { if (!LastCleaning.HasValue) { LastCleaning = DirectoryCleaner.GetLastCleaningTime(BasePath) ?? DateTime.MinValue; } return((now - LastCleaning.Value).TotalMinutes >= PeriodMinutes); }
/// <summary> /// meant for internal use only /// </summary> public void TryCleanup() { if (!(MaxFileAgeDays.HasValue || MaxDirectorySize.HasValue)) { return; } if (string.IsNullOrWhiteSpace(FileExtension)) { return; } var now = _dateTimeProvider.Now; if (!_self.IsDueForCleaning(now)) { return; } LastCleaning = DirectoryCleaner.UpdateLastCleaningTime(BasePath); _self.Cleanup(); }
/// <summary> /// Infers the file extension from the given log file /// </summary> public void InferFileExtension(string path) { FileExtension = DirectoryCleaner.GetFileExtension(path); }