-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HostingAbstractionsHostExtensions RunAsync disposes without logging #72225
Comments
Tagging subscribers to this area: @dotnet/area-extensions-hosting Issue DetailsDescriptionThe RunAsync in HostingAbstractionsHostExtensions.cs disposes the Host on exit: Lines 60 to 70 in 2be87c9
This disposes the ServiceProvider and the LoggerFactory, so unable to perform any logging after this point. Inside the Host
And because of the dispose, then any logging is impossible. Reproduction Steps var builder = Host.CreateDefaultBuilder();
builder.ConfigureServices(s => s.AddHostedService<BrokenHostedService>());
builder.Build().Run();
class BrokenHostedService : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
{
throw new Exception("Hosted service startup error!");
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
} Expected behaviorException is logged to the Microsoft Console Provider. Actual behaviorApplication crashes without any trace. Regression?No response Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
The application crashes because an unhandled exception is being thrown and killing the Main thread. You can workaround this by wrapping your |
Yes I'm aware that with enough effort, then one can add try-catches everywhere in user-code to ensure exception is logged. But since having the Host-wrapper, that already now performs logging of Host-"Starting" and Host-"Stopped", then to me it would be natural that it also performed logging of Host-"Startup Error" on exception. Thus making the user-code cleaner with more focus on the success path, than the error-path. |
I also find it odd that exceptions thrown starting hosted services aren't logged while stopping exceptions are. I include this boilerplate in my apps as a workaround. Definitely feels less idiomatic than await using ((IAsyncDisposable)(host = builder.Build()))
{
var logger = host.Services.GetService<ILogger<Program>>();
try
{
await host.StartAsync();
}
catch (Exception exception)
{
logger?.LogCritical(exception, "Failed to start host");
throw;
}
await host.WaitForShutdownAsync();
} |
Description
The RunAsync in HostingAbstractionsHostExtensions.cs disposes the Host on exit:
runtime/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs
Lines 60 to 70 in 2be87c9
This disposes the ServiceProvider and the LoggerFactory, so unable to perform any logging after this point.
Inside the Host
StartAsync
then it nicely logsStarting
, but any exception happing in the following code is not caught and logged:runtime/src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs
Lines 56 to 66 in 215b39a
And because of the dispose, then any logging is impossible on exception:
Reproduction Steps
Expected behavior
Exception is logged to the Microsoft Console Provider.
Actual behavior
Application crashes without any trace.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: