Skip to content

Commit

Permalink
Dropped old logger, replaced with serilog to help with filtering in a…
Browse files Browse the repository at this point in the history
… standard way +semver:major
  • Loading branch information
david-driscoll committed Oct 22, 2019
1 parent d62b6d3 commit 39e52cd
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 214 deletions.
5 changes: 5 additions & 0 deletions Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
<GlobalPackageReference Include="Roslynator.Analyzers" Version="2.1.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
<PackageReference Update="Microsoft.Extensions.Logging" Version="2.1.0" />
<PackageReference Update="Microsoft.Extensions.DiagnosticAdapter" Version="2.1.0" />
<PackageReference Update="System.Collections.Immutable" Version="1.6.0" />
<PackageReference Update="System.Diagnostics.DiagnosticSource" Version="4.5.1" />
<PackageReference Update="System.Interactive" Version="4.0.0" />
<PackageReference Update="System.Reactive" Version="4.2.0" />
<PackageReference Update="Serilog" Version="2.9.0" />
<PackageReference Update="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Update="Serilog.Sinks.XUnit" Version="1.0.21" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'!='netstandard2.0' and '$(TargetFramework)'!='netcoreapp2.1'">
<PackageReference Update="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageReference Update="Microsoft.Extensions.Logging" Version="3.0.0" />
<PackageReference Update="Microsoft.Extensions.DiagnosticAdapter" Version="3.0.0" />
<PackageReference Update="System.Diagnostics.DiagnosticSource" Version="4.6.0" />
Expand Down
33 changes: 30 additions & 3 deletions src/Testing.FakeItEasy/AutoFakeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Serilog.Extensions.Logging;
using Xunit.Abstractions;

namespace Rocket.Surgery.Extensions.Testing
Expand Down Expand Up @@ -34,18 +37,42 @@ public abstract class AutoFakeTest : LoggerTest
protected AutoFake Fake => _autoFake.Value;

/// <summary>
/// Create the auto test class
/// The default constructor with available logging level
/// </summary>
/// <param name="outputHelper"></param>
/// <param name="minLevel"></param>
protected AutoFakeTest(ITestOutputHelper outputHelper, LogLevel minLevel = LogLevel.Information) : base(outputHelper, minLevel, A.Fake<ILoggerFactory>(x => x.Wrapping(new FakeItEasyLoggerFactory())))
/// <param name="logFormat"></param>
/// <param name="configureLogger"></param>
protected AutoFakeTest(ITestOutputHelper outputHelper, string logFormat = "[{Timestamp:HH:mm:ss} {Level:w4}] {Message}{NewLine}{Exception}", Action<LoggerConfiguration>? configureLogger = null) : this(outputHelper, LogEventLevel.Information, logFormat, configureLogger)
{
}

/// <summary>
/// The default constructor with available logging level
/// </summary>
/// <param name="outputHelper"></param>
/// <param name="minLevel"></param>
/// <param name="logFormat"></param>
/// <param name="configureLogger"></param>
protected AutoFakeTest(ITestOutputHelper outputHelper, LogLevel minLevel, string logFormat = "[{Timestamp:HH:mm:ss} {Level:w4}] {Message}{NewLine}{Exception}", Action<LoggerConfiguration>? configureLogger = null) : this(outputHelper, LevelConvert.ToSerilogLevel(minLevel), logFormat, configureLogger)
{
}

/// <summary>
/// The default constructor with available logging level
/// </summary>
/// <param name="outputHelper"></param>
/// <param name="minLevel"></param>
/// <param name="logFormat"></param>
/// <param name="configureLogger"></param>
protected AutoFakeTest(ITestOutputHelper outputHelper, LogEventLevel minLevel, string logFormat = "[{Timestamp:HH:mm:ss} {Level:w4}] {Message}{NewLine}{Exception}", Action<LoggerConfiguration>? configureLogger = null) : base(outputHelper, minLevel, logFormat, configureLogger)
{
_autoFake = new Lazy<AutoFake>(() =>
{
var cb = new ContainerBuilder();
SetupContainer(cb);
var af = new AutoFake(builder: cb);
af.Container.ComponentRegistry.AddRegistrationSource(new LoggingRegistrationSource(LoggerFactory, Logger));
af.Container.ComponentRegistry.AddRegistrationSource(new LoggingRegistrationSource(LoggerFactory, Logger, SerilogLogger));
return af;
});
}
Expand Down
33 changes: 30 additions & 3 deletions src/Testing.Moq/AutoMockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using Serilog;
using Serilog.Events;
using Serilog.Extensions.Logging;
using Xunit.Abstractions;

namespace Rocket.Surgery.Extensions.Testing
Expand Down Expand Up @@ -34,17 +37,41 @@ public abstract class AutoMockTest : LoggerTest
protected AutoMock Moq => _autoMoq.Value;

/// <summary>
/// Create the auto test class
/// The default constructor with available logging level
/// </summary>
/// <param name="outputHelper"></param>
/// <param name="mockBehavior"></param>
/// <param name="logFormat"></param>
/// <param name="configureLogger"></param>
protected AutoMockTest(ITestOutputHelper outputHelper, MockBehavior mockBehavior = MockBehavior.Default, string logFormat = "[{Timestamp:HH:mm:ss} {Level:w4}] {Message}{NewLine}{Exception}", Action<LoggerConfiguration>? configureLogger = null) : this(outputHelper, LogEventLevel.Information, mockBehavior, logFormat, configureLogger)
{
}
/// <summary>
/// The default constructor with available logging level
/// </summary>
/// <param name="outputHelper"></param>
/// <param name="minLevel"></param>
/// <param name="mockBehavior"></param>
/// <param name="logFormat"></param>
/// <param name="configureLogger"></param>
protected AutoMockTest(ITestOutputHelper outputHelper, LogLevel minLevel, MockBehavior mockBehavior = MockBehavior.Default, string logFormat = "[{Timestamp:HH:mm:ss} {Level:w4}] {Message}{NewLine}{Exception}", Action<LoggerConfiguration>? configureLogger = null) : this(outputHelper, LevelConvert.ToSerilogLevel(minLevel), mockBehavior, logFormat, configureLogger)
{
}

/// <summary>
/// The default constructor with available logging level
/// </summary>
/// <param name="outputHelper"></param>
/// <param name="minLevel"></param>
protected AutoMockTest(ITestOutputHelper outputHelper, MockBehavior mockBehavior = MockBehavior.Default, LogLevel minLevel = LogLevel.Information) : base(outputHelper, minLevel, new LoggerFactory())
/// <param name="mockBehavior"></param>
/// <param name="logFormat"></param>
/// <param name="configureLogger"></param>
protected AutoMockTest(ITestOutputHelper outputHelper, LogEventLevel minLevel, MockBehavior mockBehavior = MockBehavior.Default, string logFormat = "[{Timestamp:HH:mm:ss} {Level:w4}] {Message}{NewLine}{Exception}", Action<LoggerConfiguration>? configureLogger = null) : base(outputHelper, minLevel, logFormat, configureLogger)
{
_autoMoq = new Lazy<AutoMock>(() =>
{
var af = AutoMock.GetFromRepository(new MockRepository(mockBehavior), SetupContainer);
af.Container.ComponentRegistry.AddRegistrationSource(new LoggingRegistrationSource(LoggerFactory, Logger));
af.Container.ComponentRegistry.AddRegistrationSource(new LoggingRegistrationSource(LoggerFactory, Logger, SerilogLogger));
return af;
});
}
Expand Down
113 changes: 113 additions & 0 deletions src/Testing/LoggerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog.Extensions.Logging;
using Xunit.Abstractions;
using System.Reactive.Disposables;
using Serilog;
using Serilog.Events;
using IMsftLogger = Microsoft.Extensions.Logging.ILogger;
using ISeriLogger = Serilog.ILogger;

namespace Rocket.Surgery.Extensions.Testing
{
/// <summary>
/// A simple base test class with logger, logger factory and diagnostic source all wired into the <see cref="ITestOutputHelper" />.
/// </summary>
public abstract class LoggerTest : IDisposable
{
private readonly Lazy<(IMsftLogger logger, ILoggerFactory loggerFactory, ISeriLogger serilogLogger, DiagnosticSource diagnosticSource)> _values;

/// <summary>
/// The <see cref="ILoggerFactory" />
/// </summary>
protected ILoggerFactory LoggerFactory => _values.Value.loggerFactory;

/// <summary>
/// The <see cref="Microsoft.Extensions.Logging.ILogger" />
/// </summary>
protected IMsftLogger Logger => _values.Value.logger;

/// <summary>
/// The <see cref="Microsoft.Extensions.Logging.ILogger" />
/// </summary>
protected ISeriLogger SerilogLogger => _values.Value.serilogLogger;

/// <summary>
/// The <see cref="DiagnosticSource" />
/// </summary>
protected DiagnosticSource DiagnosticSource => _values.Value.diagnosticSource;

/// <summary>
/// The <see cref="CompositeDisposable" />
/// </summary>
protected readonly CompositeDisposable Disposable;

/// <summary>
/// The default constructor with available logging level
/// </summary>
/// <param name="outputHelper"></param>
/// <param name="minLevel"></param>
/// <param name="logFormat"></param>
/// <param name="configureLogger"></param>
/// <returns></returns>
protected LoggerTest(ITestOutputHelper outputHelper, string logFormat = "[{Timestamp:HH:mm:ss} {Level:w4}] {Message}{NewLine}{Exception}", Action<LoggerConfiguration>? configureLogger = null)
: this(outputHelper, LogEventLevel.Information, logFormat, configureLogger)
{
}

/// <summary>
/// The default constructor with available logging level
/// </summary>
/// <param name="outputHelper"></param>
/// <param name="minLevel"></param>
/// <param name="logFormat"></param>
/// <param name="configureLogger"></param>
/// <returns></returns>
protected LoggerTest(ITestOutputHelper outputHelper, LogLevel minLevel, string logFormat = "[{Timestamp:HH:mm:ss} {Level:w4}] {Message}{NewLine}{Exception}", Action<LoggerConfiguration>? configureLogger = null)
: this(outputHelper, LevelConvert.ToSerilogLevel(minLevel), logFormat, configureLogger)
{
}

/// <summary>
/// The default constructor with available logging level
/// </summary>
/// <param name="outputHelper"></param>
/// <param name="minLevel"></param>
/// <param name="logFormat"></param>
/// <param name="configureLogger"></param>
/// <returns></returns>
protected LoggerTest(ITestOutputHelper outputHelper, LogEventLevel minLevel, string logFormat = "[{Timestamp:HH:mm:ss} {Level:w4}] {Message}{NewLine}{Exception}", Action<LoggerConfiguration>? configureLogger = null)
{
Disposable = new CompositeDisposable();

_values = new Lazy<(IMsftLogger logger, ILoggerFactory loggerFactory, ISeriLogger serilogLogger, DiagnosticSource diagnosticSource)>(() =>
{
var config = new LoggerConfiguration()
.WriteTo.TestOutput(outputHelper, LogEventLevel.Verbose, logFormat)
.MinimumLevel.Is(minLevel);
configureLogger?.Invoke(config);
var logger = config.CreateLogger();

var loggerProviderCollection = new LoggerProviderCollection();
var factory = new SerilogLoggerFactory(logger, false, loggerProviderCollection);
var container = new ServiceCollection().AddLogging().AddSingleton(factory).BuildServiceProvider();
Disposable.Add(container);
Disposable.Add(logger);

var diagnosticListener = new DiagnosticListener("Test");
var defaultLogger = factory.CreateLogger("Default");
Disposable.Add(diagnosticListener.SubscribeWithAdapter(new TestDiagnosticListenerLoggingAdapter(defaultLogger)));

return (defaultLogger, factory, logger, diagnosticListener);
});
}

void IDisposable.Dispose()
{
Disposable.Dispose();
}
}
}

22 changes: 19 additions & 3 deletions src/Testing/LoggingRegistrationSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@
using Autofac.Core.Lifetime;
using Autofac.Core.Registration;
using Microsoft.Extensions.Logging;
using IMsftLogger = Microsoft.Extensions.Logging.ILogger;
using ISeriLogger = Serilog.ILogger;

namespace Rocket.Surgery.Extensions.Testing
{
class LoggingRegistrationSource : IRegistrationSource
{
private readonly ILoggerFactory _loggerFactory;
private readonly ILogger _defaultLogger;
private readonly ISeriLogger _serilogLogger;
private readonly IMsftLogger _defaultLogger;

public LoggingRegistrationSource(ILoggerFactory loggerFactory, ILogger defaultLogger)
public LoggingRegistrationSource(ILoggerFactory loggerFactory, IMsftLogger defaultLogger, ISeriLogger serilogLogger)
{
_loggerFactory = loggerFactory;
_serilogLogger = serilogLogger;
_defaultLogger = defaultLogger;
}

Expand All @@ -32,7 +36,7 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
yield return registration.CreateRegistration();
}

if (typedService.ServiceType == typeof(ILogger))
if (typedService.ServiceType == typeof(IMsftLogger))
{
yield return new ComponentRegistration(
Guid.NewGuid(),
Expand All @@ -44,6 +48,18 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
new Dictionary<string, object>());
}

if (typedService.ServiceType == typeof(ISeriLogger))
{
yield return new ComponentRegistration(
Guid.NewGuid(),
new ProvidedInstanceActivator(_serilogLogger),
new RootScopeLifetime(),
InstanceSharing.Shared,
InstanceOwnership.OwnedByLifetimeScope,
new[] { service },
new Dictionary<string, object>());
}

if (typedService.ServiceType == typeof(ILoggerFactory))
{
yield return new ComponentRegistration(
Expand Down
4 changes: 4 additions & 0 deletions src/Testing/Rocket.Surgery.Extensions.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<PackageReference Include="Autofac.Extensions.DependencyInjection" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.analyzers" />
<PackageReference Include="Serilog" />
<PackageReference Include="Serilog.Extensions.Logging" />
<PackageReference Include="Serilog.Sinks.XUnit" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" />
<PackageReference Include="System.Collections.Immutable" />
Expand Down
67 changes: 0 additions & 67 deletions src/Testing/TestBase.cs

This file was deleted.

Loading

0 comments on commit 39e52cd

Please sign in to comment.