Add configuration to logging in WebHost
This commit is contained in:
parent
70716bdff5
commit
8102d6a038
|
|
@ -175,8 +175,9 @@ namespace Microsoft.AspNetCore
|
||||||
config.AddCommandLine(args);
|
config.AddCommandLine(args);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.ConfigureLogging(logging =>
|
.ConfigureLogging((hostingContext, logging) =>
|
||||||
{
|
{
|
||||||
|
logging.UseConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
||||||
logging.AddConsole();
|
logging.AddConsole();
|
||||||
logging.AddDebug();
|
logging.AddDebug();
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -200,6 +200,47 @@ namespace Microsoft.AspNetCore.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void LoggingConfigurationSectionPassedToLoggerByDefault()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File.WriteAllText("appsettings.json", @"
|
||||||
|
{
|
||||||
|
""Logging"": {
|
||||||
|
""LogLevel"": {
|
||||||
|
""Default"": ""Warning""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
using (var webHost = WebHost.Start(context => context.Response.WriteAsync("Hello, World!")))
|
||||||
|
{
|
||||||
|
var factory = (ILoggerFactory)webHost.Services.GetService(typeof(ILoggerFactory));
|
||||||
|
var logger = factory.CreateLogger("Test");
|
||||||
|
|
||||||
|
logger.Log(LogLevel.Information, 0, "Message", null, (s, e) =>
|
||||||
|
{
|
||||||
|
Assert.True(false);
|
||||||
|
return string.Empty;
|
||||||
|
});
|
||||||
|
|
||||||
|
var logWritten = false;
|
||||||
|
logger.Log(LogLevel.Warning, 0, "Message", null, (s, e) =>
|
||||||
|
{
|
||||||
|
logWritten = true;
|
||||||
|
return string.Empty;
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.True(logWritten);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
File.Delete("appsettings.json");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task ExecuteStartOrStartWithTest(Func<DeploymentResult, Task<HttpResponseMessage>> getResponse, string applicationName)
|
private async Task ExecuteStartOrStartWithTest(Func<DeploymentResult, Task<HttpResponseMessage>> getResponse, string applicationName)
|
||||||
{
|
{
|
||||||
await ExecuteTestApp(applicationName, async (deploymentResult, logger) =>
|
await ExecuteTestApp(applicationName, async (deploymentResult, logger) =>
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,6 @@ namespace CreateDefaultBuilderApp
|
||||||
string responseMessage = string.Empty;
|
string responseMessage = string.Empty;
|
||||||
|
|
||||||
WebHost.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" })
|
WebHost.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" })
|
||||||
.UseLoggerFactory(new TestLoggerFactory())
|
|
||||||
.ConfigureServices((context, services) =>
|
.ConfigureServices((context, services) =>
|
||||||
{
|
{
|
||||||
responseMessage = GetResponseMessage(context, services);
|
responseMessage = GetResponseMessage(context, services);
|
||||||
|
|
@ -77,34 +76,11 @@ namespace CreateDefaultBuilderApp
|
||||||
return $"Command line arguments not loaded into Configuration.";
|
return $"Command line arguments not loaded into Configuration.";
|
||||||
}
|
}
|
||||||
|
|
||||||
var testLoggerFactory = (TestLoggerFactory)context.LoggerFactory;
|
// TODO: Verify AddConsole called
|
||||||
|
// TODO: Verify AddDebug called
|
||||||
// Verify AddConsole called
|
|
||||||
if (!testLoggerFactory.Providers.Any(provider => provider is ConsoleLoggerProvider))
|
|
||||||
{
|
|
||||||
return $"Console logger not added to ILoggerFactory.";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify AddDebug called
|
|
||||||
if (!testLoggerFactory.Providers.Any(provider => provider is DebugLoggerProvider))
|
|
||||||
{
|
|
||||||
return $"Debug logger not added to ILoggerFactory.";
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Verify UseIISIntegration called
|
// TODO: Verify UseIISIntegration called
|
||||||
|
|
||||||
return context.HostingEnvironment.ApplicationName;
|
return context.HostingEnvironment.ApplicationName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TestLoggerFactory : ILoggerFactory
|
|
||||||
{
|
|
||||||
public IList<ILoggerProvider> Providers { get; } = new List<ILoggerProvider>();
|
|
||||||
|
|
||||||
public void AddProvider(ILoggerProvider provider) => Providers.Add(provider);
|
|
||||||
|
|
||||||
public ILogger CreateLogger(string categoryName) => NullLogger.Instance;
|
|
||||||
|
|
||||||
public void Dispose() { }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue