Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        /// <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));
        }
Esempio n. 3
0
        /// <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);
        }
Esempio n. 4
0
        /// <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();
        }
Esempio n. 5
0
 /// <summary>
 /// Infers the file extension from the given log file
 /// </summary>
 public void InferFileExtension(string path)
 {
     FileExtension = DirectoryCleaner.GetFileExtension(path);
 }