|
| 1 | +using System.IO.Abstractions; |
| 2 | +using GitVersion; |
1 | 3 | using GitVersion.Agents; |
2 | 4 | using GitVersion.Configuration; |
3 | 5 | using GitVersion.Extensions; |
| 6 | +using GitVersion.Logging; |
4 | 7 | using GitVersion.Output; |
5 | 8 | using Microsoft.Extensions.DependencyInjection; |
6 | 9 | using Microsoft.Extensions.Hosting; |
| 10 | +using Microsoft.Extensions.Logging; |
7 | 11 | using Microsoft.Extensions.Options; |
| 12 | +using Serilog; |
8 | 13 |
|
9 | 14 | namespace GitVersion; |
10 | 15 |
|
11 | 16 | internal static class CliHost |
12 | 17 | { |
13 | 18 | internal static HostApplicationBuilder CreateCliHostBuilder(string[] args) |
14 | 19 | { |
| 20 | + // Parse arguments early to configure logging |
| 21 | + var tempServices = new ServiceCollection(); |
| 22 | + tempServices.AddSingleton<IArgumentParser, ArgumentParser>(); |
| 23 | + var tempProvider = tempServices.BuildServiceProvider(); |
| 24 | + var argumentParser = tempProvider.GetRequiredService<IArgumentParser>(); |
| 25 | + var arguments = argumentParser.ParseArguments(args); |
| 26 | + var gitVersionOptions = arguments.ToOptions(); |
| 27 | + |
15 | 28 | var builder = Host.CreateApplicationBuilder(args); |
16 | 29 |
|
| 30 | + // Configure Serilog based on parsed arguments |
| 31 | + ConfigureSerilog(builder, gitVersionOptions); |
| 32 | + |
17 | 33 | builder.Services.AddModule(new GitVersionCoreModule()); |
18 | 34 | builder.Services.AddModule(new GitVersionLibGit2SharpModule()); |
19 | 35 | builder.Services.AddModule(new GitVersionBuildAgentsModule()); |
20 | 36 | builder.Services.AddModule(new GitVersionConfigurationModule()); |
21 | 37 | builder.Services.AddModule(new GitVersionOutputModule()); |
22 | 38 | builder.Services.AddModule(new GitVersionAppModule()); |
23 | 39 |
|
24 | | - builder.Services.AddSingleton(sp => |
25 | | - { |
26 | | - var arguments = sp.GetRequiredService<IArgumentParser>().ParseArguments(args); |
27 | | - var gitVersionOptions = arguments.ToOptions(); |
28 | | - return Options.Create(gitVersionOptions); |
29 | | - }); |
| 40 | + builder.Services.AddSingleton(sp => Options.Create(gitVersionOptions)); |
30 | 41 |
|
31 | 42 | builder.Services.AddSingleton<GitVersionApp>(); |
32 | 43 |
|
33 | 44 | return builder; |
34 | 45 | } |
| 46 | + |
| 47 | + private static void ConfigureSerilog(HostApplicationBuilder builder, GitVersionOptions options) |
| 48 | + { |
| 49 | + var loggerConfiguration = LoggingModule.CreateLoggerConfiguration( |
| 50 | + options.Verbosity, |
| 51 | + options.Output.Contains(OutputType.BuildServer) || options.LogFilePath == "console", |
| 52 | + options.LogFilePath != null && options.LogFilePath != "console" ? options.LogFilePath : null, |
| 53 | + new FileSystem() |
| 54 | + ); |
| 55 | + |
| 56 | + Serilog.Log.Logger = loggerConfiguration.CreateLogger(); |
| 57 | + |
| 58 | + builder.Services.AddLogging(loggingBuilder => |
| 59 | + { |
| 60 | + loggingBuilder.ClearProviders(); |
| 61 | + loggingBuilder.AddSerilog(Serilog.Log.Logger, dispose: true); |
| 62 | + }); |
| 63 | + } |
35 | 64 | } |
0 commit comments