/// <summary> /// Constructs the logger using the given configuration. /// </summary> /// <param name="useUtcTime">True to use UTC time rather than local time</param> /// <param name="deleteOldFiles"> /// If other than null it sets to delete any file in the log folder that is older than the specified time /// </param> /// <param name="dateTimeFormat">Format string to use when calling DateTime.Format</param> /// <param name="directory"> /// Directory where to create the log files, null to use a local "logs" directory /// </param> /// <param name="enabledLabels"> /// Labels enabled to be logged by the library, an attempt to log with a label that is not enabled is ignored /// (no error is raised), null or empty enables all labels /// </param> public L( bool useUtcTime = false, TimeSpan?deleteOldFiles = null, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", string directory = null, params string[] enabledLabels) { _useUtcTime = useUtcTime; _deleteOldFiles = deleteOldFiles; _dateTimeFormat = dateTimeFormat; _directory = directory ?? Path.Combine(AppContext.BaseDirectory, "logs"); _enabledLabels = (enabledLabels ?? new string[0]).Select(Normalize).ToArray(); _lock = new object(); _openStreams = new OpenStreams(_directory); if (_deleteOldFiles.HasValue) { var min = TimeSpan.FromSeconds(5); var max = TimeSpan.FromHours(8); var cleanUpTime = new TimeSpan(_deleteOldFiles.Value.Ticks / 5); if (cleanUpTime < min) { cleanUpTime = min; } if (cleanUpTime > max) { cleanUpTime = max; } _cleaner = new FolderCleaner(_directory, _openStreams, _deleteOldFiles.Value, cleanUpTime); } _longestLabel = _enabledLabels.Any() ? _enabledLabels.Select(l => l.Length).Max() : 5; _disposed = false; }
internal FolderCleaner(string path, OpenStreams streams, TimeSpan threshold, TimeSpan interval) { _directory = path; _openStreams = streams; _threshold = threshold; _cleanLock = new object(); _timer = new Timer(Clean, null, TimeSpan.Zero, interval); }
/// <summary> /// Constructs the logger using the given configuration. /// </summary> /// <param name="configuration">Configuration to use</param> public L(LConfiguration configuration) { _configuration = configuration; if (string.IsNullOrEmpty(_configuration.Directory)) { _configuration.Directory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs"); } _openStreams = new OpenStreams(_configuration.Directory); if (_configuration.DeleteOldFiles.HasValue) { var min = TimeSpan.FromSeconds(5); var max = TimeSpan.FromHours(8); var cleanUpTime = new TimeSpan(_configuration.DeleteOldFiles.Value.Ticks / 5); if (cleanUpTime < min) { cleanUpTime = min; } if (cleanUpTime > max) { cleanUpTime = max; } _cleaner = new FolderCleaner( _configuration.Directory, _openStreams, _configuration.DeleteOldFiles.Value, cleanUpTime); } if (string.IsNullOrEmpty(_configuration.DateTimeFormat)) { _configuration.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; } _configuration.EnabledLabels = (_configuration.EnabledLabels ?? new string[0]).Select(l => _sanitizeLabel(l)).ToArray(); _lock = new object(); _longestLabel = 5; _disposed = false; }