dotnet / dotnet/command-line-api

Logging Exceptions through UseExceptionHandler

Open
#1,311 7 comments 0 reactions 0 assignees View on GitHub
Area-Hosting and DI
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.