コード例 #1
0
        /// <summary>
        /// Adds a console target to the logging system
        /// </summary>
        /// <param name="options"></param>
        /// <param name="condition">Log conditions for this target</param>
        /// <param name="formatter">Message formatter for this target</param>
        /// <param name="output">Output buffer this target will write to</param>
        /// <param name="enableBuffering">Sets if buffering should be used with this target</param>
        /// <param name="maxBufferSize">Maximum number of elements in the buffer</param>
        /// <param name="bufferExceededBehavior">Sets the behavior of this target when the buffer is full and new entries are logged</param>
        public static LogBuilder AddConsoleTarget(
            this LogBuilder options,
            Func <LogEntry, bool> condition   = null,
            Func <LogEntry, string> formatter = null,
            ConsoleOutputs output             = ConsoleOutputs.StandardOutput,
            bool enableBuffering = true,
            int maxBufferSize    = BufferSize.Infinite,
            BufferExceededBehaviors bufferExceededBehavior = BufferExceededBehaviors.Lock)
        {
            var console_logger = new ConsoleTarget(
                (m) => System.Console.WriteLine(m),
                (m) => System.Console.Error.WriteLine(m))
            {
                Formatter    = formatter ?? ConsoleTarget.DEFAULT_FORMAT,
                OutputStream = output,
                Condition    = condition
            };

            if (enableBuffering)
            {
                options.AddLogTarget(
                    new BufferedLogger(
                        console_logger,
                        maxBufferSize,
                        bufferExceededBehavior));
            }
            else
            {
                options.AddLogTarget(console_logger);
            }

            return(options);
        }
コード例 #2
0
        /// <summary>
        /// Adds a file target to the logging system
        /// </summary>
        /// <param name="options"></param>
        /// <param name="folderPath">Path where the log files should be saved on</param>
        /// <param name="fileName">Function to determine filename</param>
        /// <param name="maxFileSize">Maximum file size before file name sequence will be increased</param>
        /// <param name="condition">Log conditions for this target</param>
        /// <param name="formatter">Message formatter for this target</param>
        /// <param name="encoding">Encoding of the file, defaults to: Encoding.Default</param>
        /// <param name="enableBuffering">Sets if buffering should be used with this target</param>
        /// <param name="maxBufferSize">Maximum number of elements in the buffer</param>
        /// <param name="bufferExceededBehavior">Sets the behavior of this target when the buffer is full and new entries are logged</param>
        public static LogBuilder AddFileTarget(
            this LogBuilder options,
            string folderPath = ".\\logs",
            Func <FileTargetNamingOptions, string> fileName = null,
            int maxFileSize   = 100 *MB,
            Encoding encoding = null,
            Func <LogEntry, bool> condition   = null,
            Func <LogEntry, string> formatter = null,
            bool enableBuffering = true,
            int maxBufferSize    = BufferSize.Infinite,
            BufferExceededBehaviors bufferExceededBehavior = BufferExceededBehaviors.Lock)
        {
            var fileLogger = new FileTarget(Repository)
            {
                Formatter   = formatter ?? FileTarget.DEFAULT_FORMAT,
                Condition   = condition,
                FolderPath  = folderPath,
                FileName    = fileName ?? FileTarget.DEFAULT_FILENAME,
                MaxFileSize = maxFileSize,
                Encoding    = encoding ?? Encoding.Default
            };

            if (enableBuffering)
            {
                options.AddLogTarget(
                    new BufferedLogger(
                        fileLogger,
                        maxBufferSize,
                        bufferExceededBehavior));
            }
            else
            {
                options.AddLogTarget(fileLogger);
            }
            return(options);
        }
コード例 #3
0
        public void LogScopeMessageFormatting()
        {
            var builder = new LogBuilder();
            var target  = new LogTarget();

            builder.AddLogTarget(target);
            var log = builder.Build();

            using (log.BeginScope("{name} {id}", "scope", 10))
            {
                log.LogTrace("Test");
            }
            var entry = target.Entries.First();

            Assert.Equal("scope 10", entry.Scope.ToString());
        }