diff --git a/src/ScriptBuilder/CSharpCounterWriter.cs b/src/ScriptBuilder/CSharpCounterWriter.cs index 39b0466..bc84923 100644 --- a/src/ScriptBuilder/CSharpCounterWriter.cs +++ b/src/ScriptBuilder/CSharpCounterWriter.cs @@ -41,26 +41,39 @@ public static void WriteCode(string scriptPath, IEnumerable tim 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/ScriptBuilder/PowerShellCounterWriter.cs b/src/ScriptBuilder/PowerShellCounterWriter.cs index c67a8ef..7821f1a 100644 --- a/src/ScriptBuilder/PowerShellCounterWriter.cs +++ b/src/ScriptBuilder/PowerShellCounterWriter.cs @@ -35,22 +35,37 @@ public static void WriteScript(string scriptPath, IEnumerable t } } - const string Template = @" -#requires -RunAsAdministrator + const string Template = @"#requires -RunAsAdministrator Function InstallNSBPerfCounters {{ - + $category = @{{Name=""NServiceBus""; Description=""NServiceBus statistics""}} $counters = New-Object System.Diagnostics.CounterCreationDataCollection $counters.AddRange(@( {0} )) + 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 -"; +InstallNSBPerfCounters"; } } \ No newline at end of file diff --git a/src/ScriptBuilderTask.Tests/CSharpCodeGenerationTests.Generates.approved.cs b/src/ScriptBuilderTask.Tests/CSharpCodeGenerationTests.Generates.approved.cs index e7032c7..63382af 100644 --- a/src/ScriptBuilderTask.Tests/CSharpCodeGenerationTests.Generates.approved.cs +++ b/src/ScriptBuilderTask.Tests/CSharpCodeGenerationTests.Generates.approved.cs @@ -4,26 +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 5f8f754..8040fa7 100644 --- a/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.Generates.approved.ps1 +++ b/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.Generates.approved.ps1 @@ -1,7 +1,6 @@ - #requires -RunAsAdministrator Function InstallNSBPerfCounters { - + $category = @{Name="NServiceBus"; Description="NServiceBus statistics"} $counters = New-Object System.Diagnostics.CounterCreationDataCollection $counters.AddRange(@( @@ -13,10 +12,27 @@ Function InstallNSBPerfCounters { New-Object System.Diagnostics.CounterCreationData "# of msgs successfully processed / sec", "The current number of messages processed successfully by the transport per second.", 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 +InstallNSBPerfCounters \ No newline at end of file diff --git a/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.cs b/src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.cs index 811fe5c..c4832f1 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; @@ -36,6 +37,8 @@ public void Generates() { task.Execute(); + GenericDiffReporter.RegisterTextFileTypes(".ps1"); + var powershell = Directory.EnumerateFiles(tempPath, "*.ps1", SearchOption.AllDirectories).Single(); Approvals.VerifyFile(powershell); }