Skip to content

Commit b08357d

Browse files
committed
Clean up logging
Consolidate all the logging calls that was separated out into the base Task class rather than confusing multiple locations.
1 parent bbcabe0 commit b08357d

7 files changed

+34
-79
lines changed

src/GitVersionTask/BuildLogger.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/GitVersionTask/GenerateGitVersionInformation.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ namespace GitVersionTask
88

99
public class GenerateGitVersionInformation : GitVersionTaskBase
1010
{
11-
TaskLogger logger;
12-
1311
public GenerateGitVersionInformation()
1412
{
15-
logger = new TaskLogger(this);
16-
Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
1713
}
1814

1915
[Required]
@@ -42,12 +38,12 @@ public override bool Execute()
4238
}
4339
catch (WarningException errorException)
4440
{
45-
logger.LogWarning(errorException.Message);
41+
this.LogWarning(errorException.Message);
4642
return true;
4743
}
4844
catch (Exception exception)
4945
{
50-
logger.LogError("Error occurred: " + exception);
46+
this.LogError("Error occurred: " + exception);
5147
return false;
5248
}
5349
}

src/GitVersionTask/GetVersion.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ namespace GitVersionTask
77

88
public class GetVersion : GitVersionTaskBase
99
{
10-
TaskLogger logger;
11-
1210
public GetVersion()
1311
{
14-
logger = new TaskLogger(this);
15-
Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
1612
}
1713

1814
[Required]
@@ -128,12 +124,12 @@ public override bool Execute()
128124
}
129125
catch (WarningException errorException)
130126
{
131-
logger.LogWarning(errorException.Message);
127+
this.LogWarning(errorException.Message);
132128
return true;
133129
}
134130
catch (Exception exception)
135131
{
136-
logger.LogError("Error occurred: " + exception);
132+
this.LogError("Error occurred: " + exception);
137133
return false;
138134
}
139135
}

src/GitVersionTask/GitVersionTaskBase.cs

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

6+
using Microsoft.Build.Framework;
67
using Microsoft.Build.Utilities;
78

89
public abstract class GitVersionTaskBase : Task
@@ -13,11 +14,32 @@ protected GitVersionTaskBase()
1314
{
1415
var fileSystem = new FileSystem();
1516
executeCore = new ExecuteCore(fileSystem);
17+
GitVersion.Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
1618
}
1719

1820
protected ExecuteCore ExecuteCore
1921
{
2022
get { return executeCore; }
2123
}
24+
25+
public void LogDebug(string message)
26+
{
27+
this.BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Low));
28+
}
29+
30+
public void LogWarning(string message)
31+
{
32+
this.BuildEngine.LogWarningEvent(new BuildWarningEventArgs(string.Empty, string.Empty, null, 0, 0, 0, 0, message, string.Empty, "GitVersionTask"));
33+
}
34+
35+
public void LogInfo(string message)
36+
{
37+
this.BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Normal));
38+
}
39+
40+
public void LogError(string message, string file = null)
41+
{
42+
this.BuildEngine.LogErrorEvent(new BuildErrorEventArgs(string.Empty, string.Empty, file, 0, 0, 0, 0, message, string.Empty, "GitVersionTask"));
43+
}
2244
}
2345
}

src/GitVersionTask/TaskLogger.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/GitVersionTask/UpdateAssemblyInfo.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ namespace GitVersionTask
99

1010
public class UpdateAssemblyInfo : GitVersionTaskBase
1111
{
12-
TaskLogger logger;
13-
1412
public UpdateAssemblyInfo()
1513
{
16-
logger = new TaskLogger(this);
17-
Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
1814
}
1915

2016
[Required]
@@ -46,12 +42,12 @@ public override bool Execute()
4642
}
4743
catch (WarningException errorException)
4844
{
49-
logger.LogWarning(errorException.Message);
45+
this.LogWarning(errorException.Message);
5046
return true;
5147
}
5248
catch (Exception exception)
5349
{
54-
logger.LogError("Error occurred: " + exception);
50+
this.LogError("Error occurred: " + exception);
5551
return false;
5652
}
5753
}

src/GitVersionTask/WriteVersionInfoToBuildLog.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ namespace GitVersionTask
77

88
public class WriteVersionInfoToBuildLog : GitVersionTaskBase
99
{
10-
readonly TaskLogger logger;
11-
1210
public WriteVersionInfoToBuildLog()
1311
{
14-
logger = new TaskLogger(this);
15-
Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
1612
}
1713

1814
[Required]
@@ -29,12 +25,12 @@ public override bool Execute()
2925
}
3026
catch (WarningException errorException)
3127
{
32-
logger.LogWarning(errorException.Message);
28+
this.LogWarning(errorException.Message);
3329
return true;
3430
}
3531
catch (Exception exception)
3632
{
37-
logger.LogError("Error occurred: " + exception);
33+
this.LogError("Error occurred: " + exception);
3834
return false;
3935
}
4036
}
@@ -54,12 +50,12 @@ void WriteIntegrationParameters(IEnumerable<IBuildServer> applicableBuildServers
5450
{
5551
foreach (var buildServer in applicableBuildServers)
5652
{
57-
logger.LogInfo(string.Format("Executing GenerateSetVersionMessage for '{0}'.", buildServer.GetType().Name));
58-
logger.LogInfo(buildServer.GenerateSetVersionMessage(variables));
59-
logger.LogInfo(string.Format("Executing GenerateBuildLogOutput for '{0}'.", buildServer.GetType().Name));
53+
this.LogInfo(string.Format("Executing GenerateSetVersionMessage for '{0}'.", buildServer.GetType().Name));
54+
this.LogInfo(buildServer.GenerateSetVersionMessage(variables));
55+
this.LogInfo(string.Format("Executing GenerateBuildLogOutput for '{0}'.", buildServer.GetType().Name));
6056
foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(buildServer, variables))
6157
{
62-
logger.LogInfo(buildParameter);
58+
this.LogInfo(buildParameter);
6359
}
6460
}
6561
}

0 commit comments

Comments
 (0)