/// <summary> /// Emits a log event to this sink /// </summary> /// <param name="logEvent">The <see cref="LogEvent"/> to emit</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ObjectDisposedException"></exception> public void Emit(LogEvent logEvent) { if (logEvent == null) { throw new ArgumentNullException("logEvent"); } lock (this.syncRoot) { if (this.disposed) { throw new ObjectDisposedException(ThisObjectName, "The rolling file sink has been disposed"); } bool newDay = this.currentSink.LogFileDescription.LogFileInfo.Date.Date != DateTime.UtcNow.Date; if (this.currentSink.SizeLimitReached || newDay) { this.currentSink = NextSizeLimitedFileSink(); } if (this.currentSink != null) { this.currentSink.Emit(logEvent); } } }
/// <summary> /// Emits a log event to this sink /// </summary> /// <param name="logEvent">The <see cref="LogEvent"/> to emit</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ObjectDisposedException"></exception> public void Emit(LogEvent logEvent) { if (logEvent == null) { throw new ArgumentNullException("logEvent"); } lock (this.syncRoot) { if (this.disposed) { throw new ObjectDisposedException(ThisObjectName, "The rolling file sink has been disposed"); } if (this.currentSink.SizeLimitReached) { this.currentSink = NextSizeLimitedFileSink(); } if (this.currentSink != null) { this.currentSink.Emit(logEvent); } } }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or /// resetting unmanaged resources. /// </summary> public void Dispose() { lock (this.syncRoot) { if (!this.disposed && this.currentSink != null) { this.currentSink.Dispose(); this.currentSink = null; this.disposed = true; } } }
/// <summary> /// Construct a <see cref="AlternateRollingFileSink"/> /// </summary> /// <param name="logDirectory"></param> /// <param name="formatter"></param> /// <param name="fileSizeLimitBytes"> /// The size in bytes at which a new file should be created</param> /// <param name="encoding"></param> public AlternateRollingFileSink( string logDirectory, ITextFormatter formatter, long fileSizeLimitBytes, Encoding encoding = null) { this.formatter = formatter; this.fileSizeLimitBytes = fileSizeLimitBytes; this.encoding = encoding; this.logDirectory = logDirectory; this.currentSink = GetLatestSink(); }
/// <summary> /// Construct a <see cref="AlternateRollingFileSink"/> /// </summary> /// <param name="logDirectory"></param> /// <param name="formatter"></param> /// <param name="fileSizeLimitBytes"> /// The size in bytes at which a new file should be created</param> /// <param name="encoding"></param> /// <param name="logFilePrefix">The prefix for the log file name.</param> public AlternateRollingFileSink( string logDirectory, ITextFormatter formatter, long fileSizeLimitBytes, int?retainedFileCountLimit = null, Encoding encoding = null, string logFilePrefix = "") { this.formatter = formatter; this.fileSizeLimitBytes = fileSizeLimitBytes; this.retainedFileCountLimit = retainedFileCountLimit; this.encoding = encoding; this.logDirectory = logDirectory; this.logFilePrefix = string.IsNullOrEmpty(logFilePrefix) ? logFilePrefix : $"{logFilePrefix}-"; this.currentSink = GetLatestSink(); }