Beispiel #1
0
 public void ItCreatesNewFileWhenSizeLimitReached()
 {
     using (var dir = new TestDirectory())
         using (var sizeRollingSink = new AlternateRollingFileSink(dir.LogDirectory, new RawFormatter(), 10))
         {
             var logEvent = Some.InformationEvent();
             sizeRollingSink.Emit(logEvent);
             Assert.That(sizeRollingSink.CurrentLogFile.LogFileInfo.Sequence, Is.EqualTo(1));
             sizeRollingSink.Emit(logEvent);
             Assert.That(sizeRollingSink.CurrentLogFile.LogFileInfo.Sequence, Is.EqualTo(2));
         }
 }
Beispiel #2
0
        public static LoggerConfiguration SuperRollingFileAlternate(
            this LoggerSinkConfiguration configuration,
            string logDirectory,
            string logFilePrefix = "",
            SuperRollingFileAlternateFormats format = SuperRollingFileAlternateFormats.Text,
            ITextFormatter formatter   = null,
            LogEventLevel minimumLevel = LevelAlias.Minimum,
            long fileSizeLimitBytes    = DefaultFileSizeLimitBytes,
            int?retainedFileCountLimit = DefaultRetainedFileCountLimit,
            Encoding encoding          = null,
            bool renderMessage         = false,
            bool async                     = false,
            string outputTemplate          = DefaultOutputTemplate,
            IFormatProvider formatProvider = null)
        {
            if (formatter == null)
            {
                switch (format)
                {
                case SuperRollingFileAlternateFormats.Json:
                    formatter = new JsonFormatter(renderMessage: renderMessage);
                    break;

                case SuperRollingFileAlternateFormats.CompactJson:
                    formatter = new CompactJsonFormatter();
                    break;

                case SuperRollingFileAlternateFormats.Text:
                default:
                    formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
                    break;
                }
            }
            var sink = new AlternateRollingFileSink(logDirectory, formatter, fileSizeLimitBytes,
                                                    retainedFileCountLimit, encoding, logFilePrefix);

            if (async)
            {
                return(configuration.Async(c => c.Sink(sink, minimumLevel)));
            }
            else
            {
                return(configuration.Sink(sink, minimumLevel));
            }
        }
        /// <summary>
        /// Creates an alternative implementation of the rolling file sink
        /// that rolls files based on their size.
        /// </summary>
        /// <param name="configuration"><see cref="LoggerSinkConfiguration"/></param>
        /// <param name="logDirectory">The names of the directory to be logged</param>
        /// <param name="minimumLevel">Minimum <see cref="LogLevel"/></param>
        /// <param name="outputTemplate">The template for substituting logged parameters</param>
        /// <param name="formatProvider">A culture specific format provider</param>
        /// <param name="fileSizeLimitBytes">The size files should grow up to (default 2MB)</param>
        /// <returns></returns>
        public static LoggerConfiguration RollingFileAlternate(
            this LoggerSinkConfiguration configuration,
            string logDirectory,
            LogEventLevel minimumLevel     = LevelAlias.Minimum,
            string outputTemplate          = DefaultOutputTemplate,
            IFormatProvider formatProvider = null,
            long?fileSizeLimitBytes        = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            var templateFormatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
            var sink = new AlternateRollingFileSink(logDirectory, templateFormatter, fileSizeLimitBytes ?? TwoMegabytes);

            return(configuration.Sink(sink, minimumLevel));
        }
        /// <summary>
        /// Creates an alternative implementation of the rolling file sink with
        /// an overload to pass an ITextFormatter.
        /// </summary>
        /// <param name="configuration"><see cref="LoggerSinkConfiguration"/></param>
        /// <param name="formatter">Formatter to control how events are rendered into the file. To control
        /// plain text formatting, use the overload that accepts an output template instead.</param>
        /// <param name="logDirectory">The names of the directory to be logged</param>
        /// <param name="minimumLevel">Minimum <see cref="LogLevel"/></param>
        /// <param name="fileSizeLimitBytes">The size files should grow up to (default 2MB)</param>
        /// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
        /// including the current log file. The default is null which is unlimited.</param>
        /// <returns></returns>
        public static LoggerConfiguration RollingFileAlternate(
            this LoggerSinkConfiguration configuration,
            ITextFormatter formatter,
            string logDirectory,
            LogEventLevel minimumLevel = LevelAlias.Minimum,
            long?fileSizeLimitBytes    = null,
            int?retainedFileCountLimit = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            var sink = new AlternateRollingFileSink(logDirectory, formatter, fileSizeLimitBytes ?? TwoMegabytes,
                                                    retainedFileCountLimit);

            return(configuration.Sink(sink, minimumLevel));
        }