如何添加一个新的计数器,以现有的性能计数器类别,而不删除旧的计数器? [英] How to add a new counter to an existing performance counter category without deleting the old counters?

查看:174
本文介绍了如何添加一个新的计数器,以现有的性能计数器类别,而不删除旧的计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的计数器类别,而我需要添加一个新的计数器,而不会删除或重置任何现有的计数器。我怎样才能做到这一点?

I have a custom counter category, to which I need to add a new counter, without deleting or resetting any existing counters. How can I do this?

我试图用CounterExists(),但即使在我创作的柜台,我怎么能它关联到CounterCreationDataCollection项,并将其关联到我现有的反类?

I tried using CounterExists(), but even after I create the counter, how can I associate it to a CounterCreationDataCollection item and associate it to my existing counter category?

推荐答案

要做到这一点,最好的方法,我发现,尤其是因为似乎没有要对这个话题很多信息,是preserve现有的原料值,然后后该类别被删除并重新创建重新申请他们。

The best way to do this I found, especially since there doesn't seem to be much info on this topic, is to preserve the existing raw values and then re-apply them after the category is deleted and re-created.

/// <summary>
/// When deleting the Category, need to preserve the existing counter values
/// </summary>
static Dictionary<string, long> GetPreservedValues(string category, XmlNodeList nodes)
{
    Dictionary<string, long> preservedValues = new Dictionary<string, long>();

    foreach (XmlNode counterNode in nodes)
    {
        string counterName = counterNode.Attributes["name"].Value;

        if (PerformanceCounterCategory.CounterExists(counterName, category))
        {
            PerformanceCounter performanceCounter = new PerformanceCounter(category, counterName, false);
            preservedValues.Add(counterName, performanceCounter.RawValue);

            Console.WriteLine("Preserving {0} with a RawValue of {1}", counterName, performanceCounter.RawValue);
        }
        else
        {
            Console.WriteLine("Unable to preserve {0} because it doesn't exist", counterName);
        }
    }

    return preservedValues;
}

/// <summary>
/// Restore preserved values after the category has been re-created
/// </summary>
static void SetPreservedValues(string category, Dictionary<string, long> preservedValues)
{
    foreach (KeyValuePair<string, long> preservedValue in preservedValues)
    {
        PerformanceCounter performanceCounter = new PerformanceCounter(category, preservedValue.Key, false);
        performanceCounter.RawValue = preservedValue.Value;

        Console.WriteLine("Restoring {0} with a RawValue of {1}", preservedValue.Key, performanceCounter.RawValue);
    }
}

这篇关于如何添加一个新的计数器,以现有的性能计数器类别,而不删除旧的计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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