Пример #1
0
        public void WhenRenderingProtectedTokensShouldNotBeParsed()
        {
            var formatter  = new MessageTemplateTextFormatter(string.Empty, null);
            var properties = new Dictionary <string, LogEventPropertyValue>
            {
                { "MyToken", new ScalarValue("Foo") },
                { "Date", new ScalarValue("Bar") },
                { "Hour", new ScalarValue("Bar") },
                { "HalfHour", new ScalarValue("Bar") }
            };

            // Verify Date
            var sink = new ContextRollingFileSink("/Logs/{MyToken:l}_{Date}.txt", formatter, 1, 1);
            var path = sink.RenderPath(properties);

            path.Should().Be("/Logs/Foo_{Date}.txt");

            // Verify Hour
            sink = new ContextRollingFileSink("/Logs/{MyToken:l}_{Hour}.txt", formatter, 1, 1);
            path = sink.RenderPath(properties);
            path.Should().Be("/Logs/Foo_{Hour}.txt");

            // Verify HalfHour
            sink = new ContextRollingFileSink("/Logs/{MyToken:l}_{HalfHour}.txt", formatter, 1, 1);
            path = sink.RenderPath(properties);
            path.Should().Be("/Logs/Foo_{HalfHour}.txt");
        }
Пример #2
0
        /// <summary>
        /// Write log events to a series of files. Each file will be named according to
        /// the date of the first log entry written to it. Only simple date-based rolling is
        /// currently supported.
        /// </summary>
        /// <param name="sinkConfiguration">Logger sink configuration.</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="pathFormat">String describing the location of the log files,
        /// with {Date} in the place of the file date. E.g. "Logs\myapp-{Date}.log" will result in log
        /// files such as "Logs\myapp-2013-10-20.log", "Logs\myapp-2013-10-21.log" and so on.</param>
        /// <param name="restrictedToMinimumLevel">The minimum level for
        /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
        /// <param name="levelSwitch">A switch allowing the pass-through minimum level
        /// to be changed at runtime.</param>
        /// <param name="fileSizeLimitBytes">The maximum size, in bytes, to which any single log file will be allowed to grow.
        /// For unrestricted growth, pass null. The default is 1 GB.</param>
        /// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
        /// including the current log file. For unlimited retention, pass null. The default is 31.</param>
        /// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
        /// is false.</param>
        /// <param name="shared">Allow the log files to be shared by multiple processes. The default is false.</param>
        /// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        /// <remarks>The file will be written using the UTF-8 encoding without a byte-order mark.</remarks>
        public static LoggerConfiguration ContextRollingFile(
            this LoggerSinkConfiguration sinkConfiguration,
            ITextFormatter formatter,
            string pathFormat,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            long?fileSizeLimitBytes        = DefaultFileSizeLimitBytes,
            int?retainedFileCountLimit     = DefaultRetainedFileCountLimit,
            LoggingLevelSwitch levelSwitch = null,
            bool buffered = false,
            bool shared   = false,
            TimeSpan?flushToDiskInterval = null)
        {
            if (sinkConfiguration == null)
            {
                throw new ArgumentNullException(nameof(sinkConfiguration));
            }
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            if (shared && buffered)
            {
                throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered));
            }

            ILogEventSink sink = new ContextRollingFileSink(pathFormat, formatter, fileSizeLimitBytes, retainedFileCountLimit, buffered: buffered, shared: shared);

            if (flushToDiskInterval.HasValue)
            {
                sink = new PeriodicFlushToDiskSink(sink, flushToDiskInterval.Value);
            }

            return(sinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch));
        }
Пример #3
0
        public void WhenRenderingPathTemplateDateTokenRemains()
        {
            var formatter = new MessageTemplateTextFormatter(string.Empty, null);
            var sink      = new ContextRollingFileSink("/Logs/{MyToken:l}_{Date}.txt", formatter, 1, 1);

            var properties = new Dictionary <string, LogEventPropertyValue>
            {
                { "MyToken", new ScalarValue("Foo") }
            };
            var path = sink.RenderPath(properties);

            path.Should().Be("/Logs/Foo_{Date}.txt");
        }