-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathXUnitLoggerProvider.cs
More file actions
162 lines (134 loc) · 5.24 KB
/
XUnitLoggerProvider.cs
File metadata and controls
162 lines (134 loc) · 5.24 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System.Text;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit.Abstractions;
namespace TestBuildingBlocks;
// Based on https://www.meziantou.net/how-to-get-asp-net-core-logs-in-the-output-of-xunit-tests.htm.
public sealed class XUnitLoggerProvider : ILoggerProvider
{
private const LogOutputFields DefaultLogOutputFields = LogOutputFields.All & ~LogOutputFields.CategoryNamespace;
private readonly ITestOutputHelper _testOutputHelper;
private readonly LogOutputFields _outputFields;
private readonly string? _categoryPrefixFilter;
public XUnitLoggerProvider(ITestOutputHelper testOutputHelper, string? categoryPrefixFilter, LogOutputFields outputFields = DefaultLogOutputFields)
{
ArgumentNullException.ThrowIfNull(testOutputHelper);
_testOutputHelper = testOutputHelper;
_categoryPrefixFilter = categoryPrefixFilter;
_outputFields = outputFields;
}
public ILogger CreateLogger(string categoryName)
{
ArgumentException.ThrowIfNullOrEmpty(categoryName);
if (_categoryPrefixFilter == null || categoryName.StartsWith(_categoryPrefixFilter, StringComparison.Ordinal))
{
return new XUnitLogger(_testOutputHelper, _outputFields, categoryName);
}
return NullLogger.Instance;
}
public void Dispose()
{
}
private sealed class XUnitLogger(ITestOutputHelper testOutputHelper, LogOutputFields outputFields, string categoryName) : ILogger
{
private readonly ITestOutputHelper _testOutputHelper = testOutputHelper;
private readonly LogOutputFields _outputFields = outputFields;
private readonly string? _categoryText = GetCategoryText(categoryName, outputFields);
private static string? GetCategoryText(string categoryName, LogOutputFields outputFields)
{
if (outputFields.HasFlag(LogOutputFields.Category))
{
return categoryName;
}
bool hasName = outputFields.HasFlag(LogOutputFields.CategoryName);
bool hasNamespace = outputFields.HasFlag(LogOutputFields.CategoryNamespace);
if (hasName || hasNamespace)
{
// Microsoft.Extensions.Logging.LoggerFactory.CreateLogger(Type) removes generic type parameters
// and replaces '+' (nested class) with '.'.
int lastDotIndex = categoryName.LastIndexOf('.');
if (lastDotIndex == -1)
{
return hasName ? categoryName : string.Empty;
}
return hasName ? categoryName[(lastDotIndex + 1)..] : categoryName[..lastDotIndex];
}
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
#if DEBUG
return logLevel != LogLevel.None;
#else
return false;
#endif
}
public IDisposable BeginScope<TState>(TState state)
where TState : notnull
{
return EmptyDisposable.Instance;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
var builder = new StringBuilder();
if (_outputFields.HasFlag(LogOutputFields.Level))
{
string logLevelString = GetLogLevelString(logLevel);
builder.Append(logLevelString);
}
if (_categoryText != null)
{
if (builder.Length > 0)
{
builder.Append(' ');
}
builder.Append('[');
builder.Append(_categoryText);
builder.Append(']');
}
if (_outputFields.HasFlag(LogOutputFields.Message))
{
if (builder.Length > 0)
{
builder.Append(' ');
}
string message = formatter(state, exception);
builder.Append(message);
}
if (exception != null && _outputFields.HasFlag(LogOutputFields.Exception))
{
builder.Append(Environment.NewLine);
builder.Append(exception);
}
try
{
_testOutputHelper.WriteLine(builder.ToString());
}
catch (InvalidOperationException)
{
// Silently ignore when there is no currently active test.
}
}
private static string GetLogLevelString(LogLevel logLevel)
{
return logLevel switch
{
LogLevel.Trace => "TRCE",
LogLevel.Debug => "DBUG",
LogLevel.Information => "INFO",
LogLevel.Warning => "WARN",
LogLevel.Error => "FAIL",
LogLevel.Critical => "CRIT",
LogLevel.None => "",
_ => throw new ArgumentOutOfRangeException(nameof(logLevel))
};
}
private sealed class EmptyDisposable : IDisposable
{
public static EmptyDisposable Instance { get; } = new();
public void Dispose()
{
}
}
}
}