如何添加一个新的计数器,以现有的性能计数器类别,而不删除旧的计数器?

编程入门 行业动态 更新时间:2024-10-10 07:24:44
本文介绍了如何添加一个新的计数器,以现有的性能计数器类别,而不删除旧的计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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); } }

更多推荐

如何添加一个新的计数器,以现有的性能计数器类别,而不删除旧的计数器?

本文发布于:2023-10-18 15:34:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504643.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计数器   而不   性能   类别

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!