-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathTestOutputSink.cs
More file actions
38 lines (32 loc) · 1.22 KB
/
TestOutputSink.cs
File metadata and controls
38 lines (32 loc) · 1.22 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
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Xunit;
using Xunit.Sdk;
using Xunit.v3;
namespace Serilog.Sinks.XUnit;
internal sealed class TestOutputSink : ILogEventSink
{
private readonly IMessageSink messageSink;
private readonly ITestOutputHelper testOutputHelper;
private readonly ITextFormatter textFormatter;
public TestOutputSink(IMessageSink messageSink, ITextFormatter textFormatter)
{
this.messageSink = messageSink ?? throw new ArgumentNullException(nameof(messageSink));
this.textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter));
}
public TestOutputSink(ITestOutputHelper testOutputHelper, ITextFormatter textFormatter)
{
this.testOutputHelper = testOutputHelper ?? throw new ArgumentNullException(nameof(testOutputHelper));
this.textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter));
}
public void Emit(LogEvent logEvent)
{
ArgumentNullException.ThrowIfNull(logEvent);
using var renderSpace = new StringWriter();
textFormatter.Format(logEvent, renderSpace);
var message = renderSpace.ToString().Trim();
messageSink?.OnMessage(new DiagnosticMessage(message));
testOutputHelper?.WriteLine(message);
}
}