-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathXUnitLoggerConfigurationExtensions.cs
More file actions
124 lines (112 loc) · 6.1 KB
/
XUnitLoggerConfigurationExtensions.cs
File metadata and controls
124 lines (112 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Formatting.Display;
using Serilog.Sinks.XUnit;
using Xunit;
using Xunit.Sdk;
namespace Serilog;
/// <summary>
/// Adds the WriteTo.TestOutput() extension method to <see cref="LoggerConfiguration"/>.
/// </summary>
public static class TestOutputLoggerConfigurationExtensions
{
internal const string DefaultConsoleOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}";
/// <summary>
/// Writes log events to <see cref="ITestOutputHelper"/>.
/// </summary>
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="messageSink">The <see cref="IMessageSink"/> that will be written to.</param>
/// <param name="restrictedToMinimumLevel">The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
/// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level to be changed at runtime.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration TestOutput(
this LoggerSinkConfiguration sinkConfiguration,
IMessageSink messageSink,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = DefaultConsoleOutputTemplate,
IFormatProvider formatProvider = null,
LoggingLevelSwitch levelSwitch = null)
{
ArgumentNullException.ThrowIfNull(sinkConfiguration);
ArgumentNullException.ThrowIfNull(messageSink);
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
return sinkConfiguration.Sink(new TestOutputSink(messageSink, formatter), restrictedToMinimumLevel, levelSwitch);
}
/// <summary>
/// Writes log events to <see cref="ITestOutputHelper"/>.
/// </summary>
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="messageSink">The <see cref="IMessageSink"/> that will be written to.</param>
/// <param name="formatter">Controls the rendering of log events into text, for example to log JSON. To
/// control plain text formatting, use the overload that accepts an output template.</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>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration TestOutput(
this LoggerSinkConfiguration sinkConfiguration,
IMessageSink messageSink,
ITextFormatter formatter,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
LoggingLevelSwitch levelSwitch = null)
{
ArgumentNullException.ThrowIfNull(sinkConfiguration);
ArgumentNullException.ThrowIfNull(formatter);
return sinkConfiguration.Sink(new TestOutputSink(messageSink, formatter), restrictedToMinimumLevel, levelSwitch);
}
/// <summary>
/// Writes log events to <see cref="ITestOutputHelper"/>.
/// </summary>
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="testOutputHelper">The <see cref="ITestOutputHelper"/> that will be written to.</param>
/// <param name="restrictedToMinimumLevel">The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
/// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level to be changed at runtime.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration TestOutput(
this LoggerSinkConfiguration sinkConfiguration,
ITestOutputHelper testOutputHelper,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = DefaultConsoleOutputTemplate,
IFormatProvider formatProvider = null,
LoggingLevelSwitch levelSwitch = null)
{
ArgumentNullException.ThrowIfNull(sinkConfiguration);
ArgumentNullException.ThrowIfNull(testOutputHelper);
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
return sinkConfiguration.Sink(new TestOutputSink(testOutputHelper, formatter), restrictedToMinimumLevel, levelSwitch);
}
/// <summary>
/// Writes log events to <see cref="ITestOutputHelper"/>.
/// </summary>
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="testOutputHelper">The <see cref="ITestOutputHelper"/> that will be written to.</param>
/// <param name="formatter">Controls the rendering of log events into text, for example to log JSON. To
/// control plain text formatting, use the overload that accepts an output template.</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>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration TestOutput(
this LoggerSinkConfiguration sinkConfiguration,
ITestOutputHelper testOutputHelper,
ITextFormatter formatter,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
LoggingLevelSwitch levelSwitch = null)
{
ArgumentNullException.ThrowIfNull(sinkConfiguration);
ArgumentNullException.ThrowIfNull(formatter);
return sinkConfiguration.Sink(new TestOutputSink(testOutputHelper, formatter), restrictedToMinimumLevel, levelSwitch);
}
}