Log Output Does Not Work for Application Paths When Using ParallelApplicationPipelinesExtensions
When using ParallelApplicationPipelinesExtensions, log messages originating from an application path do not show in the console.
Repro Steps
- Clone https://github.com/WebApiContrib/WebAPIContrib.Core/
- Make the following edit to
WelcomeService.cs
public class WelcomeService : IGreetService
{
readonly ILogger<WelcomeService> _logger;
public WelcomeService(ILogger<WelcomeService> logger)
{
_logger = logger;
}
public string Greet()
{
_logger.LogError("Error Will Robinson!");
return "Welcome!";
}
}
- Run the app using
dotnet runfrom the console. - Visit https://localhost:5001/welcome-branch in your browser.
Expected Behavior
I expect to see the "Error Will Robinson" message in the console output.
Actual Behavior
I only see log messages from ASP.NET Core itself.
Extra Information
Note that if you change the Main method in Program.cs like so...
public static void Main(string[] args)
{
var host = BuildWebHost(args);
var logger = host.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("This log message works.");
host.Run();
}
That log message works. It seems to me that the logging services or configuration are not carried over to the application paths.
/cc @filipw
Note that I also tested this on ASP.NET Core 3.0 and it still doesn't work.
Ok, I figured out a fix for my scenario, but it applies to asp.net core 3.0. I haven't investigated a asp.net core 2* fix.
Here's the change to make in https://github.com/WebApiContrib/WebAPIContrib.Core/blob/master/src/WebApiContrib.Core/ParallelApplicationPipelinesExtensions.cs
- var webHost = new WebHostBuilder().
+ var webHost = Host.CreateDefaultBuilder().
ConfigureServices(s => {
s.AddSingleton<IServer, DummyServer>();
}).
ConfigureServices(servicesConfiguration).
- UseStartup<EmptyStartup>().
+ ConfigureWebHostDefaults(wh => wh.UseStartup<EmptyStartup>()).
Build();
var serviceProvider = webHost.Services;
- var serverFeatures = webHost.Features;
+ var serverFeatures = new FeatureCollection();
This sets up logging correctly.