-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
79 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Foundatio.Xunit; | ||
|
||
public abstract class TestLoggerBase : IAsyncDisposable | ||
public abstract class TestLoggerBase : IClassFixture<TestLoggerFixture>, IAsyncLifetime | ||
{ | ||
protected readonly ILogger _logger; | ||
protected readonly List<IDisposable> _disposables = []; | ||
|
||
protected TestLoggerBase(ITestOutputHelper output) | ||
{ | ||
var services = new ServiceCollection(); | ||
RegisterRequiredServices(services, output); | ||
var sp = services.BuildServiceProvider(); | ||
_disposables.Add(sp); | ||
Services = sp; | ||
Log = Services.GetTestLogger(); | ||
_logger = Log.CreateLogger(GetType()); | ||
} | ||
|
||
protected IServiceProvider Services { get; } | ||
|
||
protected TService GetService<TService>() where TService : notnull | ||
protected TestLoggerBase(ITestOutputHelper output, TestLoggerFixture fixture) | ||
{ | ||
return Services.GetRequiredService<TService>(); | ||
Fixture = fixture; | ||
fixture.Output = output; | ||
fixture.AddServiceRegistrations(RegisterServices); | ||
} | ||
|
||
protected TestLogger Log { get; } | ||
protected TestLoggerFixture Fixture { get; } | ||
protected IServiceProvider Services => Fixture.Services; | ||
protected TestLogger TestLogger => Fixture.TestLogger; | ||
protected ILogger Log => Fixture.Log; | ||
|
||
private void RegisterRequiredServices(IServiceCollection services, ITestOutputHelper output) | ||
protected virtual void RegisterServices(IServiceCollection services) | ||
{ | ||
services.AddLogging(c => c.AddTestLogger(output)); | ||
RegisterServices(services); | ||
} | ||
|
||
protected virtual void RegisterServices(IServiceCollection services) | ||
public virtual Task InitializeAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public virtual ValueTask DisposeAsync() | ||
public virtual Task DisposeAsync() | ||
{ | ||
foreach (var disposable in _disposables) | ||
{ | ||
try | ||
{ | ||
disposable.Dispose(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger?.LogError(ex, "Error disposing resource."); | ||
} | ||
} | ||
|
||
return new ValueTask(Task.CompletedTask); | ||
Fixture.TestLogger.Clear(); | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters