WebAPIContrib.Core icon indicating copy to clipboard operation
WebAPIContrib.Core copied to clipboard

Log Output Does Not Work for Application Paths When Using ParallelApplicationPipelinesExtensions

Open haacked opened this issue 6 years ago • 2 comments

When using ParallelApplicationPipelinesExtensions, log messages originating from an application path do not show in the console.

Repro Steps

  1. Clone https://github.com/WebApiContrib/WebAPIContrib.Core/
  2. 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!";
    }
}
  1. Run the app using dotnet run from the console.
  2. 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

haacked avatar Oct 31 '19 20:10 haacked

Note that I also tested this on ASP.NET Core 3.0 and it still doesn't work.

haacked avatar Oct 31 '19 20:10 haacked

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.

haacked avatar Nov 04 '19 17:11 haacked