public void WhenLimitIsSpecifiedFileSizeIsRestricted() { const int maxBytes = 5000; const int eventsToLimit = 10; using (var tmp = TempFolder.ForCaller()) { var path = tmp.AllocateFilename("txt"); var evt = Some.LogEvent(new string('n', maxBytes / eventsToLimit)); using (var sink = new SharedFileSink(path, new JsonFormatter(), maxBytes)) { for (var i = 0; i < eventsToLimit * 2; i++) { sink.Emit(evt); } } var size = new FileInfo(path).Length; Assert.True(size > maxBytes); Assert.True(size < maxBytes * 2); } }
static LoggerConfiguration ConfigureFile( this Func <ILogEventSink, LogEventLevel, LoggingLevelSwitch, LoggerConfiguration> addSink, ITextFormatter formatter, string path, LogEventLevel restrictedToMinimumLevel, long?fileSizeLimitBytes, LoggingLevelSwitch levelSwitch, bool buffered, bool propagateExceptions, bool shared, TimeSpan?flushToDiskInterval, Encoding encoding, PersistentFileRollingInterval persistentFileRollingInterval, bool rollOnFileSizeLimit, int?retainedFileCountLimit, FileLifecycleHooks hooks, bool preserveLogFilename = true, bool rollOnEachProcessRun = true, bool useLastWriteAsTimestamp = false) { if (addSink == null) { throw new ArgumentNullException(nameof(addSink)); } if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } if (path == null) { throw new ArgumentNullException(nameof(path)); } if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) { throw new ArgumentException("Negative value provided; file size limit must be non-negative.", nameof(fileSizeLimitBytes)); } if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) { throw new ArgumentException("At least one file must be retained.", nameof(retainedFileCountLimit)); } if (shared && buffered) { throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered)); } if (shared && hooks != null) { throw new ArgumentException("File lifecycle hooks are not currently supported for shared log files.", nameof(hooks)); } ILogEventSink sink; if (rollOnFileSizeLimit || persistentFileRollingInterval != PersistentFileRollingInterval.Infinite) { sink = new RollingFileSink(path, formatter, fileSizeLimitBytes, retainedFileCountLimit, encoding, buffered, shared, persistentFileRollingInterval, rollOnFileSizeLimit, hooks, preserveLogFilename, rollOnEachProcessRun, useLastWriteAsTimestamp); } else { try { if (shared) { #pragma warning disable 618 sink = new SharedFileSink(path, formatter, fileSizeLimitBytes, encoding); #pragma warning restore 618 } else { sink = new FileSink(path, formatter, fileSizeLimitBytes, encoding, buffered, hooks); } } catch (Exception ex) { SelfLog.WriteLine("Unable to open file sink for {0}: {1}", path, ex); if (propagateExceptions) { throw; } return(addSink(new NullSink(), LevelAlias.Maximum, null)); } } if (flushToDiskInterval.HasValue) { #pragma warning disable 618 sink = new PeriodicFlushToDiskSink(sink, flushToDiskInterval.Value); #pragma warning restore 618 } return(addSink(sink, restrictedToMinimumLevel, levelSwitch)); }
static LoggerConfiguration ConfigureFile( this Func <ILogEventSink, LogEventLevel, LoggingLevelSwitch, LoggerConfiguration> addSink, ITextFormatter formatter, string path, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, long?fileSizeLimitBytes = DefaultFileSizeLimitBytes, LoggingLevelSwitch levelSwitch = null, bool buffered = false, bool propagateExceptions = false, bool shared = false, TimeSpan?flushToDiskInterval = null) { if (addSink == null) { throw new ArgumentNullException(nameof(addSink)); } if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } if (path == null) { throw new ArgumentNullException(nameof(path)); } if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) { throw new ArgumentException("Negative value provided; file size limit must be non-negative"); } if (shared && buffered) { throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered)); } ILogEventSink sink; try { if (shared) { sink = new SharedFileSink(path, formatter, fileSizeLimitBytes); } else { sink = new FileSink(path, formatter, fileSizeLimitBytes, buffered: buffered); } } catch (Exception ex) { SelfLog.WriteLine("Unable to open file sink for {0}: {1}", path, ex); if (propagateExceptions) { throw; } return(addSink(new NullSink(), LevelAlias.Maximum, null)); } if (flushToDiskInterval.HasValue) { sink = new PeriodicFlushToDiskSink(sink, flushToDiskInterval.Value); } return(addSink(sink, restrictedToMinimumLevel, levelSwitch)); }
static LoggerConfiguration ConfigureFile( this Func <ILogEventSink, LogEventLevel, LoggingLevelSwitch, LoggerConfiguration> addSink, ITextFormatter formatter, string path, LogEventLevel restrictedToMinimumLevel, long?fileSizeLimitBytes, LoggingLevelSwitch levelSwitch, bool buffered, bool propagateExceptions, bool shared, TimeSpan?flushToDiskInterval, Encoding encoding, RollingInterval rollingInterval, bool rollOnFileSizeLimit, int?retainedFileCountLimit) { if (addSink == null) { throw new ArgumentNullException(nameof(addSink)); } if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } if (path == null) { throw new ArgumentNullException(nameof(path)); } if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) { throw new ArgumentException("Negative value provided; file size limit must be non-negative.", nameof(fileSizeLimitBytes)); } if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) { throw new ArgumentException("At least one file must be retained.", nameof(retainedFileCountLimit)); } if (shared && buffered) { throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered)); } ILogEventSink sink; if (rollOnFileSizeLimit || rollingInterval != RollingInterval.Infinite) { sink = new RollingFileSink(path, formatter, fileSizeLimitBytes, retainedFileCountLimit, encoding, buffered, shared, rollingInterval, rollOnFileSizeLimit); } else { try { #pragma warning disable 618 if (shared) { sink = new SharedFileSink(path, formatter, fileSizeLimitBytes); } else { sink = new FileSink(path, formatter, fileSizeLimitBytes, buffered: buffered); } #pragma warning restore 618 } catch (Exception ex) { SelfLog.WriteLine("Unable to open file sink for {0}: {1}", path, ex); if (propagateExceptions) { throw; } return(addSink(new NullSink(), LevelAlias.Maximum, null)); } } if (flushToDiskInterval.HasValue) { sink = new PeriodicFlushToDiskSink(sink, flushToDiskInterval.Value); } return(addSink(sink, restrictedToMinimumLevel, levelSwitch)); }