Skip to content

Commit dfcce6c

Browse files
committed
Reorganize Program.cs
1 parent fdcf08d commit dfcce6c

1 file changed

Lines changed: 13 additions & 20 deletions

File tree

src/ImageBuilder/Program.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,34 @@
77
using Microsoft.DotNet.ImageBuilder;
88
using Microsoft.DotNet.ImageBuilder.Commands;
99
using Microsoft.Extensions.DependencyInjection;
10-
using Microsoft.Extensions.Hosting;
11-
using Microsoft.Extensions.Logging.Abstractions;
12-
using ICommand = Microsoft.DotNet.ImageBuilder.Commands.ICommand;
1310

14-
using IHost host = ImageBuilder.CreateAppHost();
11+
using var host = ImageBuilder.CreateAppHost();
1512

16-
// Some parts of ImageBuilder aren't fully onboarded to DI yet (see StandaloneLoggerFactory).
17-
// Hand them the host's logger factory so those static code paths can log through the
18-
// application host's configured logging.
19-
StandaloneLoggerFactory.LoggerFactory = host.Services.GetRequiredService<ILoggerFactory>();
2013
try
2114
{
2215
await host.StartAsync();
2316

24-
RootCommand rootCliCommand = new();
17+
// Some parts of ImageBuilder aren't fully onboarded to DI yet (see StandaloneLoggerFactory).
18+
// Hand them the host's logger factory so those static code paths can log through the
19+
// application host's configured logging.
20+
var loggerFactory = host.Services.GetRequiredService<ILoggerFactory>();
21+
StandaloneLoggerFactory.LoggerFactory = loggerFactory;
2522

26-
foreach (ICommand command in host.Services.GetServices<ICommand>())
27-
{
23+
var rootCliCommand = new RootCommand();
24+
var commands = host.Services.GetServices<ICommand>();
25+
26+
foreach (var command in commands)
2827
rootCliCommand.Add(command.GetCliCommand());
29-
}
3028

31-
int exitCode = await rootCliCommand.Parse(args).InvokeAsync();
29+
var parseResult = rootCliCommand.Parse(args);
30+
int exitCode = await parseResult.InvokeAsync();
3231

3332
await host.StopAsync();
3433
return exitCode;
3534
}
3635
catch (Exception e)
3736
{
38-
ILoggerFactory? loggerFactory = host.Services.GetService<ILoggerFactory>();
37+
var loggerFactory = host.Services.GetService<ILoggerFactory>();
3938
if (loggerFactory is not null)
4039
{
4140
ILogger logger = loggerFactory.CreateLogger("ImageBuilder.Program");
@@ -46,11 +45,5 @@
4645
Console.Error.WriteLine(e);
4746
}
4847
}
49-
finally
50-
{
51-
// Drop the reference to the host-owned factory so static logging falls back to a no-op
52-
// once the host is disposed.
53-
StandaloneLoggerFactory.LoggerFactory = NullLoggerFactory.Instance;
54-
}
5548

5649
return 1;

0 commit comments

Comments
 (0)