dotnet / dotnet/command-line-api
Logging Exceptions through UseExceptionHandler
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
It would seem reasonable to log errors in the exception handler, and configure it using the modified builder (as per https://github.com/dotnet/command-line-api/issues/796#issuecomment-670763630).
However, the context available in the `UseExceptionHandler` method has its services disposed.
Is this expected behavior, or how would I proceed to log the error with the logging infrastructure configured in the `IHost.ConfigureLogging`?
```
var _ = new CommandLineBuilder(rootCommand)
.UseHost(_ => Host.CreateDefaultBuilder(), host =>
{
host
.ConfigureAppConfiguration( ... )
.ConfigureLogging( ... )
.ConfigureServices( ... );
})
.UseDefaults()
.UseExceptionHandler((e, context) =>
{
// throws 'System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'IServiceProvider'.'
var logger = context.GetHost().Services.GetRequiredService();
logger.LogError(e);
})
.Build();
rootCommand.Invoke(args);
```
Contributor guide
Assessment
This issue has not been assessed yet.