-
Notifications
You must be signed in to change notification settings - Fork 151
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
Hosted app startup errors cause top-level exception logging to be lost when ShutdownOnDispose is enabled #605
Comments
What happens if you change to this: var logger = LogManager.GetCurrentClassLogger();
bool LogError(Exception exception)
{
logger.Error(exception, "Stopped program because of exception");
return false; // Exception Filter should not suppress the Exception
}
try
{
logger.Info("Starting program...");
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureLogging(l => l.ClearProviders().AddNLog(new NLogProviderOptions { ShutdownOnDispose = true }));
builder.ConfigureServices(s => s.AddHostedService<BrokenHostedService>());
// This exception does get correctly logged when thrown:
// builder.ConfigureServices(s => throw new Exception("DI config-time error!"));
var app = builder.Build();
app.Run();
}
catch (Exception exception) when (LogError(exception))
{
Environment.ExitCode = 1;
throw;
}
finally
{
LogManager.Shutdown();
} |
The reason for changing to |
In that case, |
Have you tested ? (since it works for me) |
Yes, when I run it the only output is:
Which is from the unhandled exception being logged to console by the .NET runtime, not NLog (note how it doesn't contain our log message "Stopped program because of exception") |
I get this output:
|
That's very strange - I definitely don't get that first log when I run it... |
Have added an extra line to the example program: logger.Info("Starting program..."); Do you see that output? (Maybe NLog.config is not loaded correctly) |
Yes I can reproduce it now. I was testing with the exception from: .ConfigureServices(s => throw new Exception("DI config-time error!")); But I get your experience when |
Curious why Microsoft decided to dispose its LoggerFactory, before logging the error. Espcially since the Microsoft Host has a Logger-object that is used for logging "Starting" / "Stopping" (But not failure) |
Thank you for bringing this to my attention. NLog.Web.AspNetCore v5.1 have now been released: https://www.nuget.org/packages/NLog.Web.AspNetCore/5.1.0 It reverts ShutdownOnDispose default value to false. |
Type: Bug
NLog version: 5.0.1
NLog.Extensions.Logging version: 5.0.1
Platform: .NET 6
In this example, we have an
IHostedService
which throws an exception on startup, which bubbles up out ofapp.Run()
. It should be caught and logged by the top-level exception handler, but nothing is logged because NLog has already been shutdown, presumably due to theIServiceProvider
having been disposed.Example nlog.config (not particularly relevant to this bug, but here it is to aid repro):
This only occurs when
ShutdownOnDispose
is set to true - when false, the error is correctly logged. Of course, doing so may also cause different logs to be lost during app shutdown unless we also explicitly disableAutoShutdown
.When an exception is thrown during the
IServiceCollection
configuration, e.g. by uncommenting the relevant line of code in the above example, it is logged, presumably because theIServiceProvider
has not yet been constructed (and so obviously hasn't been disposed).This problem also occurs using
NLog.Web.AspNetCore
with the defaultNLogAspNetCoreOptions
, as they now haveShutdownOnDispose
enabled by default.The text was updated successfully, but these errors were encountered: