RawFraction性能计数器甚至删除性能类别后仍继续其状态 [英] RawFraction performance counter persists its state even after deleting the performance category

查看:148
本文介绍了RawFraction性能计数器甚至删除性能类别后仍继续其状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建并正确设置的性能计数器,但是当我删除的分类,重建类具有相同的名称,并添加/更新计数器这一类,它未能更新计数器和它的价值。



下面的代码运行正常首次但不是第二时间。该代码删除删除类别是不需要的,但现在我希望能够删除每次我们部署我们的应用程序存在的类别。



我永久哪有?删除如果不这样做或重置其值计数器

 私人的PerformanceCounter mainCounter; 
私人的PerformanceCounter mainCounterBase;
私人串类=TestPerformanceCounterTest;
公共无效测试()
{
//计数器设置

如果(PerformanceCounterCategory.Exists(类))
PerformanceCounterCategory.Delete(类); (!PerformanceCounterCategory.Exists(类))
如果
{
变种categoryCollection =新CounterCreationDataCollection();

变种计数器1 =新CounterCreationData(RawCounter1,,PerformanceCounterType.RawFraction);
变种计数器2 =新CounterCreationData(RawCounterBase1,,PerformanceCounterType.RawBase);
categoryCollection.Add(C1的);
categoryCollection.Add(C2的);


PerformanceCounterCategory.Create(类,PerformanceCounterCategoryType.SingleInstance,categoryCollection);

//等待,等待......
的Thread.Sleep(TimeSpan.FromSeconds(3));
}
//创建计数器
mainCounter =新的PerformanceCounter(类别,RawCounter1,FALSE);
mainCounterBase =新的PerformanceCounter(类别,RawCounterBase1,FALSE);
//复位值
mainCounter.RawValue = 0;
mainCounterBase.RawValue = 0;

//更新计数器
mainCounter.IncrementBy(10);
mainCounterBase.IncrementBy(20);
** Console.WriteLine(主计:+ mainCounter.RawValue); //犯规示值50这是运行第二次**
Console.WriteLine(主计基地:+ mainCounterBase.RawValue);
Console.WriteLine(主计下一个值:+ mainCounter.NextValue());
Console.WriteLine(主计基础下一个值:+ mainCounterBase.NextValue());
}


解决方案

我敢肯定这是由于Windows管理性能数据的方式。



从MSDN,的 PerformanceCounterCategory.Create方法(字符串,字符串,PerformanceCounterCategoryType,CounterCreationDataCollection)




注意
强烈建议新的性能计数器
类应用程序的执行过程中安装的应用程序,而不是
期间创建。这使得时间
操作系统刷新其注册的性能计数器
类别列表。如果列表中没有被刷新,使用
类的尝试将失败。




我没有第一手知识,但是这表明类别的添加或删除不是同步的操作。



要解决这个问题,你可能要更换你的第一个如果,而代替,像这样>:

 而(PerformanceCounterCategory.Exists(类))
{
PerformanceCounterCategory.Delete(类);
}

这是一个有点笨拙,但。最好的建议是不做柜台设置或推倒你需要它之​​前。相反,把它变成一个安装程序,或者最起码,创建一个单独的工具来安装/卸载。此外,您还可以创建一个PowerShell脚本安装/卸载。请参见 http://msdn.microsoft.com/en-us/library/ windowsazure / hh508994.aspx 一个例子。


I am creating and setting up the performance counters correctly but when I delete the category, recreate the category with the same name and add/update the counters to that category, it fails to update the counters and its values.

The following code runs fine for the first time but not the second time. The code to remove the "Delete category" is not needed right now but I want to be able to delete existing category each time we deploy our application.

How can I permanently delete the counter if its not doing so or reset its values?

    private PerformanceCounter mainCounter;
    private PerformanceCounter mainCounterBase;
    private string category = "TestPerformanceCounterTest";
    public void Test()
    {
                   //Counter setup

        if (PerformanceCounterCategory.Exists(category))
            PerformanceCounterCategory.Delete(category);
        if (!PerformanceCounterCategory.Exists(category))
        {
            var categoryCollection = new CounterCreationDataCollection();

            var counter1 = new CounterCreationData("RawCounter1", "", PerformanceCounterType.RawFraction);
            var counter2 = new CounterCreationData("RawCounterBase1", "", PerformanceCounterType.RawBase);
            categoryCollection.Add(counter1);
            categoryCollection.Add(counter2);


            PerformanceCounterCategory.Create(category, "", PerformanceCounterCategoryType.SingleInstance, categoryCollection);

            //  Wait and wait...
            Thread.Sleep(TimeSpan.FromSeconds(3));
        }
                    //create counters
                    mainCounter = new PerformanceCounter(category, "RawCounter1", false);
        mainCounterBase = new PerformanceCounter(category, "RawCounterBase1", false);
                    //reset values
                    mainCounter.RawValue = 0;
        mainCounterBase.RawValue = 0;

                    //update counter
                    mainCounter.IncrementBy(10);
        mainCounterBase.IncrementBy(20);
        **Console.WriteLine("Main counter: " +mainCounter.RawValue);//doesnt show value 50 the second time this is run**
        Console.WriteLine("Main counter Base: " + mainCounterBase.RawValue);
        Console.WriteLine("Main counter next value: " + mainCounter.NextValue());
        Console.WriteLine("Main counter base next value: " + mainCounterBase.NextValue());
    }

解决方案

I am pretty sure this is due to the way Windows manages performance data.

From MSDN, PerformanceCounterCategory.Create Method (String, String, PerformanceCounterCategoryType, CounterCreationDataCollection):

Note It is strongly recommended that new performance counter categories be created during the installation of the application, not during the execution of the application. This allows time for the operating system to refresh its list of registered performance counter categories. If the list has not been refreshed, the attempt to use the category will fail.

I don't have firsthand knowledge, but this suggests that the addition or deletion of categories is not a synchronous action.

To work around this, you might want to replace your first if with a while instead, like so:

while (PerformanceCounterCategory.Exists(category))
{
    PerformanceCounterCategory.Delete(category);
}

That's a little heavy-handed, though. Best recommendation is to not do the counter set up or tear down just before you need it. Instead, put it into an installer, or at the very least, create a separate tool to install/uninstall them. Also, you could create a Powershell script to install/uninstall them. See http://msdn.microsoft.com/en-us/library/windowsazure/hh508994.aspx for an example.

这篇关于RawFraction性能计数器甚至删除性能类别后仍继续其状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆