Exemplo n.º 1
0
        /// <summary>
        /// Writes the output to files by period.
        /// </summary>
        /// <param name="sinksFactory">Sinks factory.</param>
        /// <param name="logFileFolder">Log files folder path. The default is the application running folder.</param>
        /// <param name="period">Period for rolling the files. The default is
        /// <see cref="RollingFilePeriod.Day"/>.</param>
        /// <param name="async">True if write to the console should be asynchronous, otherwise false.
        /// The default is false.</param>
        /// <param name="encoding">File encoding. The default is UTF8.</param>
        /// <param name="bufferSize">Buffer size to be used. The default is 4096.</param>
        /// <param name="minimumLevel">Minimum log level. The default is the logger minimum level.</param>
        /// <param name="formatter">Log formatter to be used. The default is
        /// <see cref="CompactKeyValueFormatter"/>.</param>
        /// <returns>Logger factory.</returns>
        public static LoggerFactory RollingFile(this LogSinksFactory sinksFactory, string logFileFolder = null,
                                                RollingFilePeriod period = null, bool async              = false, Encoding encoding = null, int bufferSize = 4096,
                                                LogLevel?minimumLevel    = null, ILogFormatter formatter = null)
        {
            if (string.IsNullOrEmpty(logFileFolder))
            {
                logFileFolder = DefaultLogFolderPath;
            }

            if (period == null)
            {
                period = RollingFilePeriod.Day;
            }

            formatter = formatter ?? new CompactKeyValueFormatter();
            Func <IDataRenderer> renderer = () => new DefaultDataRenderer();
            var namer = new RollingFileNamer(logFileFolder, period);

            var sink = async
                ? (ILogSink) new AsyncRollingFileSink(formatter, renderer, namer, encoding, bufferSize)
                : new RollingFileSink(formatter, renderer, namer, encoding, bufferSize);

            sink.MinimumLevel = minimumLevel;

            sinksFactory.Sinks.Add(sink);
            return(sinksFactory.Factory);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Writes the output to an in-memory variable.
 /// <para />
 /// Please use <see cref="InMemorySink.GetLogs"/> to read the logs.
 /// </summary>
 /// <param name="sinksFactory">Sinks factory.</param>
 /// <param name="appendNewLine">Indicates whether a new line should be appended at the end of each log.
 /// The default is true.</param>
 /// <param name="minimumLevel">Minimum log level. The default is the logger minimum level.</param>
 /// <param name="formatter">Log formatter to be used. The default is
 /// <see cref="CompactKeyValueFormatter"/>.</param>
 /// <returns>Logger factory.</returns>
 public static LoggerFactory InMemory(this LogSinksFactory sinksFactory, bool appendNewLine = true,
                                      LogLevel?minimumLevel = null, ILogFormatter formatter = null)
 {
     formatter = formatter ?? new CompactKeyValueFormatter();
     sinksFactory.Sinks.Add(new InMemorySink(formatter, () => new DefaultDataRenderer())
     {
         AppendNewLine = appendNewLine,
         MinimumLevel  = minimumLevel
     });
     return(sinksFactory.Factory);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Writes the output to a xUnit test runner.
        /// </summary>
        /// <param name="sinksFactory">Sinks factory.</param>
        /// <param name="output">xUnit output helper.</param>
        /// <param name="minimumLevel">Minimum log level. The default is the logger minimum level.</param>
        /// <param name="formatter">Log formatter to be used. The default is
        /// <see cref="CompactKeyValueFormatter"/>.</param>
        /// <returns>Logger factory.</returns>
        public static LoggerFactory TestOutput(this LogSinksFactory sinksFactory, ITestOutputHelper output,
                                               LogLevel?minimumLevel = null, ILogFormatter formatter = null)
        {
            formatter = formatter ?? new CompactKeyValueFormatter();

            var sink = new TestOutputSink(output, formatter, () => new DefaultDataRenderer(), minimumLevel);

            sinksFactory.Sinks.Add(sink);

            return(sinksFactory.Factory);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Writes the output to the console.
        /// </summary>
        /// <param name="sinksFactory">Sinks factory.</param>
        /// <param name="async">True if write to the console should be asynchronous, otherwise false.
        /// The default is false.</param>
        /// <param name="theme">Output color theme.</param>
        /// <param name="minimumLevel">Minimum log level. The default is the logger minimum level.</param>
        /// <param name="formatter">Log formatter to be used. The default is
        /// <see cref="CompactKeyValueFormatter"/>.</param>
        /// <returns>Logger factory.</returns>
        public static LoggerFactory Console(this LogSinksFactory sinksFactory, bool async = false,
                                            IConsoleTheme theme = null, LogLevel?minimumLevel = null, ILogFormatter formatter = null)
        {
            theme     = theme ?? new DefaultConsoleTheme();
            formatter = formatter ?? new CompactKeyValueFormatter();
            Func <IDataRenderer> renderer = () => new ThemedConsoleDataRenderer(theme);

            var sink = async
                ? (ILogSink) new AsyncConsoleSink(formatter, renderer)
                : new ConsoleSink(formatter, renderer);

            sink.MinimumLevel = minimumLevel;

            sinksFactory.Sinks.Add(sink);

            return(sinksFactory.Factory);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Writes the output to a single file, without any kind of limits.
        /// </summary>
        /// <param name="sinksFactory">Sinks factory.</param>
        /// <param name="logFilePath">Log path, including file name and extension. The default is a log.txt file in
        /// the application running folder.</param>
        /// <param name="async">True if write to the console should be asynchronous, otherwise false.
        /// The default is false.</param>
        /// <param name="encoding">File encoding. The default is UTF8.</param>
        /// <param name="bufferSize">Buffer size to be used. The default is 4096.</param>
        /// <param name="minimumLevel">Minimum log level. The default is the logger minimum level.</param>
        /// <param name="formatter">Log formatter to be used. The default is
        /// <see cref="CompactKeyValueFormatter"/>.</param>
        /// <returns>Logger factory.</returns>
        public static LoggerFactory SingleFile(this LogSinksFactory sinksFactory, string logFilePath = null,
                                               bool async = false, Encoding encoding = null, int bufferSize = 4096, LogLevel?minimumLevel = null,
                                               ILogFormatter formatter = null)
        {
            if (string.IsNullOrEmpty(logFilePath))
            {
                logFilePath = Path.Combine(DefaultLogFolderPath, DefaultLogFileName);
            }
            formatter = formatter ?? new CompactKeyValueFormatter();
            Func <IDataRenderer> renderer = () => new DefaultDataRenderer();

            var sink = async
                ? (ILogSink) new AsyncSingleFileSink(formatter, renderer, logFilePath, encoding, bufferSize)
                : new FileSink(formatter, renderer, logFilePath, encoding, bufferSize);

            sink.MinimumLevel = minimumLevel;

            sinksFactory.Sinks.Add(sink);
            return(sinksFactory.Factory);
        }