Skip to content

Commit 8823f91

Browse files
committed
Update readme for C3D.Extensions.Logging.Xunit
1 parent 1eeeedb commit 8823f91

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/C3D/Extensions/Logging/Xunit/README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,38 @@ Alternatively, you can explicitly add the output, and/or setup the logging confi
4242
logging
4343
.AddXunit(output, config=>{
4444
});
45-
```
45+
```
46+
47+
### Direct injection
48+
49+
```c#
50+
private readonly ITestOutputHelper output;
51+
52+
ILogger<ClassUnderTest> log = output.CreateLogger<ClassUnderTest>();
53+
54+
\\ Assuming that the constuctor of ClassUnderTest takes an ILogger or ILogger<ClassUnderTest>
55+
var sut = new ClassUnderTest(log);
56+
57+
\\ perform tests with logging...
58+
```
59+
60+
Here the ILogger creation line might replace something like
61+
```c#
62+
ILogger<ClassUnderTest> log = Microsoft.Extensions.Logging.Abstractions.NullLoggerFactory.Instance.CreateLogger<ClassUnderTest>();
63+
```
64+
65+
### Factory
66+
67+
You can also create a LoggerFactory if you need to using something like
68+
69+
```c#
70+
private readonly ITestOutputHelper output;
71+
72+
var loggerFactory = LoggerFactory.Create(builder => builder.AddXunit(output));
73+
```
74+
75+
### Fixtures
76+
77+
The same mechanisms used in tests based on injecting `ITestOutputHelper` into the test class, can be used in fixtures.
78+
However, in fixtures you inject `IMessageSink`. The same extension methods are available for both interfaces.
79+

src/C3D/Extensions/Logging/Xunit/XunitLoggerProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public ILogger CreateLogger(string categoryName) =>
5454
name => output1 is null ?
5555
(output2 is null ? NullLogger.Instance :
5656
new MessageSinkLogger(name, output2, GetOptions)) :
57-
new TextOutputLogger(name, output1, GetOptions));
57+
new TextOutputLogger(name, output1, GetOptions));
5858

5959
private void Dispose(bool disposing)
6060
{

0 commit comments

Comments
 (0)