66using Aspire . Hosting . Testing ;
77using Elastic . Documentation . ServiceDefaults ;
88using FluentAssertions ;
9+ using InMemLogger ;
910using Microsoft . Extensions . DependencyInjection ;
1011using Microsoft . Extensions . Logging ;
1112
@@ -18,12 +19,17 @@ public class AssembleFixture : IAsyncLifetime
1819{
1920 public DistributedApplication DistributedApplication { get ; private set ; } = null ! ;
2021
22+ public InMemoryLogger InMemoryLogger { get ; private set ; } = null ! ;
23+
2124 /// <inheritdoc />
2225 public async ValueTask InitializeAsync ( )
2326 {
2427 var builder = await DistributedApplicationTestingBuilder . CreateAsync < Projects . Elastic_Documentation_Aspire > ( ) ;
2528 _ = builder . Services . AddAppLogging ( LogLevel . Information ) ;
29+ _ = builder . Services . AddLogging ( c => c . AddXUnit ( ) ) ;
30+ _ = builder . Services . AddLogging ( c => c . AddInMemory ( ) ) ;
2631 DistributedApplication = await builder . BuildAsync ( ) ;
32+ InMemoryLogger = DistributedApplication . Services . GetService < InMemoryLogger > ( ) ! ;
2733 await DistributedApplication . StartAsync ( ) ;
2834 }
2935
@@ -38,7 +44,8 @@ public async ValueTask DisposeAsync()
3844
3945}
4046
41- public class DatabaseTestClass1 ( AssembleFixture fixture , ITestOutputHelper output )
47+
48+ public class DatabaseTestClass1 ( AssembleFixture fixture , ITestOutputHelper output ) : IAsyncLifetime
4249{
4350 [ Fact ]
4451 public async Task X ( )
@@ -52,5 +59,17 @@ public async Task X()
5259 }
5360
5461
55- // ...
62+ /// <inheritdoc />
63+ public ValueTask DisposeAsync ( )
64+ {
65+ GC . SuppressFinalize ( this ) ;
66+ if ( TestContext . Current . TestState ? . Result is TestResult . Passed )
67+ return default ;
68+ foreach ( var resource in fixture . InMemoryLogger . RecordedLogs )
69+ output . WriteLine ( resource . Message ) ;
70+ return default ;
71+ }
72+
73+ /// <inheritdoc />
74+ public ValueTask InitializeAsync ( ) => default ;
5675}
0 commit comments