c#/ perfmon中的自定义性能计数器

前端之家收集整理的这篇文章主要介绍了c#/ perfmon中的自定义性能计数器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,我正在尝试创建一个自定义性能计数器,用于perfmon.以下代码运行良好,但我有一个问题..

有了这个解决方案,我有一个更新性能计数器值的计时器,但是我不想运行这个可执行文件获取我需要的数据.这就是我想能够安装计数器作为一次性的事情,然后对数据进行perfmon查询(就像它对所有预安装的计数器一样).

我怎样才能做到这一点?

using System;
using System.Diagnostics;
using System.Net.NetworkInformation;

namespace PerfCounter
{
class PerfCounter
{
    private const String categoryName = "Custom category";
    private const String counterName = "Total bytes received";
    private const String categoryHelp = "A category for custom performance counters";
    private const String counterHelp = "Total bytes received on network interface";
    private const String lanName = "Local Area Connection"; // change this to match your network connection
    private const int sampleRateInMillis = 1000;
    private const int numberofSamples = 100;

    private static NetworkInterface lan = null;
    private static PerformanceCounter perfCounter;

    static void Main(string[] args)
    {
        setupLAN();
        setupCategory();
        createCounters();
        updatePerfCounters();
    }

    private static void setupCategory()
    {
        if (!PerformanceCounterCategory.Exists(categoryName))
        {
            CounterCreationDataCollection counterCreationDataCollection = new CounterCreationDataCollection();
            CounterCreationData totalBytesReceived = new CounterCreationData();
            totalBytesReceived.CounterType = PerformanceCounterType.NumberOfItems64;
            totalBytesReceived.CounterName = counterName;
            counterCreationDataCollection.Add(totalBytesReceived);
            PerformanceCounterCategory.Create(categoryName,categoryHelp,PerformanceCounterCategoryType.MultiInstance,counterCreationDataCollection);
        }
        else
            Console.WriteLine("Category {0} exists",categoryName);
    }

    private static void createCounters() {
        perfCounter = new PerformanceCounter(categoryName,counterName,false);
        perfCounter.RawValue = getTotalBytesReceived();
    }

    private static long getTotalBytesReceived()
    {
        return lan.GetIPv4Statistics().BytesReceived;
    }

    private static void setupLAN()
    {
        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface networkInterface in interfaces)
        {
            if (networkInterface.Name.Equals(lanName))
                lan = networkInterface;
        }
    }
    private static void updatePerfCounters()
    {
        for (int i = 0; i < numberofSamples; i++)
        {
            perfCounter.RawValue = getTotalBytesReceived();
            Console.WriteLine("perfCounter.RawValue = {0}",perfCounter.RawValue);
            System.Threading.Thread.Sleep(sampleRateInMillis);
        }
    }
}

}

解决方法

在Win32中,性能计数器通过让PerfMon加载提供计数器值的DLL来工作.

在.NET中,此DLL是一个存根,它使用共享内存与正在运行的.NET进程通信.该过程定期将新值推送到共享内存块,DLL使它们可用作性能计数器.

因此,基本上,您可能必须在本机代码中实现性能计数器DLL,因为.NET性能计数器假定正在运行进程.

原文链接:https://www.f2er.com/csharp/99463.html

猜你在找的C#相关文章