|
7 | 7 | using Microsoft.DotNet.ImageBuilder; |
8 | 8 | using Microsoft.DotNet.ImageBuilder.Commands; |
9 | 9 | using Microsoft.Extensions.DependencyInjection; |
10 | | -using Microsoft.Extensions.Hosting; |
11 | | -using Microsoft.Extensions.Logging.Abstractions; |
12 | | -using ICommand = Microsoft.DotNet.ImageBuilder.Commands.ICommand; |
13 | 10 |
|
14 | | -using IHost host = ImageBuilder.CreateAppHost(); |
| 11 | +using var host = ImageBuilder.CreateAppHost(); |
15 | 12 |
|
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>(); |
20 | 13 | try |
21 | 14 | { |
22 | 15 | await host.StartAsync(); |
23 | 16 |
|
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; |
25 | 22 |
|
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) |
28 | 27 | rootCliCommand.Add(command.GetCliCommand()); |
29 | | - } |
30 | 28 |
|
31 | | - int exitCode = await rootCliCommand.Parse(args).InvokeAsync(); |
| 29 | + var parseResult = rootCliCommand.Parse(args); |
| 30 | + int exitCode = await parseResult.InvokeAsync(); |
32 | 31 |
|
33 | 32 | await host.StopAsync(); |
34 | 33 | return exitCode; |
35 | 34 | } |
36 | 35 | catch (Exception e) |
37 | 36 | { |
38 | | - ILoggerFactory? loggerFactory = host.Services.GetService<ILoggerFactory>(); |
| 37 | + var loggerFactory = host.Services.GetService<ILoggerFactory>(); |
39 | 38 | if (loggerFactory is not null) |
40 | 39 | { |
41 | 40 | ILogger logger = loggerFactory.CreateLogger("ImageBuilder.Program"); |
|
46 | 45 | Console.Error.WriteLine(e); |
47 | 46 | } |
48 | 47 | } |
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 | | -} |
55 | 48 |
|
56 | 49 | return 1; |
0 commit comments