|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Microsoft.Extensions.Configuration; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Hosting; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Serilog.Extensions.Hosting.Tests |
| 10 | +{ |
| 11 | + public class SerilogHostBuilderExtensionsTests |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public void ServicesAreRegisteredWhenCallingUseSerilog() |
| 15 | + { |
| 16 | + // Arrange |
| 17 | + var collection = new ServiceCollection(); |
| 18 | + IHostBuilder builder = new FakeHostBuilder(collection); |
| 19 | + |
| 20 | + // Act |
| 21 | + builder.UseSerilog(); |
| 22 | + |
| 23 | + // Assert |
| 24 | + IServiceProvider provider = collection.BuildServiceProvider(); |
| 25 | + provider.GetRequiredService<ILoggerFactory>(); |
| 26 | + provider.GetRequiredService<IDiagnosticContext>(); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void ServicesAreRegisteredWhenCallingUseSerilogWithLogger() |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + var collection = new ServiceCollection(); |
| 34 | + IHostBuilder builder = new FakeHostBuilder(collection); |
| 35 | + ILogger logger = new LoggerConfiguration().CreateLogger(); |
| 36 | + |
| 37 | + // Act |
| 38 | + builder.UseSerilog(logger); |
| 39 | + |
| 40 | + // Assert |
| 41 | + IServiceProvider provider = collection.BuildServiceProvider(); |
| 42 | + provider.GetRequiredService<ILogger>(); |
| 43 | + provider.GetRequiredService<ILoggerFactory>(); |
| 44 | + provider.GetRequiredService<IDiagnosticContext>(); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void ServicesAreRegisteredWhenCallingUseSerilogWithConfigureDelegate() |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + var collection = new ServiceCollection(); |
| 52 | + IHostBuilder builder = new FakeHostBuilder(collection); |
| 53 | + |
| 54 | + // Act |
| 55 | + builder.UseSerilog((_, _) => { }); |
| 56 | + |
| 57 | + // Assert |
| 58 | + IServiceProvider provider = collection.BuildServiceProvider(); |
| 59 | + provider.GetRequiredService<ILogger>(); |
| 60 | + provider.GetRequiredService<ILoggerFactory>(); |
| 61 | + provider.GetRequiredService<IDiagnosticContext>(); |
| 62 | + } |
| 63 | + |
| 64 | + private class FakeHostBuilder : IHostBuilder |
| 65 | + { |
| 66 | + private readonly IServiceCollection _collection; |
| 67 | + |
| 68 | + public FakeHostBuilder(IServiceCollection collection) => _collection = collection; |
| 69 | + |
| 70 | + public IHostBuilder ConfigureHostConfiguration(Action<IConfigurationBuilder> configureDelegate) |
| 71 | + { |
| 72 | + throw new NotImplementedException(); |
| 73 | + } |
| 74 | + |
| 75 | + public IHostBuilder ConfigureAppConfiguration(Action<HostBuilderContext, IConfigurationBuilder> configureDelegate) |
| 76 | + { |
| 77 | + throw new NotImplementedException(); |
| 78 | + } |
| 79 | + |
| 80 | + public IHostBuilder ConfigureServices(Action<HostBuilderContext, IServiceCollection> configureDelegate) |
| 81 | + { |
| 82 | + configureDelegate(null, _collection); |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + public IHostBuilder UseServiceProviderFactory<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory) |
| 87 | + { |
| 88 | + throw new NotImplementedException(); |
| 89 | + } |
| 90 | + |
| 91 | + public IHostBuilder UseServiceProviderFactory<TContainerBuilder>(Func<HostBuilderContext, IServiceProviderFactory<TContainerBuilder>> factory) |
| 92 | + { |
| 93 | + throw new NotImplementedException(); |
| 94 | + } |
| 95 | + |
| 96 | + public IHostBuilder ConfigureContainer<TContainerBuilder>(Action<HostBuilderContext, TContainerBuilder> configureDelegate) |
| 97 | + { |
| 98 | + throw new NotImplementedException(); |
| 99 | + } |
| 100 | + |
| 101 | + public IHost Build() |
| 102 | + { |
| 103 | + throw new NotImplementedException(); |
| 104 | + } |
| 105 | + |
| 106 | + public IDictionary<object, object> Properties { get; } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments