Skip to content

Commit 56bcb8a

Browse files
committed
Merge branch 'stable'
2 parents d73d0b1 + 57be80e commit 56bcb8a

1 file changed

Lines changed: 27 additions & 27 deletions

File tree

docs/site/docs/misc-test-tips.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,33 @@ It can sometimes be helpful to capture log messages sent to `ILogger` types in t
4545

4646
With xUnit, this can be done as follows:
4747

48-
1. Add the following packages to your test project: `Serilog`, `Serilog.Extensions.Logging`, and `Serilog.Sinks.XUnit`.
48+
1. Add the following packages to your test project: [Serilog](https://www.nuget.org/packages/Serilog), [Serilog.Extensions.Logging](https://www.nuget.org/packages/Serilog.Extensions.Logging), [Serilog.Expressions](https://www.nuget.org/packages/Serilog.Expressions), and [Serilog.Sinks.XUnit](https://www.nuget.org/packages/Serilog.Sinks.XUnit).
4949
2. Add the following class/extension method to your test project (which replicates the signature of the removed `AddXunitLogger` method):
5050

5151
```csharp
52-
using Microsoft.Extensions.DependencyInjection;
5352
using Microsoft.Extensions.Logging;
5453
using Serilog;
5554
using Serilog.Events;
55+
using Serilog.Templates;
5656
using Xunit.Abstractions;
57-
58-
namespace Bunit
57+
58+
namespace Xunit;
59+
60+
public static class ServiceCollectionLoggingExtensions
5961
{
60-
public static class ServiceCollectionLoggingExtensions
62+
public static IServiceCollection AddXunitLogger(this IServiceCollection services, ITestOutputHelper outputHelper)
6163
{
62-
public static IServiceCollection AddXunitLogger(this IServiceCollection services, ITestOutputHelper outputHelper)
63-
{
64-
var serilogLogger = new LoggerConfiguration()
65-
.MinimumLevel.Verbose()
66-
.WriteTo.TestOutput(outputHelper, LogEventLevel.Verbose)
67-
.CreateLogger();
68-
69-
services.AddSingleton<ILoggerFactory>(new LoggerFactory().AddSerilog(serilogLogger, dispose: true));
70-
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
71-
72-
return services;
73-
}
64+
var serilogLogger = new LoggerConfiguration()
65+
.MinimumLevel.Verbose()
66+
.WriteTo.TestOutput(
67+
testOutputHelper: outputHelper,
68+
formatter: new ExpressionTemplate("[{UtcDateTime(@t):mm:ss.ffffff} | {@l:u3} | {Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)} | {Coalesce(EventId.Name, '<none>')}] {@m}\n{@x}"),
69+
restrictedToMinimumLevel: LogEventLevel.Verbose)
70+
.CreateLogger();
71+
72+
services.AddSingleton<ILoggerFactory>(_ => new LoggerFactory().AddSerilog(serilogLogger, dispose: true));
73+
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
74+
return services;
7475
}
7576
}
7677
```
@@ -84,19 +85,18 @@ With xUnit, this can be done as follows:
8485
using Xunit;
8586
using Xunit.Abstractions;
8687

87-
namespace MyTests
88+
namespace MyTests;
89+
90+
public class MyComponenTest : TestContext
8891
{
89-
public class MyComponenTest : TestContext
92+
public MyComponenTest(ITestOutputHelper outputHelper)
9093
{
91-
public MyComponenTest(ITestOutputHelper outputHelper)
92-
{
93-
Services.AddXunitLogger(outputHelper);
94-
}
95-
96-
[Fact]
97-
public void Test() ...
94+
Services.AddXunitLogger(outputHelper);
9895
}
99-
}
96+
97+
[Fact]
98+
public void Test() ...
99+
}
100100
```
101101

102102
## Easier HTML copying/pasting

0 commit comments

Comments
 (0)