Skip to content

Commit

Permalink
Merge pull request #64 from JSkimming/register-registered-logger
Browse files Browse the repository at this point in the history
Ensure RegisteredLogger is registred in all required instances
  • Loading branch information
nblumhardt authored Jul 18, 2022
2 parents abae8aa + 2b5895c commit 30be557
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public static IHostBuilder UseSerilog(
{
// This won't (and shouldn't) take ownership of the logger.
collection.AddSingleton(logger);

// Still need to use RegisteredLogger as it is used by ConfigureDiagnosticContext.
collection.AddSingleton(new RegisteredLogger(logger));
}
bool useRegisteredLogger = logger != null;
ConfigureDiagnosticContext(collection, useRegisteredLogger);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Xunit;

namespace Serilog.Extensions.Hosting.Tests
{
public class SerilogHostBuilderExtensionsTests
{
[Fact]
public void ServicesAreRegisteredWhenCallingUseSerilog()
{
// Arrange
var collection = new ServiceCollection();
IHostBuilder builder = new FakeHostBuilder(collection);

// Act
builder.UseSerilog();

// Assert
IServiceProvider provider = collection.BuildServiceProvider();
provider.GetRequiredService<ILoggerFactory>();
provider.GetRequiredService<IDiagnosticContext>();
}

[Fact]
public void ServicesAreRegisteredWhenCallingUseSerilogWithLogger()
{
// Arrange
var collection = new ServiceCollection();
IHostBuilder builder = new FakeHostBuilder(collection);
ILogger logger = new LoggerConfiguration().CreateLogger();

// Act
builder.UseSerilog(logger);

// Assert
IServiceProvider provider = collection.BuildServiceProvider();
provider.GetRequiredService<ILogger>();
provider.GetRequiredService<ILoggerFactory>();
provider.GetRequiredService<IDiagnosticContext>();
}

[Fact]
public void ServicesAreRegisteredWhenCallingUseSerilogWithConfigureDelegate()
{
// Arrange
var collection = new ServiceCollection();
IHostBuilder builder = new FakeHostBuilder(collection);

// Act
builder.UseSerilog((_, _) => { });

// Assert
IServiceProvider provider = collection.BuildServiceProvider();
provider.GetRequiredService<ILogger>();
provider.GetRequiredService<ILoggerFactory>();
provider.GetRequiredService<IDiagnosticContext>();
}

private class FakeHostBuilder : IHostBuilder
{
private readonly IServiceCollection _collection;

public FakeHostBuilder(IServiceCollection collection) => _collection = collection;

public IHostBuilder ConfigureHostConfiguration(Action<IConfigurationBuilder> configureDelegate)
{
throw new NotImplementedException();
}

public IHostBuilder ConfigureAppConfiguration(Action<HostBuilderContext, IConfigurationBuilder> configureDelegate)
{
throw new NotImplementedException();
}

public IHostBuilder ConfigureServices(Action<HostBuilderContext, IServiceCollection> configureDelegate)
{
configureDelegate(null, _collection);
return this;
}

public IHostBuilder UseServiceProviderFactory<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory)
{
throw new NotImplementedException();
}

public IHostBuilder UseServiceProviderFactory<TContainerBuilder>(Func<HostBuilderContext, IServiceProviderFactory<TContainerBuilder>> factory)
{
throw new NotImplementedException();
}

public IHostBuilder ConfigureContainer<TContainerBuilder>(Action<HostBuilderContext, TContainerBuilder> configureDelegate)
{
throw new NotImplementedException();
}

public IHost Build()
{
throw new NotImplementedException();
}

public IDictionary<object, object> Properties { get; }
}
}
}

0 comments on commit 30be557

Please sign in to comment.