React to logging in DI changes (#1089)

This commit is contained in:
Pavel Krymets 2017-06-02 08:33:37 -07:00 committed by GitHub
parent 140cfc21bd
commit 0f0c88b8cd
8 changed files with 23 additions and 314 deletions

View File

@ -29,20 +29,6 @@ namespace Microsoft.AspNetCore.Hosting
/// </remarks>
IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate);
/// <summary>
/// Adds a delegate for configuring the provided <see cref="ILoggerFactory"/>. This may be called multiple times.
/// </summary>
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggerFactory"/>.</param>
/// <typeparam name="T">
/// The type of <see cref="ILoggerFactory"/> to configure.
/// The delegate will not execute if the type provided does not match the <see cref="ILoggerFactory"/> used by the <see cref="IWebHostBuilder"/>.
/// </typeparam>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
/// <remarks>
/// The <see cref="ILoggerFactory"/> on the <see cref="WebHostBuilderContext"/> is uninitialized at this stage.
/// </remarks>
IWebHostBuilder ConfigureLogging<T>(Action<WebHostBuilderContext, T> configureLogging) where T : ILoggerFactory;
/// <summary>
/// Adds a delegate for configuring additional services for the host or web application. This may be called
/// multiple times.
@ -73,23 +59,5 @@ namespace Microsoft.AspNetCore.Hosting
/// <param name="value">The value of the setting to add or replace.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder UseSetting(string key, string value);
/// <summary>
/// Specify the <see cref="ILoggerFactory"/> to be used by the web host.
/// </summary>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to be used.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder UseLoggerFactory(ILoggerFactory loggerFactory);
/// <summary>
/// Adds a delegate to construct the <see cref="ILoggerFactory"/> that will be registered
/// as a singleton and used by the application.
/// </summary>
/// <param name="createLoggerFactory">The delegate that constructs an <see cref="IConfigurationBuilder" /></param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
/// <remarks>
/// The <see cref="ILoggerFactory"/> on the <see cref="WebHostBuilderContext"/> is uninitialized at this stage.
/// </remarks>
IWebHostBuilder UseLoggerFactory(Func<WebHostBuilderContext, ILoggerFactory> createLoggerFactory);
}
}

View File

@ -20,10 +20,5 @@ namespace Microsoft.AspNetCore.Hosting
/// The <see cref="IConfiguration" /> containing the merged configuration of the application and the <see cref="IWebHost" />.
/// </summary>
public IConfiguration Configuration { get; set; }
/// <summary>
/// The <see cref="ILoggerFactory" /> configured on the <see cref="IWebHost" />.
/// </summary>
public ILoggerFactory LoggerFactory { get; set; }
}
}

View File

@ -9,21 +9,11 @@
"MemberId": "Microsoft.AspNetCore.Hosting.IWebHostBuilder ConfigureAppConfiguration(System.Action<Microsoft.AspNetCore.Hosting.WebHostBuilderContext, Microsoft.Extensions.Configuration.IConfigurationBuilder> configureDelegate)",
"Kind": "Addition"
},
{
"TypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"MemberId": "Microsoft.AspNetCore.Hosting.IWebHostBuilder ConfigureLogging<T0>(System.Action<Microsoft.AspNetCore.Hosting.WebHostBuilderContext, T0> configureLogging) where T0 : Microsoft.Extensions.Logging.ILoggerFactory",
"Kind": "Addition"
},
{
"TypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"MemberId": "Microsoft.AspNetCore.Hosting.IWebHostBuilder ConfigureServices(System.Action<Microsoft.AspNetCore.Hosting.WebHostBuilderContext, Microsoft.Extensions.DependencyInjection.IServiceCollection> configureServices)",
"Kind": "Addition"
},
{
"TypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"MemberId": "Microsoft.AspNetCore.Hosting.IWebHostBuilder UseLoggerFactory(System.Func<Microsoft.AspNetCore.Hosting.WebHostBuilderContext, Microsoft.Extensions.Logging.ILoggerFactory> createLoggerFactory)",
"Kind": "Addition"
},
{
"TypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHost : System.IDisposable",
"MemberId": "System.Threading.Tasks.Task StartAsync(System.Threading.CancellationToken cancellationToken)",
@ -38,5 +28,10 @@
"TypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"MemberId": "Microsoft.AspNetCore.Hosting.IWebHostBuilder ConfigureLogging(System.Action<Microsoft.Extensions.Logging.ILoggerFactory> configureLogging)",
"Kind": "Removal"
}
]
},
{
"TypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"MemberId": "Microsoft.AspNetCore.Hosting.IWebHostBuilder UseLoggerFactory(Microsoft.Extensions.Logging.ILoggerFactory loggerFactory)",
"Kind": "Removal"
}
]

View File

@ -26,13 +26,11 @@ namespace Microsoft.AspNetCore.Hosting
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly List<Action<WebHostBuilderContext, IServiceCollection>> _configureServicesDelegates;
private readonly List<Action<WebHostBuilderContext, ILoggerFactory>> _configureLoggingDelegates;
private IConfiguration _config;
private WebHostOptions _options;
private WebHostBuilderContext _context;
private bool _webHostBuilt;
private Func<WebHostBuilderContext, ILoggerFactory> _createLoggerFactoryDelegate;
private List<Action<WebHostBuilderContext, IConfigurationBuilder>> _configureAppConfigurationBuilderDelegates;
/// <summary>
@ -42,7 +40,6 @@ namespace Microsoft.AspNetCore.Hosting
{
_hostingEnvironment = new HostingEnvironment();
_configureServicesDelegates = new List<Action<WebHostBuilderContext, IServiceCollection>>();
_configureLoggingDelegates = new List<Action<WebHostBuilderContext, ILoggerFactory>>();
_configureAppConfigurationBuilderDelegates = new List<Action<WebHostBuilderContext, IConfigurationBuilder>>();
_config = new ConfigurationBuilder()
@ -90,41 +87,6 @@ namespace Microsoft.AspNetCore.Hosting
return this;
}
/// <summary>
/// Specify the <see cref="ILoggerFactory"/> to be used by the web host.
/// </summary>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to be used.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public IWebHostBuilder UseLoggerFactory(ILoggerFactory loggerFactory)
{
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
return UseLoggerFactory(_ => loggerFactory);
}
/// <summary>
/// Adds a delegate to construct the <see cref="ILoggerFactory"/> that will be registered
/// as a singleton and used by the application.
/// </summary>
/// <param name="createLoggerFactory">The delegate that constructs an <see cref="IConfigurationBuilder" /></param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
/// <remarks>
/// The <see cref="ILoggerFactory"/> on the <see cref="WebHostBuilderContext"/> is uninitialized at this stage.
/// </remarks>
public IWebHostBuilder UseLoggerFactory(Func<WebHostBuilderContext, ILoggerFactory> createLoggerFactory)
{
if (createLoggerFactory == null)
{
throw new ArgumentNullException(nameof(createLoggerFactory));
}
_createLoggerFactoryDelegate = createLoggerFactory;
return this;
}
/// <summary>
/// Adds a delegate for configuring additional services for the host or web application. This may be called
/// multiple times.
@ -158,35 +120,6 @@ namespace Microsoft.AspNetCore.Hosting
return this;
}
/// <summary>
/// Adds a delegate for configuring the provided <see cref="ILoggerFactory"/>. This may be called multiple times.
/// </summary>
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggerFactory"/>.</param>
/// <typeparam name="T">
/// The type of <see cref="ILoggerFactory"/> to configure.
/// The delegate will not execute if the type provided does not match the <see cref="ILoggerFactory"/> used by the <see cref="IWebHostBuilder"/>.
/// </typeparam>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
/// <remarks>
/// The <see cref="ILoggerFactory"/> on the <see cref="WebHostBuilderContext"/> is uninitialized at this stage.
/// </remarks>
public IWebHostBuilder ConfigureLogging<T>(Action<WebHostBuilderContext, T> configureLogging) where T : ILoggerFactory
{
if (configureLogging == null)
{
throw new ArgumentNullException(nameof(configureLogging));
}
_configureLoggingDelegates.Add((context, factory) =>
{
if (factory is T typedFactory)
{
configureLogging(context, typedFactory);
}
});
return this;
}
/// <summary>
/// Adds a delegate for configuring the <see cref="IConfigurationBuilder"/> that will construct an <see cref="IConfiguration"/>.
/// </summary>
@ -313,19 +246,6 @@ namespace Microsoft.AspNetCore.Hosting
services.AddSingleton<IConfiguration>(configuration);
_context.Configuration = configuration;
// The configured ILoggerFactory is added as a singleton here. AddLogging below will not add an additional one.
var loggerFactory = _createLoggerFactoryDelegate?.Invoke(_context) ?? new LoggerFactory();
services.AddSingleton(loggerFactory);
_context.LoggerFactory = loggerFactory;
foreach (var configureLogging in _configureLoggingDelegates)
{
configureLogging(_context, loggerFactory);
}
//This is required to add ILogger of T.
services.AddLogging();
var listener = new DiagnosticListener("Microsoft.AspNetCore");
services.AddSingleton<DiagnosticListener>(listener);
services.AddSingleton<DiagnosticSource>(listener);
@ -334,6 +254,7 @@ namespace Microsoft.AspNetCore.Hosting
services.AddTransient<IHttpContextFactory, HttpContextFactory>();
services.AddScoped<IMiddlewareFactory, MiddlewareFactory>();
services.AddOptions();
services.AddLogging();
// Conjure up a RequestServices
services.AddTransient<IStartupFilter, AutoRequestServicesStartupFilter>();
@ -387,9 +308,6 @@ namespace Microsoft.AspNetCore.Hosting
// can still manage their lifetime (disposal) shared instances with application services.
// NOTE: This code overrides original services lifetime. Instances would always be singleton in
// application container.
var loggerFactory = hostingServiceProvider.GetService<ILoggerFactory>();
services.Replace(ServiceDescriptor.Singleton(typeof(ILoggerFactory), loggerFactory));
var listener = hostingServiceProvider.GetService<DiagnosticListener>();
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticListener), listener));
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticSource), listener));

View File

@ -111,11 +111,11 @@ namespace Microsoft.AspNetCore.Hosting
/// Adds a delegate for configuring the provided <see cref="LoggerFactory"/>. This may be called multiple times.
/// </summary>
/// <param name="hostBuilder">The <see cref="IWebHostBuilder" /> to configure.</param>
/// <param name="configureLogging">The delegate that configures the <see cref="LoggerFactory"/>.</param>
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggingBuilder"/>.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action<LoggerFactory> configureLogging)
public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action<ILoggingBuilder> configureLogging)
{
return hostBuilder.ConfigureLogging<LoggerFactory>((_, loggerFactory) => configureLogging(loggerFactory));
return hostBuilder.ConfigureServices(collection => collection.AddLogging(configureLogging));
}
/// <summary>
@ -124,24 +124,9 @@ namespace Microsoft.AspNetCore.Hosting
/// <param name="hostBuilder">The <see cref="IWebHostBuilder" /> to configure.</param>
/// <param name="configureLogging">The delegate that configures the <see cref="LoggerFactory"/>.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, LoggerFactory> configureLogging)
public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, ILoggingBuilder> configureLogging)
{
return hostBuilder.ConfigureLogging(configureLogging);
}
/// <summary>
/// Adds a delegate for configuring the provided <see cref="ILoggerFactory"/>. This may be called multiple times.
/// </summary>
/// <param name="hostBuilder">The <see cref="IWebHostBuilder" /> to configure.</param>
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggerFactory"/>.</param>
/// <typeparam name="T">
/// The type of <see cref="ILoggerFactory"/> to configure.
/// The delegate will not execute if the type provided does not match the <see cref="ILoggerFactory"/> used by the <see cref="IWebHostBuilder"/>.
/// </typeparam>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder ConfigureLogging<T>(this IWebHostBuilder hostBuilder, Action<T> configureLogging) where T : ILoggerFactory
{
return hostBuilder.ConfigureLogging<T>((_, factory) => configureLogging(factory));
return hostBuilder.ConfigureServices((context, collection) => collection.AddLogging(builder => configureLogging(context, builder)));
}
}
}
}

View File

@ -12,5 +12,10 @@
"TypeId": "public static class Microsoft.AspNetCore.Hosting.WebHostExtensions",
"MemberId": "public static System.Void Run(this Microsoft.AspNetCore.Hosting.IWebHost host, System.Threading.CancellationToken token)",
"Kind": "Removal"
},
{
"TypeId": "public class Microsoft.AspNetCore.Hosting.WebHostBuilder : Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"MemberId": "public Microsoft.AspNetCore.Hosting.IWebHostBuilder UseLoggerFactory(Microsoft.Extensions.Logging.ILoggerFactory loggerFactory)",
"Kind": "Removal"
}
]
]

View File

@ -7,7 +7,9 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
namespace ServerComparison.TestSites
{
@ -25,7 +27,7 @@ namespace ServerComparison.TestSites
.ConfigureLogging((_, factory) =>
{
factory.AddConsole();
factory.AddFilter("Console", level => level >= LogLevel.Warning);
factory.AddFilter<ConsoleLoggerProvider>(level => level >= LogLevel.Warning);
})
.UseStartup("Microsoft.AspNetCore.Hosting.TestSites");

View File

@ -205,7 +205,6 @@ namespace Microsoft.AspNetCore.Hosting
{
Assert.NotNull(context.HostingEnvironment);
Assert.NotNull(context.Configuration);
Assert.NotNull(context.LoggerFactory);
configurationCallbackCalled = true;
options.ValidateScopes = true;
});
@ -214,22 +213,6 @@ namespace Microsoft.AspNetCore.Hosting
Assert.True(configurationCallbackCalled);
}
[Fact]
public void UseLoggerFactoryHonored()
{
var loggerFactory = new LoggerFactory();
var hostBuilder = new WebHostBuilder()
.UseLoggerFactory(loggerFactory)
.UseServer(new TestServer())
.UseStartup<StartupNoServices>();
using (var host = (WebHost)hostBuilder.Build())
{
Assert.Same(loggerFactory, host.Services.GetService<ILoggerFactory>());
}
}
[Fact]
public void MultipleConfigureLoggingInvokedInOrder()
{
@ -252,46 +235,6 @@ namespace Microsoft.AspNetCore.Hosting
}
}
[Fact]
public void UseLoggerFactoryDelegateIsHonored()
{
var loggerFactory = new LoggerFactory();
var hostBuilder = new WebHostBuilder()
.UseLoggerFactory(_ => loggerFactory)
.UseServer(new TestServer())
.UseStartup<StartupNoServices>();
using (var host = (WebHost)hostBuilder.Build())
{
Assert.Same(loggerFactory, host.Services.GetService<ILoggerFactory>());
}
}
[Fact]
public void UseLoggerFactoryFuncAndConfigureLoggingCompose()
{
var callCount = 0; //Verify that multiple configureLogging calls still compose correctly.
var loggerFactory = new LoggerFactory();
var hostBuilder = new WebHostBuilder()
.UseLoggerFactory(_ => loggerFactory)
.ConfigureLogging(factory =>
{
Assert.Equal(0, callCount++);
})
.ConfigureLogging(factory =>
{
Assert.Equal(1, callCount++);
})
.UseServer(new TestServer())
.UseStartup<StartupNoServices>();
using (var host = (WebHost)hostBuilder.Build())
{
Assert.Equal(2, callCount);
Assert.Same(loggerFactory, host.Services.GetService<ILoggerFactory>());
}
}
[Fact]
public void HostingContextContainsAppConfigurationDuringConfigureLogging()
{
@ -332,62 +275,6 @@ namespace Microsoft.AspNetCore.Hosting
using (hostBuilder.Build()) { }
}
[Fact]
public void ConfigureLoggingCalledIfLoggerFactoryTypeMatches()
{
var callCount = 0;
var hostBuilder = new WebHostBuilder()
.UseLoggerFactory(_ => new SubLoggerFactory())
.ConfigureLogging<CustomLoggerFactory>(factory =>
{
Assert.Equal(0, callCount++);
})
.UseServer(new TestServer())
.UseStartup<StartupNoServices>();
using (hostBuilder.Build())
{
Assert.Equal(1, callCount);
}
}
[Fact]
public void ConfigureLoggingNotCalledIfLoggerFactoryTypeDoesNotMatches()
{
var callCount = 0;
var hostBuilder = new WebHostBuilder()
.UseLoggerFactory(_ => new NonSubLoggerFactory())
.ConfigureLogging<CustomLoggerFactory>(factory =>
{
Assert.Equal(0, callCount++);
})
.UseServer(new TestServer())
.UseStartup<StartupNoServices>();
using (hostBuilder.Build())
{
Assert.Equal(0, callCount);
}
}
[Fact]
public void CanUseCustomLoggerFactory()
{
var hostBuilder = new WebHostBuilder()
.UseLoggerFactory(_ => new CustomLoggerFactory())
.ConfigureLogging<CustomLoggerFactory>(factory =>
{
factory.CustomConfigureMethod();
})
.UseServer(new TestServer())
.UseStartup<StartupNoServices>();
using (var host = (WebHost)hostBuilder.Build())
{
Assert.IsType(typeof(CustomLoggerFactory), host.Services.GetService<ILoggerFactory>());
}
}
[Fact]
public void ThereIsAlwaysConfiguration()
{
@ -794,52 +681,6 @@ namespace Microsoft.AspNetCore.Hosting
}
}
[Fact]
public void Build_PassesSameAutoCreatedILoggerFactoryEverywhere()
{
var builder = CreateWebHostBuilder();
var server = new TestServer();
using (var host = builder.UseServer(server)
.UseStartup<StartupWithILoggerFactory>()
.Build())
{
var startup = host.Services.GetService<StartupWithILoggerFactory>();
Assert.Equal(startup.ConfigureLoggerFactory, startup.ConstructorLoggerFactory);
}
}
[Fact]
public void Build_PassesSamePassedILoggerFactoryEverywhere()
{
var factory = new LoggerFactory();
var builder = CreateWebHostBuilder();
var server = new TestServer();
using (var host = builder.UseServer(server)
.UseLoggerFactory(factory)
.UseStartup<StartupWithILoggerFactory>()
.Build())
{
var startup = host.Services.GetService<StartupWithILoggerFactory>();
Assert.Equal(factory, startup.ConfigureLoggerFactory);
Assert.Equal(factory, startup.ConstructorLoggerFactory);
}
}
[Fact]
public void Build_PassedILoggerFactoryNotDisposed()
{
var factory = new DisposableLoggerFactory();
var builder = CreateWebHostBuilder();
var server = new TestServer();
using (var host = builder.UseServer(server)
.UseLoggerFactory(factory)
.UseStartup<StartupWithILoggerFactory>()
.Build()) { }
Assert.False(factory.Disposed);
}
[Fact]
public void Build_DoesNotOverrideILoggerFactorySetByConfigureServices()
{