【问题标题】:unregistering a performance counters注销性能计数器
【发布时间】:2011-04-15 04:57:50
【问题描述】:

我已在我的应用程序中添加了性能自定义计数器以 操作是否发生的次数。

但现在我想注销它。 是否可以注销它删除它。

我在 C# 中使用过这个计数器。

创建性能类别的类和 为其添加计数器。

using System;
using System.Diagnostics;

namespace GroupChatLibrarySamples
{
    ///<summary>
    ///Helper class to demonstrate the setup of performance counters.
    ///</summary>
    public class PerformanceCounterHelper
    {
        //Total number of tests executed
        private PerformanceCounter _TotalFiles = null;
        /// <summary>
        /// Constructor
        /// </summary>
        public PerformanceCounterHelper()
        {
            // Set up the performance counter(s)
             if (!PerformanceCounterCategory.Exists("Total_Files","."))
             {
                //Create the collection container
                CounterCreationDataCollection counters = new CounterCreationDataCollection();
                ////Create counter #1 and add it to the collection
                CounterCreationData tests = new CounterCreationData();
                tests.CounterName = "Total_Files_Sent";
                tests.CounterHelp = "Total number of Files Sent";
                tests.CounterType = PerformanceCounterType.NumberOfItems32;
                counters.Add(tests);

                //Create the category and all of the counters.
                PerformanceCounterCategory.Create("Total_Files", "Performance Counter for checking no of files sent", counters);
             }
             try
             {
                 _TotalFiles = new PerformanceCounter();
                 _TotalFiles.CategoryName = "Total_Files";
                 _TotalFiles.CounterName = "Total_Files_Sent";
                 _TotalFiles.MachineName = ".";
                 _TotalFiles.ReadOnly = false;
             }
             catch (Exception e)
             {
                 Console.WriteLine(" {0} \n", e.StackTrace);

             }

        }


        public void IncrementCounters()
        {
            _TotalFiles.Increment();

        }
    }
}

计数器递增的代码:

    private void BeginSendFilesFinished(IAsyncResult ar) 
    {

        currentOperation.End("BeginSendFilesFinished", () => session.EndUploadFile(ar)); 
        Console.WriteLine(" File Count  : {0} \n", listoffiles.Count);
        if(sentFiles <  (listoffiles.Count-1))
        {
                sentFiles++;  
                string SampleFile = listoffiles[sentFiles].ToString();
                FileInfo filetoupload = new FileInfo(SampleFile);                        
                ChatRoomFileUploadJob uploadfilejob = new ChatRoomFileUploadJob(filetoupload);
                counterHelper.IncrementCounters();   
                currentOperation.Begin(string.Format("Send Files: # {0}", sentFiles), () => session.BeginUploadFile(uploadfilejob, BeginSendFilesFinished, null));

        }
        else
        {
            Log("Leave ChatRoom:");
            // After sending 10 chat msgs, leave the chat room (and rejoin).
            currentOperation.Begin("Leave ChatRoom:", () => session.BeginLeave(BeginLeaveChatRoomFinished, null));
        }
    }

感谢和问候, 塔齐姆。

【问题讨论】:

  • Could you please provide us the code where you register it?
  • 我已经添加了这个计数器,并使用 perfmon 监控它。但我想删除它知道或删除它或取消注册它。是否可以 ?任何链接和指针都将是有价值的。提前致谢
  • 可以删除计数器。 stackoverflow.com/questions/140149/…

标签: c# performance performancecounter


【解决方案1】:

这可能太明显了,但考虑到您正在尝试执行与 PerformanceCounterCategory.Create 操作相反的操作(我认为),您是否考虑过使用 PerformanceCounterCategory.Delete?根据文档:

删除类别及其 来自本地的相关计数器 电脑

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2011-04-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多