question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebHost inside Generic host not logging startup messages

See original GitHub issue

If you have a generic host, then inside this host you add a web host, and you are using .UseSerilog() for both, then the web host startup messages are not logged to Serilog.

This is what I mean by “startup messages”:

Hosting environment: Production Content root path: C:\Users\Me\Project\src\App\bin\Debug\netcoreapp3.0 Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.

Program.cs

public static async Task Main(string[] args)
{
    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
        .Enrich.FromLogContext()
        .WriteTo.Console()
        .CreateLogger();

    IHostBuilder builder = new HostBuilder()
        .UseSerilog()
        .ConfigureServices(services => { collection.AddHostedService<MyService>(); });

    await builder.RunConsoleAsync();
}

MyService.cs

public class MyService : IHostedService
{
    private IWebHost _webHost;

    public async Task StartAsync(CancellationToken ct)
    {
        _webHost = WebHost.CreateDefaultBuilder()
            .UseStartup<MyService>()
            .UseSerilog()
            .Build();

        _webHost.RunAsync();
    }

    public async Task StopAsync(CancellationToken ct)
    {
        await _webHost.StopAsync();
    }

    public void ConfigureServices(IServiceCollection services) {
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
    }
}

When this starts the logging from the generic host uses Serilog, but the logging in the web host startup does not. Logging of requests to the web host, on the other hand, does use Serilog. This is apparent in the output:

Hosting environment: Production Content root path: C:\Users\Me\Project\src\App\bin\Debug\netcoreapp3.0 Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down. [15:32:01 INF] Application started. Press Ctrl+C to shut down. [15:32:01 INF] Hosting environment: Production [15:32:01 INF] Content root path: C:\Users\Me\Project\src\App\bin\Debug\netcoreapp3.0
[15:32:33 INF] Request starting HTTP/1.1 GET http://localhost:5000/ [15:32:33 INF] Request finished in 13.2002ms 404

Expected behaviour

The startup messages from a web host inside a generic host should still be logged to Serilog.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
gthvidstencommented, Sep 11, 2019

@sungam3r Good find! So it seems this is an issue with ASP,NET Core outputting messages to the console regardless of logging, which basically explains everything. It also means this is not a Serilog issue at all, so I’ll close this issue and see if anybody has reported this for aspnet/AspNetCore (and report it myself if not).

Thanks for the assistance!

Read more comments on GitHub >

github_iconTop Results From Across the Web

NET Generic Host in ASP.NET Core
Use .NET Core Generic Host in ASP.NET Core apps. Generic Host is responsible for app startup and lifetime management.
Read more >
Suppressing the startup and shutdown messages in ASP. ...
In this post I show how to disable the startup messages shown in the console when you run an ASP.NET Core application using...
Read more >
How to log in IHostingStartup.Configure()
I am interested in being able to log messages in Configure() method. However, the IHostingStartup class is not instantiated using DI (because ...
Read more >
Using HostBuilder and the Generic Host in .NET Core ...
NET Core 2.1 is the new “generic” Host which enables developers to easily set up cross-cutting concerns such as logging, configuration and ...
Read more >
Configure Serilog in ASP.NET Core - few practical tips
We needed a quick and clean way to log requests to the API and the responses from the API. Make Serilog available in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found