Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Pass existing LoggerFactory into WebHostBuilder #661

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Microsoft.AspNetCore.Hosting.Abstractions/IWebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.Hosting
{
Expand All @@ -18,6 +19,13 @@ public interface IWebHostBuilder
/// </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>
Expand Down Expand Up @@ -46,6 +54,13 @@ public interface IWebHostBuilder
/// <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>
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.AspNetCore.Hosting.Abstractions/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down
46 changes: 41 additions & 5 deletions src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,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
Expand All @@ -40,11 +41,14 @@ public class WebHostBuilder : IWebHostBuilder
// 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>
Expand All @@ -69,6 +73,22 @@ public string GetSetting(string key)
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>
Expand Down Expand Up @@ -135,13 +155,18 @@ public IWebHostBuilder Configure(Action<IApplicationBuilder> configureApp)
}

/// <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;
}

Expand Down Expand Up @@ -184,14 +209,25 @@ private IServiceCollection BuildHostingServices()

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");
Expand Down
28 changes: 28 additions & 0 deletions test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using System.Reflection;
using Xunit;
Expand Down Expand Up @@ -124,6 +125,33 @@ public async Task StartupConfigureThrows_Fallback()
}
}

[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 DefaultConfigurationCapturesStartupErrors()
{
Expand Down