none
Unable to delete custom performance counter category RRS feed

  • Question

  • Hi, 

    I have created a performance counter category and I am not able to delete it. Running this:

    ```powershell

    $categoryName = "NServiceBus"
    [System.Diagnostics.PerformanceCounterCategory]::Exists($categoryName)
    [System.Diagnostics.PerformanceCounterCategory]::delete($categoryName)
    ```

    Returns this
    ```powershell
    True
    Exception calling "Delete" with "1" argument(s): "Cannot create file mapping."
    At line:6 char:1
    + [System.Diagnostics.PerformanceCounterCategory]::delete($categoryName ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : InvalidOperationException
     ```

    I have followed the instructions in [this knowledgebase article](https://support.microsoft.com/en-au/help/2554336/how-to-manually-rebuild-performance-counters-for-windows-server-2008-6) to rebuild the counters in 32-bit and 64-bit, as well as resync-ing WMI. I have restarted the computer. I have verified that the counter details are present in the registry and that the DisablePerformanceCounters key is not present.

    I dug up [the code that is throwing the exception](https://github.com/microsoft/referencesource/blob/4.6.2/System/services/monitoring/system/diagnosticts/SharedPerformanceCounter.cs#L1614) to try and better understand what is going on. I do not believe I am somehow exhausting the 14 retry attempts as the exception is thrown immediately.

    The [documentation for the Delete(...) call](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.performancecountercategory.delete?view=netframework-4.6.1) suggests that it will throw InvalidOperationException if this is not a custom category but I know that it is because I created it with a call to [Create(...)](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.performancecountercategory.create?view=netframework-4.6.1)

    I am trying to delete the custom performance counter category from an elevated command prompt so I do not think this is permissions-related. It does seem that the system is not able to create (or open) the memory mapped file with the specified name but I don't know what conditions can cause that.

    This is .NET 4.6.1 running on Windows 2008 R2. I now have several servers somehow stuck in this state.

    Thanks,

    Mike

    $categoryName = "NServiceBus

    [System.Diagnostics.PerformanceCounterCategory]::Exists($categoryName

    [System.Diagnostics.PerformanceCounterCategory]::delete($categoryNam

    $categoryName = "NServiceBus"

    [System.Diagnostics.PerformanceCounterCategory]::Exists($categoryName)

    [System.Diagnostics.PerformanceCounterCategory]::delete($categoryName)

    Tuesday, January 7, 2020 9:30 AM

All replies

  • Hi,

    Thank you for posting here.

    According to your question, I make a test on my side.

    I increase the permission of my application which help me have administrative privileges.

    Then I use the following code to create and delete custom performance counter category.

                string customCategory = "Custom Performance Counter Category";
    
                if (!PerformanceCounterCategory.Exists(customCategory))
                {
                    CounterCreationDataCollection counterCreationDataCollection = new CounterCreationDataCollection();
    
                    counterCreationDataCollection.Add(new CounterCreationData("Counter 1", "Sample Counter 1", PerformanceCounterType.ElapsedTime));
    
                    counterCreationDataCollection.Add(new CounterCreationData("Counter 2", "Sample Counter 2", PerformanceCounterType.SampleCounter));
    
                    counterCreationDataCollection.Add(new CounterCreationData("Counter 3", "Sample Counter 3", PerformanceCounterType.SampleCounter));
    
                    PerformanceCounterCategory.Create(customCategory, "This is just an example", PerformanceCounterCategoryType.SingleInstance, counterCreationDataCollection);
                }
                else
                {
                    Console.WriteLine("delete the Custom Performance Counter Category");
                    PerformanceCounterCategory.Delete(customCategory);
                }
                var performanceCounterCategories = PerformanceCounterCategory.GetCategories();
    
                foreach (PerformanceCounterCategory performanceCounterCategory in performanceCounterCategories)
                {
                    if (performanceCounterCategory.CategoryName.Contains("Custom"))
                    {
                        Console.WriteLine(performanceCounterCategory.CategoryName);
                    }
                    
                }       
               
                Console.ReadLine();

    Create:

    Delete:

    I get no exception in my application, and this is .NET 4.6.1 running on windows 10 1903.

    Could you provide more information about how you create your performance counter category or more details about your performance counter category?

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 8, 2020 6:54 AM
  • Hi,

    Thank you for your response. This is the code that we use to create the performance counter category.

    [CmdletBinding()]
    param(
      [switch]$ForceRecreate
    )
    
    #requires -RunAsAdministrator
    
    $category = @{Name="NServiceBus"; Description="NServiceBus statistics"}
    $counters = New-Object System.Diagnostics.CounterCreationDataCollection
    $counters.AddRange(@(
    	New-Object System.Diagnostics.CounterCreationData "SLA violation countdown", "Duration in seconds until the configured Service Level Agreement (SLA) for this endpoint is breached. This is an instantaneous snapshot, not an average over the time interval.",  NumberOfItems32
    	New-Object System.Diagnostics.CounterCreationData "Critical Time Average", "Average duration in seconds for sending and processing of all messages during the sample interval. Useful to understand how long a new message added to the queue right now will take to be processed.",  AverageTimer32
    	New-Object System.Diagnostics.CounterCreationData "Critical Time AverageBase", "A base counter used to calculate the Critical Time Average",  AverageBase
    	New-Object System.Diagnostics.CounterCreationData "Critical Time", "Duration in seconds for sending and processing the last processed message, useful to understand how long a new message added to the queue right now will take to be processed. This is an instantaneous snapshot, not an average over the time interval.",  NumberOfItems32
    	New-Object System.Diagnostics.CounterCreationData "Processing Time Average", "Average duration in seconds of all successfully processed messages during the sample interval.",  AverageTimer32
    	New-Object System.Diagnostics.CounterCreationData "Processing Time AverageBase", "A base counter used to calculate the Processing Time.",  AverageBase
    	New-Object System.Diagnostics.CounterCreationData "Processing Time", "Duration in seconds of the last successfully processed message. This is an instantaneous snapshot, not an average over the time interval.",  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
    	New-Object System.Diagnostics.CounterCreationData "# of msgs pulled from the input queue /sec", "The current number of messages pulled from the input queue by the transport per second.",  RateOfCountsPerSecond32
    	New-Object System.Diagnostics.CounterCreationData "Retries", "A message has been scheduled for retry (FLR or SLR)",  RateOfCountsPerSecond32
    
    ))
    
    if ([System.Diagnostics.PerformanceCounterCategory]::Exists($category.Name)) {
    
    	if($ForceRecreate) {
    		Write-Host "Option -ForceRecreate was used. The performance counter category will be recreated"
    		[System.Diagnostics.PerformanceCounterCategory]::Delete($category.Name)
    	} 
    	else {
    		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
    			}
    		}
    	}
    }
    
    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()

    Regards,

    Mike

    Tuesday, January 14, 2020 5:02 PM
  • Hi mikeminutillo,

    Thanks for your feedback.

    I note that your code is more related to PowerShell, so I suggest you ask your question in PowerShell forum for more help.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 16, 2020 9:08 AM