From c52b0727e81244da50cd701ac372809d7b7f83cc Mon Sep 17 00:00:00 2001 From: Doug Semler Date: Thu, 21 Mar 2019 10:40:51 -0400 Subject: [PATCH 1/2] Do not reset Logger during call to Execute() If the MSBuild Task object is reused, calls after the first call to the Execute method fail because of the Reset. --- src/GitVersionTask/GenerateGitVersionInformation.cs | 4 ---- src/GitVersionTask/GetVersion.cs | 4 ---- src/GitVersionTask/UpdateAssemblyInfo.cs | 4 ---- src/GitVersionTask/WriteVersionInfoToBuildLog.cs | 4 ---- 4 files changed, 16 deletions(-) diff --git a/src/GitVersionTask/GenerateGitVersionInformation.cs b/src/GitVersionTask/GenerateGitVersionInformation.cs index d91a7898dc..cc5664561d 100644 --- a/src/GitVersionTask/GenerateGitVersionInformation.cs +++ b/src/GitVersionTask/GenerateGitVersionInformation.cs @@ -50,10 +50,6 @@ public override bool Execute() logger.LogError("Error occurred: " + exception); return false; } - finally - { - Logger.Reset(); - } } void InnerExecute() diff --git a/src/GitVersionTask/GetVersion.cs b/src/GitVersionTask/GetVersion.cs index f041a77120..a51126422e 100644 --- a/src/GitVersionTask/GetVersion.cs +++ b/src/GitVersionTask/GetVersion.cs @@ -136,10 +136,6 @@ public override bool Execute() logger.LogError("Error occurred: " + exception); return false; } - finally - { - Logger.Reset(); - } } } } diff --git a/src/GitVersionTask/UpdateAssemblyInfo.cs b/src/GitVersionTask/UpdateAssemblyInfo.cs index 27ccab47d1..b8fe5daa67 100644 --- a/src/GitVersionTask/UpdateAssemblyInfo.cs +++ b/src/GitVersionTask/UpdateAssemblyInfo.cs @@ -54,10 +54,6 @@ public override bool Execute() logger.LogError("Error occurred: " + exception); return false; } - finally - { - Logger.Reset(); - } } void InnerExecute() diff --git a/src/GitVersionTask/WriteVersionInfoToBuildLog.cs b/src/GitVersionTask/WriteVersionInfoToBuildLog.cs index fcc1f8998d..c11a96890f 100644 --- a/src/GitVersionTask/WriteVersionInfoToBuildLog.cs +++ b/src/GitVersionTask/WriteVersionInfoToBuildLog.cs @@ -37,10 +37,6 @@ public override bool Execute() logger.LogError("Error occurred: " + exception); return false; } - finally - { - Logger.Reset(); - } } void InnerExecute() From 9be3c29d4edebbbe820cc4ff2faef07c40cbde16 Mon Sep 17 00:00:00 2001 From: Doug Semler Date: Thu, 21 Mar 2019 11:04:35 -0400 Subject: [PATCH 2/2] Clean up logging Consolidate all the logging calls that was separated out into the base Task class rather than confusing multiple locations. --- src/GitVersionTask/BuildLogger.cs | 25 ------------------ .../GenerateGitVersionInformation.cs | 8 ++---- src/GitVersionTask/GetVersion.cs | 8 ++---- src/GitVersionTask/GitVersionTaskBase.cs | 22 ++++++++++++++++ src/GitVersionTask/TaskLogger.cs | 26 ------------------- src/GitVersionTask/UpdateAssemblyInfo.cs | 8 ++---- .../WriteVersionInfoToBuildLog.cs | 16 +++++------- 7 files changed, 34 insertions(+), 79 deletions(-) delete mode 100644 src/GitVersionTask/BuildLogger.cs delete mode 100644 src/GitVersionTask/TaskLogger.cs diff --git a/src/GitVersionTask/BuildLogger.cs b/src/GitVersionTask/BuildLogger.cs deleted file mode 100644 index 9c9ec461cf..0000000000 --- a/src/GitVersionTask/BuildLogger.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Microsoft.Build.Framework; -using Microsoft.Build.Utilities; - -public static class BuildLogger -{ - public static void LogDebug(this Task task, string message) - { - task.BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Low)); - } - - public static void LogInfo(this Task task, string message) - { - task.BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Normal)); - } - - public static void LogWarning(this Task task, string message) - { - task.BuildEngine.LogWarningEvent(new BuildWarningEventArgs(string.Empty, string.Empty, null, 0, 0, 0, 0, message, string.Empty, "GitVersionTask")); - } - - public static void LogError(this Task task, string message, string file = null) - { - task.BuildEngine.LogErrorEvent(new BuildErrorEventArgs(string.Empty, string.Empty, file, 0, 0, 0, 0, message, string.Empty, "GitVersionTask")); - } -} \ No newline at end of file diff --git a/src/GitVersionTask/GenerateGitVersionInformation.cs b/src/GitVersionTask/GenerateGitVersionInformation.cs index cc5664561d..2e37e3975f 100644 --- a/src/GitVersionTask/GenerateGitVersionInformation.cs +++ b/src/GitVersionTask/GenerateGitVersionInformation.cs @@ -8,12 +8,8 @@ namespace GitVersionTask public class GenerateGitVersionInformation : GitVersionTaskBase { - TaskLogger logger; - public GenerateGitVersionInformation() { - logger = new TaskLogger(this); - Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s)); } [Required] @@ -42,12 +38,12 @@ public override bool Execute() } catch (WarningException errorException) { - logger.LogWarning(errorException.Message); + this.LogWarning(errorException.Message); return true; } catch (Exception exception) { - logger.LogError("Error occurred: " + exception); + this.LogError("Error occurred: " + exception); return false; } } diff --git a/src/GitVersionTask/GetVersion.cs b/src/GitVersionTask/GetVersion.cs index a51126422e..07f8be237f 100644 --- a/src/GitVersionTask/GetVersion.cs +++ b/src/GitVersionTask/GetVersion.cs @@ -7,12 +7,8 @@ namespace GitVersionTask public class GetVersion : GitVersionTaskBase { - TaskLogger logger; - public GetVersion() { - logger = new TaskLogger(this); - Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s)); } [Required] @@ -128,12 +124,12 @@ public override bool Execute() } catch (WarningException errorException) { - logger.LogWarning(errorException.Message); + this.LogWarning(errorException.Message); return true; } catch (Exception exception) { - logger.LogError("Error occurred: " + exception); + this.LogError("Error occurred: " + exception); return false; } } diff --git a/src/GitVersionTask/GitVersionTaskBase.cs b/src/GitVersionTask/GitVersionTaskBase.cs index 3337ad8fb6..caeb254ed2 100644 --- a/src/GitVersionTask/GitVersionTaskBase.cs +++ b/src/GitVersionTask/GitVersionTaskBase.cs @@ -3,6 +3,7 @@ using GitVersion; using GitVersion.Helpers; + using Microsoft.Build.Framework; using Microsoft.Build.Utilities; public abstract class GitVersionTaskBase : Task @@ -13,11 +14,32 @@ protected GitVersionTaskBase() { var fileSystem = new FileSystem(); executeCore = new ExecuteCore(fileSystem); + GitVersion.Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s)); } protected ExecuteCore ExecuteCore { get { return executeCore; } } + + public void LogDebug(string message) + { + this.BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Low)); + } + + public void LogWarning(string message) + { + this.BuildEngine.LogWarningEvent(new BuildWarningEventArgs(string.Empty, string.Empty, null, 0, 0, 0, 0, message, string.Empty, "GitVersionTask")); + } + + public void LogInfo(string message) + { + this.BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Normal)); + } + + public void LogError(string message, string file = null) + { + this.BuildEngine.LogErrorEvent(new BuildErrorEventArgs(string.Empty, string.Empty, file, 0, 0, 0, 0, message, string.Empty, "GitVersionTask")); + } } } \ No newline at end of file diff --git a/src/GitVersionTask/TaskLogger.cs b/src/GitVersionTask/TaskLogger.cs deleted file mode 100644 index ba33729ed8..0000000000 --- a/src/GitVersionTask/TaskLogger.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Microsoft.Build.Framework; - -class TaskLogger -{ - ITask task; - - public TaskLogger(ITask task) - { - this.task = task; - } - - public void LogWarning(string message) - { - task.BuildEngine.LogWarningEvent(new BuildWarningEventArgs(string.Empty, string.Empty, null, 0, 0, 0, 0, message, string.Empty, "GitVersionTask")); - } - - public void LogInfo(string message) - { - task.BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Normal)); - } - - public void LogError(string message, string file = null) - { - task.BuildEngine.LogErrorEvent(new BuildErrorEventArgs(string.Empty, string.Empty, file, 0, 0, 0, 0, message, string.Empty, "GitVersionTask")); - } -} \ No newline at end of file diff --git a/src/GitVersionTask/UpdateAssemblyInfo.cs b/src/GitVersionTask/UpdateAssemblyInfo.cs index b8fe5daa67..125575022c 100644 --- a/src/GitVersionTask/UpdateAssemblyInfo.cs +++ b/src/GitVersionTask/UpdateAssemblyInfo.cs @@ -9,12 +9,8 @@ namespace GitVersionTask public class UpdateAssemblyInfo : GitVersionTaskBase { - TaskLogger logger; - public UpdateAssemblyInfo() { - logger = new TaskLogger(this); - Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s)); } [Required] @@ -46,12 +42,12 @@ public override bool Execute() } catch (WarningException errorException) { - logger.LogWarning(errorException.Message); + this.LogWarning(errorException.Message); return true; } catch (Exception exception) { - logger.LogError("Error occurred: " + exception); + this.LogError("Error occurred: " + exception); return false; } } diff --git a/src/GitVersionTask/WriteVersionInfoToBuildLog.cs b/src/GitVersionTask/WriteVersionInfoToBuildLog.cs index c11a96890f..b2412132a9 100644 --- a/src/GitVersionTask/WriteVersionInfoToBuildLog.cs +++ b/src/GitVersionTask/WriteVersionInfoToBuildLog.cs @@ -7,12 +7,8 @@ namespace GitVersionTask public class WriteVersionInfoToBuildLog : GitVersionTaskBase { - readonly TaskLogger logger; - public WriteVersionInfoToBuildLog() { - logger = new TaskLogger(this); - Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s)); } [Required] @@ -29,12 +25,12 @@ public override bool Execute() } catch (WarningException errorException) { - logger.LogWarning(errorException.Message); + this.LogWarning(errorException.Message); return true; } catch (Exception exception) { - logger.LogError("Error occurred: " + exception); + this.LogError("Error occurred: " + exception); return false; } } @@ -54,12 +50,12 @@ void WriteIntegrationParameters(IEnumerable applicableBuildServers { foreach (var buildServer in applicableBuildServers) { - logger.LogInfo(string.Format("Executing GenerateSetVersionMessage for '{0}'.", buildServer.GetType().Name)); - logger.LogInfo(buildServer.GenerateSetVersionMessage(variables)); - logger.LogInfo(string.Format("Executing GenerateBuildLogOutput for '{0}'.", buildServer.GetType().Name)); + this.LogInfo(string.Format("Executing GenerateSetVersionMessage for '{0}'.", buildServer.GetType().Name)); + this.LogInfo(buildServer.GenerateSetVersionMessage(variables)); + this.LogInfo(string.Format("Executing GenerateBuildLogOutput for '{0}'.", buildServer.GetType().Name)); foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(buildServer, variables)) { - logger.LogInfo(buildParameter); + this.LogInfo(buildParameter); } } }