コード例 #1
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="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 RollingFile(
            this LoggerSinkConfiguration sinkConfiguration,
            ITextFormatter formatter,
            string pathFormat,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            long?fileSizeLimitBytes        = DefaultFileSizeLimitBytes,
            int?retainedFileCountLimit     = DefaultRetainedFileCountLimit,
            LoggingLevelSwitch levelSwitch = null,
            TimeSpan?flushToDiskInterval   = null)
        {
            if (sinkConfiguration == null)
            {
                throw new ArgumentNullException(nameof(sinkConfiguration));
            }
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            ILogEventSink sink = new RollingFileSink(pathFormat, formatter, fileSizeLimitBytes, retainedFileCountLimit);

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

            return(sinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch));
        }
コード例 #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 RollingFile(
            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 RollingFileSink(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
        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));
        }
コード例 #4
0
        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));
        }