Skip to content

Commit bc318b3

Browse files
committed
Enable logging in GitTools.
1 parent e399ab7 commit bc318b3

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/GitVersionCore/GitPreparer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace GitVersion
44
using System.IO;
55
using System.Linq;
66
using GitTools.Git;
7+
using GitTools.Logging;
78
using LibGit2Sharp;
89

910
public class GitPreparer
@@ -28,6 +29,9 @@ public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentic
2829
};
2930
this.noFetch = noFetch;
3031
this.targetPath = targetPath.TrimEnd('/', '\\');
32+
33+
// GitTools has its own logging. So that it actually outputs something, it needs to be initialized.
34+
LogProvider.SetCurrentLogProvider(new LoggerWrapper());
3135
}
3236

3337
public string WorkingDirectory

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<Compile Include="Helpers\ServiceMessageEscapeHelper.cs" />
134134
<Compile Include="Helpers\ThreadSleep.cs" />
135135
<Compile Include="IncrementStrategyFinder.cs" />
136+
<Compile Include="LoggerWrapper.cs" />
136137
<Compile Include="OutputVariables\VersionVariables.cs" />
137138
<Compile Include="SemanticVersionExtensions.cs" />
138139
<Compile Include="SemanticVersionFormatValues.cs" />

src/GitVersionCore/LoggerWrapper.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
using GitTools.Logging;
5+
6+
/// <summary>
7+
/// Wraps the <see cref="Logger" /> for use by GitTools.
8+
/// </summary>
9+
public class LoggerWrapper : ILogProvider
10+
{
11+
public GitTools.Logging.Logger GetLogger(string name)
12+
{
13+
return Log;
14+
}
15+
16+
public IDisposable OpenNestedContext(string message)
17+
{
18+
throw new NotImplementedException();
19+
}
20+
21+
public IDisposable OpenMappedContext(string key, string value)
22+
{
23+
throw new NotImplementedException();
24+
}
25+
26+
private static bool Log(LogLevel loglevel, Func<string> messagefunc, Exception exception, object[] formatparameters)
27+
{
28+
// Create the main message. Careful of string format errors.
29+
string message;
30+
if (messagefunc == null)
31+
{
32+
message = null;
33+
}
34+
else
35+
{
36+
if (formatparameters == null || formatparameters.Length == 0)
37+
{
38+
message = messagefunc();
39+
}
40+
else
41+
{
42+
try
43+
{
44+
message = string.Format(messagefunc(), formatparameters);
45+
}
46+
catch (FormatException)
47+
{
48+
message = messagefunc();
49+
Logger.WriteError(string.Format("LoggerWrapper.Log(): Incorrectly formatted string: message: '{0}'; formatparameters: {1}", message, string.Join(";", formatparameters)));
50+
}
51+
}
52+
}
53+
54+
if (exception != null)
55+
{
56+
// Append the exception to the end of the message.
57+
message = string.IsNullOrEmpty(message) ? exception.ToString() : string.Format("{0}\n{1}", message, exception);
58+
}
59+
60+
if (!string.IsNullOrEmpty(message))
61+
{
62+
switch (loglevel)
63+
{
64+
case LogLevel.Trace:
65+
case LogLevel.Debug:
66+
Logger.WriteDebug(message);
67+
break;
68+
case LogLevel.Info:
69+
Logger.WriteInfo(message);
70+
break;
71+
case LogLevel.Warn:
72+
Logger.WriteWarning(message);
73+
break;
74+
case LogLevel.Error:
75+
case LogLevel.Fatal:
76+
Logger.WriteError(message);
77+
break;
78+
}
79+
}
80+
81+
return true;
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)