Skip to content

Commit 55c2f64

Browse files
committed
refactor(logging): Centralize logging configuration
Consolidate logging setup by passing `GitVersionOptions` directly to `LoggingEnricher`, removing redundant configuration logic from `GitVersionExecutor`.
1 parent 7202102 commit 55c2f64

3 files changed

Lines changed: 10 additions & 44 deletions

File tree

src/GitVersion.App/GitVersionApp.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using GitVersion.Extensions;
2+
using GitVersion.Logging;
23

34
namespace GitVersion;
45

@@ -16,6 +17,7 @@ public Task RunAsync(CancellationToken _)
1617
try
1718
{
1819
var gitVersionOptions = this.options.Value;
20+
LoggingEnricher.Configure(gitVersionOptions);
1921

2022
if (gitVersionOptions.IsHelp || gitVersionOptions.IsVersion)
2123
{

src/GitVersion.App/GitVersionExecutor.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using GitVersion.Extensions;
44
using GitVersion.Git;
55
using GitVersion.Helpers;
6-
using GitVersion.Logging;
76

87
namespace GitVersion;
98

@@ -115,8 +114,6 @@ private void Initialize(GitVersionOptions gitVersionOptions)
115114
gitVersionOptions.Settings.NoCache = true;
116115
}
117116

118-
ConfigureLogging(gitVersionOptions);
119-
120117
var workingDirectory = gitVersionOptions.WorkingDirectory;
121118
if (gitVersionOptions.Diag)
122119
{
@@ -146,11 +143,4 @@ private bool VerifyAndDisplayConfiguration(GitVersionOptions gitVersionOptions)
146143
this.console.WriteLine(configurationString);
147144
return true;
148145
}
149-
150-
private static void ConfigureLogging(GitVersionOptions gitVersionOptions)
151-
{
152-
var enableConsoleOutput = gitVersionOptions.Output.Contains(OutputType.BuildServer)
153-
|| string.Equals(gitVersionOptions.LogFilePath, "console", StringComparison.OrdinalIgnoreCase);
154-
LoggingEnricher.Configure(gitVersionOptions.LogFilePath, gitVersionOptions.Verbosity, enableConsoleOutput);
155-
}
156146
}

src/GitVersion.Core/Logging/LoggingEnricher.cs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,16 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
4747
logEvent.AddPropertyIfAbsent(logFilePathProp);
4848
}
4949

50-
/// <summary>
51-
/// Configures the logging enricher with the specified log file path and verbosity.
52-
/// </summary>
53-
/// <param name="logFile">The log file path, or null to disable file logging.</param>
54-
/// <param name="verbosity">The verbosity level.</param>
55-
/// <param name="enableConsoleOutput">Whether to enable console output. When false, console output is suppressed.</param>
56-
public static void Configure(string? logFile, Verbosity verbosity, bool enableConsoleOutput = true)
50+
public static void Configure(GitVersionOptions gitVersionOptions)
5751
{
58-
if (!string.IsNullOrWhiteSpace(logFile))
59-
logFilePath = logFile;
60-
LogLevelSwitch.MinimumLevel = GetLevelForVerbosity(verbosity);
61-
IsConsoleEnabled = enableConsoleOutput;
62-
}
52+
var enableConsoleOutput = gitVersionOptions.Output.Contains(OutputType.BuildServer)
53+
|| string.Equals(gitVersionOptions.LogFilePath, "console", StringComparison.OrdinalIgnoreCase);
6354

64-
/// <summary>
65-
/// Configures the logging enricher with the specified log file path and log level.
66-
/// </summary>
67-
/// <param name="logFile">The log file path, or null to disable file logging.</param>
68-
/// <param name="logLevel">The Microsoft.Extensions.Logging log level.</param>
69-
/// <param name="enableConsoleOutput">Whether to enable console output. When false, console output is suppressed.</param>
70-
public static void Configure(string? logFile, LogLevel logLevel, bool enableConsoleOutput = true)
71-
{
72-
if (!string.IsNullOrWhiteSpace(logFile))
73-
logFilePath = logFile;
74-
LogLevelSwitch.MinimumLevel = GetLevelForLogLevel(logLevel);
55+
if (!string.IsNullOrWhiteSpace(gitVersionOptions.LogFilePath))
56+
{
57+
logFilePath = gitVersionOptions.LogFilePath;
58+
}
59+
LogLevelSwitch.MinimumLevel = GetLevelForVerbosity(gitVersionOptions.Verbosity);
7560
IsConsoleEnabled = enableConsoleOutput;
7661
}
7762

@@ -85,15 +70,4 @@ public static void Configure(string? logFile, LogLevel logLevel, bool enableCons
8570
{ Verbosity.Minimal, LogEventLevel.Warning },
8671
{ Verbosity.Quiet, LogEventLevel.Error }
8772
};
88-
89-
private static LogEventLevel GetLevelForLogLevel(LogLevel logLevel) => logLevel switch
90-
{
91-
LogLevel.Trace => LogEventLevel.Verbose,
92-
LogLevel.Debug => LogEventLevel.Debug,
93-
LogLevel.Information => LogEventLevel.Information,
94-
LogLevel.Warning => LogEventLevel.Warning,
95-
LogLevel.Error => LogEventLevel.Error,
96-
LogLevel.Critical or LogLevel.None => LogEventLevel.Fatal,
97-
_ => LogEventLevel.Information
98-
};
9973
}

0 commit comments

Comments
 (0)