|
| 1 | +// Copyright 2017 Serilog Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using Serilog.Configuration; |
| 17 | +using Serilog.Core; |
| 18 | +using Serilog.Events; |
| 19 | +using Serilog.Formatting; |
| 20 | +using Serilog.Formatting.Display; |
| 21 | +using Serilog.Sinks.Debug; |
| 22 | + |
| 23 | +namespace Serilog |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Adds the WriteTo.Debug() extension method to <see cref="LoggerConfiguration"/>. |
| 27 | + /// </summary> |
| 28 | + public static class ConsoleLoggerConfigurationExtensions |
| 29 | + { |
| 30 | + const string DefaultDebugOutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Writes log events to <see cref="System.Diagnostics.Debug"/>. |
| 34 | + /// </summary> |
| 35 | + /// <param name="sinkConfiguration">Logger sink configuration.</param> |
| 36 | + /// <param name="restrictedToMinimumLevel">The minimum level for |
| 37 | + /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param> |
| 38 | + /// <param name="levelSwitch">A switch allowing the pass-through minimum level |
| 39 | + /// to be changed at runtime.</param> |
| 40 | + /// <param name="outputTemplate">A message template describing the format used to write to the sink. |
| 41 | + /// the default is <code>"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"</code>.</param> |
| 42 | + /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> |
| 43 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 44 | + public static LoggerConfiguration Debug( |
| 45 | + this LoggerSinkConfiguration sinkConfiguration, |
| 46 | + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, |
| 47 | + string outputTemplate = DefaultDebugOutputTemplate, |
| 48 | + IFormatProvider formatProvider = null, |
| 49 | + LoggingLevelSwitch levelSwitch = null) |
| 50 | + { |
| 51 | + if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration)); |
| 52 | + if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate)); |
| 53 | + |
| 54 | + var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider); |
| 55 | + return sinkConfiguration.Debug(formatter, restrictedToMinimumLevel, levelSwitch); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Writes log events to <see cref="System.Diagnostics.Debug"/>. |
| 60 | + /// </summary> |
| 61 | + /// <param name="sinkConfiguration">Logger sink configuration.</param> |
| 62 | + /// <param name="formatter">Controls the rendering of log events into text, for example to log JSON. To |
| 63 | + /// control plain text formatting, use the overload that accepts an output template.</param> |
| 64 | + /// <param name="restrictedToMinimumLevel">The minimum level for |
| 65 | + /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param> |
| 66 | + /// <param name="levelSwitch">A switch allowing the pass-through minimum level |
| 67 | + /// to be changed at runtime.</param> |
| 68 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 69 | + public static LoggerConfiguration Debug( |
| 70 | + this LoggerSinkConfiguration sinkConfiguration, |
| 71 | + ITextFormatter formatter, |
| 72 | + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, |
| 73 | + LoggingLevelSwitch levelSwitch = null) |
| 74 | + { |
| 75 | + if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration)); |
| 76 | + if (formatter == null) throw new ArgumentNullException(nameof(formatter)); |
| 77 | + |
| 78 | + return sinkConfiguration.Sink(new DebugSink(formatter), restrictedToMinimumLevel, levelSwitch); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments