Pass existing LoggerFactory into WebHostBuilder

This commit is contained in:
Christian Weiss 2016-03-19 21:49:03 +01:00 committed by John Luo
parent 22eab75005
commit 4b16e83a1f
4 changed files with 86 additions and 6 deletions

View File

@ -5,6 +5,7 @@ using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Hosting
{
@ -18,6 +19,13 @@ namespace Microsoft.AspNetCore.Hosting
/// </summary>
IWebHost Build();
/// <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>
/// Specify the <see cref="IServerFactory"/> to be used by the web host.
/// </summary>
@ -46,6 +54,13 @@ namespace Microsoft.AspNetCore.Hosting
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder Configure(Action<IApplicationBuilder> configureApplication);
/// <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>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder ConfigureLogging(Action<ILoggerFactory> configureLogging);
/// <summary>
/// Add or replace a setting in the configuration.
/// </summary>

View File

@ -18,7 +18,8 @@
"Microsoft.AspNetCore.Hosting.Server.Abstractions": "1.0.0-*",
"Microsoft.Extensions.Configuration.Abstractions": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-*",
"Microsoft.Extensions.FileProviders.Abstractions": "1.0.0-*"
"Microsoft.Extensions.FileProviders.Abstractions": "1.0.0-*",
"Microsoft.Extensions.Logging.Abstractions": "1.0.0-*"
},
"frameworks": {
"net451": {},

View File

@ -28,10 +28,11 @@ namespace Microsoft.AspNetCore.Hosting
public class WebHostBuilder : IWebHostBuilder
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly ILoggerFactory _loggerFactory;
private readonly List<Action<IServiceCollection>> _configureServicesDelegates;
private readonly List<Action<ILoggerFactory>> _configureLoggingDelegates;
private IConfiguration _config = new ConfigurationBuilder().AddInMemoryCollection().Build();
private ILoggerFactory _loggerFactory;
private WebHostOptions _options;
// Only one of these should be set
@ -41,11 +42,14 @@ namespace Microsoft.AspNetCore.Hosting
// Only one of these should be set
private IServerFactory _serverFactory;
/// <summary>
/// Initializes a new instance of the <see cref="WebHostBuilder"/> class.
/// </summary>
public WebHostBuilder()
{
_hostingEnvironment = new HostingEnvironment();
_loggerFactory = new LoggerFactory();
_configureServicesDelegates = new List<Action<IServiceCollection>>();
_configureLoggingDelegates = new List<Action<ILoggerFactory>>();
}
/// <summary>
@ -70,6 +74,22 @@ namespace Microsoft.AspNetCore.Hosting
return _config[key];
}
/// <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));
}
_loggerFactory = loggerFactory;
return this;
}
/// <summary>
/// Specify the <see cref="IServerFactory"/> to be used by the web host.
/// </summary>
@ -136,13 +156,18 @@ namespace Microsoft.AspNetCore.Hosting
}
/// <summary>
/// Configure the provided <see cref="ILoggerFactory"/> which will be available as a hosting service.
/// 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>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public IWebHostBuilder ConfigureLogging(Action<ILoggerFactory> configureLogging)
{
configureLogging(_loggerFactory);
if (configureLogging == null)
{
throw new ArgumentNullException(nameof(configureLogging));
}
_configureLoggingDelegates.Add(configureLogging);
return this;
}
@ -185,14 +210,25 @@ namespace Microsoft.AspNetCore.Hosting
var services = new ServiceCollection();
services.AddSingleton(_hostingEnvironment);
if (_loggerFactory == null)
{
_loggerFactory = new LoggerFactory();
}
foreach (var configureLogging in _configureLoggingDelegates)
{
configureLogging(_loggerFactory);
}
services.AddSingleton(_loggerFactory);
services.AddLogging();
services.AddTransient<IStartupLoader, StartupLoader>();
services.AddTransient<IServerLoader, ServerLoader>();
services.AddTransient<IApplicationBuilderFactory, ApplicationBuilderFactory>();
services.AddTransient<IHttpContextFactory, HttpContextFactory>();
services.AddLogging();
services.AddOptions();
var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");

View File

@ -13,6 +13,7 @@ using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.PlatformAbstractions;
using Xunit;
@ -141,6 +142,33 @@ namespace Microsoft.AspNetCore.Hosting
}
}
[Fact]
public void DefaultCreatesLoggerFactory()
{
var hostBuilder = new WebHostBuilder()
.UseServer(new TestServer())
.UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build();
Assert.NotNull(host.Services.GetService<ILoggerFactory>());
}
[Fact]
public void UseLoggerFactoryHonored()
{
var loggerFactory = new LoggerFactory();
var hostBuilder = new WebHostBuilder()
.UseLoggerFactory(loggerFactory)
.UseServer(new TestServer())
.UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build();
Assert.Same(loggerFactory, host.Services.GetService<ILoggerFactory>());
}
[Fact]
public void DefaultHostingConfigurationDoesNotCaptureStartupErrors()
{