-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Missing Logging of Exceptions in IHostedService without await #84559
Comments
Tagging subscribers to this area: @dotnet/area-extensions-hosting Issue DetailsDescriptionWhen an exception occures in IHostedService without calling Reproduction StepsIHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
})
.Build();
await host.RunAsync();
public class Worker : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken) =>
throw new Exception();
public Task StopAsync(CancellationToken cancellationToken) =>
Task.CompletedTask;
} Expected behaviorWhen an exception occurs within the Actual behaviorWhen an exception occurs in the Regression?No response Known WorkaroundsNo response ConfigurationSdk: Microsoft.NET.Sdk.Worker Other informationIt is also lead to misunderstanding of This can be relative to #67146
|
It looks we did not had any exception hanlding for running hosted services:
This is updated recently with #84048 and #85191: runtime/src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs Lines 69 to 123 in 71c9c73
So, the issues you are seeing might have already fixed. The repro you provided doesn't have a logger and the await that causing the logging work, so I cannot check. @Tripletri please tryout your code with the .NET 8 preview 5 bits and let us know. |
This issue has been marked |
Yes, the issue is fixed in |
Description
When an exception occures in IHostedService without calling
await
, it is not logged to theILogger
.This make it difficult to identify the reason for the application shutdown when writing logs to an external service.
Reproduction Steps
Expected behavior
When an exception occurs within the
StartAsync
ofIHostedService
, whether or not method hasawait
, the exception should be logged to theILogger
Actual behavior
When an exception occurs in the
StartAsync
without callingawait
, the exception is not logged to theILogger
Regression?
No response
Known Workarounds
No response
Configuration
Sdk: Microsoft.NET.Sdk.Worker
Framework: .NET 7.0.3
Other information
It is also lead to misunderstanding of
BackgroundServiceExceptionBehavior.Ignore
, which continue to stop host when exception occurs.This can be relative to #67146
The text was updated successfully, but these errors were encountered: