diff --git a/src/ScriptBuilderTask.Tests/CSharpCodeGenerationTests.Generates.approved.cs b/src/ScriptBuilderTask.Tests/CSharpCodeGenerationTests.Generates.approved.cs index 9c6659d..de6ba84 100644 --- a/src/ScriptBuilderTask.Tests/CSharpCodeGenerationTests.Generates.approved.cs +++ b/src/ScriptBuilderTask.Tests/CSharpCodeGenerationTests.Generates.approved.cs @@ -4,27 +4,39 @@ using System.Runtime.CompilerServices; [CompilerGenerated] -public static class CounterCreator +public static class CounterCreator { - public static void Create() + public static void Create() { var counterCreationCollection = new CounterCreationDataCollection(Counters); try { var categoryName = "NServiceBus"; + if (PerformanceCounterCategory.Exists(categoryName)) { - PerformanceCounterCategory.Delete(categoryName); + foreach (CounterCreationData counter in counterCreationCollection) + { + if (!PerformanceCounterCategory.CounterExists(counter.CounterName, categoryName)) + { + PerformanceCounterCategory.Delete(categoryName); + break; + } + } + } + + if (PerformanceCounterCategory.Exists(categoryName) == false) + { + PerformanceCounterCategory.Create( + categoryName: categoryName, + categoryHelp: "NServiceBus statistics", + categoryType: PerformanceCounterCategoryType.MultiInstance, + counterData: counterCreationCollection); } - PerformanceCounterCategory.Create( - categoryName: categoryName, - categoryHelp: "NServiceBus statistics", - categoryType: PerformanceCounterCategoryType.MultiInstance, - counterData: counterCreationCollection); PerformanceCounter.CloseSharedResources(); } - catch (Exception ex) when (ex is SecurityException || ex is UnauthorizedAccessException) + catch (Exception ex) when(ex is SecurityException || ex is UnauthorizedAccessException) { throw new Exception("Execution requires elevated permissions", ex); } diff --git a/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.Generates.approved.ps1 b/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.Generates.approved.ps1 index 215f60d..53d1d67 100644 --- a/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.Generates.approved.ps1 +++ b/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.Generates.approved.ps1 @@ -1,16 +1,16 @@ #requires -RunAsAdministrator Function InstallNSBPerfCounters { - + $category = @{Name="NServiceBus"; Description="NServiceBus statistics"} $counters = New-Object System.Diagnostics.CounterCreationDataCollection $counters.AddRange(@( New-Object System.Diagnostics.CounterCreationData "SLA violation countdown", "Seconds until the SLA for this endpoint is breached.", NumberOfItems32 New-Object System.Diagnostics.CounterCreationData "Critical Time Average", "The time it took from sending to processing the message.", AverageTimer32 - New-Object System.Diagnostics.CounterCreationData "Critical Time AverageBase", "The time it took from sending to processing the message.", AverageBase + New-Object System.Diagnostics.CounterCreationData "Critical Time AverageBase", "The time it took from sending to processing the message.", AverageBase New-Object System.Diagnostics.CounterCreationData "Critical Time", "The time it took from sending to processing the message.", NumberOfItems32 New-Object System.Diagnostics.CounterCreationData "Processing Time Average", "The time it took to successfully process a message.", AverageTimer32 - New-Object System.Diagnostics.CounterCreationData "Processing Time AverageBase", "The time it took to successfully process a message.", AverageBase + New-Object System.Diagnostics.CounterCreationData "Processing Time AverageBase", "The time it took to successfully process a message.", AverageBase New-Object System.Diagnostics.CounterCreationData "Processing Time", "The time it took to successfully process a message.", NumberOfItems32 New-Object System.Diagnostics.CounterCreationData "# of msgs failures / sec", "The current number of failed processed messages by the transport per second.", RateOfCountsPerSecond32 New-Object System.Diagnostics.CounterCreationData "# of msgs successfully processed / sec", "The current number of messages processed successfully by the transport per second.", RateOfCountsPerSecond32 @@ -18,10 +18,28 @@ Function InstallNSBPerfCounters { New-Object System.Diagnostics.CounterCreationData "Retries", "A message has been scheduled for retry (FLR or SLR)", RateOfCountsPerSecond32 )) + if ([System.Diagnostics.PerformanceCounterCategory]::Exists($category.Name)) { - [System.Diagnostics.PerformanceCounterCategory]::Delete($category.Name) + + foreach($counter in $counters){ + $exists = [System.Diagnostics.PerformanceCounterCategory]::CounterExists($counter.CounterName, $category.Name) + if (!$exists){ + Write-Host "One or more counters are missing.The performance counter category will be recreated" + [System.Diagnostics.PerformanceCounterCategory]::Delete($category.Name) + + break + } + } } - [void] [System.Diagnostics.PerformanceCounterCategory]::Create($category.Name, $category.Description, [System.Diagnostics.PerformanceCounterCategoryType]::MultiInstance, $counters) + + if (![System.Diagnostics.PerformanceCounterCategory]::Exists($category.Name)) { + Write-Host "Creating the performance counter category" + [void] [System.Diagnostics.PerformanceCounterCategory]::Create($category.Name, $category.Description, [System.Diagnostics.PerformanceCounterCategoryType]::MultiInstance, $counters) + } + else { + Write-Host "No performance counters have to be created" + } + [System.Diagnostics.PerformanceCounter]::CloseSharedResources() } InstallNSBPerfCounters diff --git a/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.cs b/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.cs index 811fe5c..b4e94b9 100644 --- a/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.cs +++ b/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Runtime.CompilerServices; using ApprovalTests; + using ApprovalTests.Reporters; using NServiceBus.Metrics.PerformanceCounters; using NUnit.Framework; @@ -34,6 +35,8 @@ public void SetUp() [MethodImpl(MethodImplOptions.NoInlining)] public void Generates() { + GenericDiffReporter.RegisterTextFileTypes(".ps1"); + task.Execute(); var powershell = Directory.EnumerateFiles(tempPath, "*.ps1", SearchOption.AllDirectories).Single(); diff --git a/src/ScriptBuilderTask/CSharpCounterWriter.cs b/src/ScriptBuilderTask/CSharpCounterWriter.cs index 2e0e2bb..22d2459 100644 --- a/src/ScriptBuilderTask/CSharpCounterWriter.cs +++ b/src/ScriptBuilderTask/CSharpCounterWriter.cs @@ -52,27 +52,39 @@ public static void WriteCode(string scriptPath, IEnumerable using System.Runtime.CompilerServices; [CompilerGenerated] -public static class CounterCreator +public static class CounterCreator {{ - public static void Create() + public static void Create() {{ var counterCreationCollection = new CounterCreationDataCollection(Counters); try {{ var categoryName = ""NServiceBus""; + if (PerformanceCounterCategory.Exists(categoryName)) {{ - PerformanceCounterCategory.Delete(categoryName); + foreach (CounterCreationData counter in counterCreationCollection) + {{ + if (!PerformanceCounterCategory.CounterExists(counter.CounterName, categoryName)) + {{ + PerformanceCounterCategory.Delete(categoryName); + break; + }} + }} + }} + + if (PerformanceCounterCategory.Exists(categoryName) == false) + {{ + PerformanceCounterCategory.Create( + categoryName: categoryName, + categoryHelp: ""NServiceBus statistics"", + categoryType: PerformanceCounterCategoryType.MultiInstance, + counterData: counterCreationCollection); }} - PerformanceCounterCategory.Create( - categoryName: categoryName, - categoryHelp: ""NServiceBus statistics"", - categoryType: PerformanceCounterCategoryType.MultiInstance, - counterData: counterCreationCollection); PerformanceCounter.CloseSharedResources(); }} - catch (Exception ex) when (ex is SecurityException || ex is UnauthorizedAccessException) + catch (Exception ex) when(ex is SecurityException || ex is UnauthorizedAccessException) {{ throw new Exception(""Execution requires elevated permissions"", ex); }} diff --git a/src/ScriptBuilderTask/PowerShellCounterWriter.cs b/src/ScriptBuilderTask/PowerShellCounterWriter.cs index 7c5b6c5..461e338 100644 --- a/src/ScriptBuilderTask/PowerShellCounterWriter.cs +++ b/src/ScriptBuilderTask/PowerShellCounterWriter.cs @@ -25,8 +25,8 @@ public static void WriteScript(string scriptPath, IEnumerable