Skip to content

Commit

Permalink
Updated logger tests to have a new method for creating the logger fac…
Browse files Browse the repository at this point in the history
…tory
  • Loading branch information
david-driscoll committed Oct 27, 2019
1 parent ba5947c commit 15ba2df
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/Testing.FakeItEasy/AutoFakeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ protected virtual void BuildContainer(ContainerBuilder cb)
{
}

/// <summary>
/// Control the way that the serilog logger factory is created.
/// </summary>
protected override ILoggerFactory CreateLoggerFactory(Serilog.ILogger logger, LoggerProviderCollection loggerProviderCollection)
{
var factory = new FakeItEasyLoggerFactory(new SerilogLoggerFactory(logger, false, loggerProviderCollection));
return A.Fake<ILoggerFactory>(l => l.Wrapping(factory));
}

private void SetupContainer(ContainerBuilder cb)
{
if (_serviceCollection != null)
Expand Down
10 changes: 9 additions & 1 deletion src/Testing.FakeItEasy/FakeItEasyLoggerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FakeItEasy;
using Microsoft.Extensions.Logging;
using Serilog.Extensions.Logging;

namespace Rocket.Surgery.Extensions.Testing
{
Expand All @@ -8,9 +9,16 @@ namespace Rocket.Surgery.Extensions.Testing
/// </summary>
class FakeItEasyLoggerFactory : LoggerFactory, ILoggerFactory
{
private readonly ILoggerFactory _factory;

public FakeItEasyLoggerFactory(ILoggerFactory factory)
{
_factory = factory;
}

ILogger ILoggerFactory.CreateLogger(string categoryName)
{
var logger = CreateLogger(categoryName);
var logger = _factory.CreateLogger(categoryName);
return A.Fake<ILogger>(x => x.Wrapping(logger));
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/Testing/LoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected LoggerTest(ITestOutputHelper outputHelper, LogEventLevel minLevel, str
var logger = config.CreateLogger();

var loggerProviderCollection = new LoggerProviderCollection();
var factory = new SerilogLoggerFactory(logger, false, loggerProviderCollection);
var factory = CreateLoggerFactory(logger, loggerProviderCollection);
var container = new ServiceCollection().AddLogging().AddSingleton(factory).BuildServiceProvider();
Disposable.Add(container);
Disposable.Add(logger);
Expand All @@ -103,6 +103,14 @@ protected LoggerTest(ITestOutputHelper outputHelper, LogEventLevel minLevel, str
});
}

/// <summary>
/// Control the way that the serilog logger factory is created.
/// </summary>
protected virtual ILoggerFactory CreateLoggerFactory(ISeriLogger logger, LoggerProviderCollection loggerProviderCollection)
{
return new SerilogLoggerFactory(logger, false, loggerProviderCollection);
}

void IDisposable.Dispose()
{
Disposable.Dispose();
Expand Down

0 comments on commit 15ba2df

Please sign in to comment.