c#/perfmon中的自定义性能计数器
您好,我正在尝试创建一个用于perfmon的自定义性能计数器。下面的代码运行得很好,但是我有一个问题 在这个解决方案中,我有一个定时器来更新性能计数器的值,但是我不想运行这个可执行文件来获取我需要的数据。也就是说,我希望能够一次性安装计数器,然后对数据进行perfmon查询(就像它对所有预安装的计数器所做的那样) 我怎样才能做到这一点c#/perfmon中的自定义性能计数器,c#,performance,counter,perfmon,C#,Performance,Counter,Perfmon,您好,我正在尝试创建一个用于perfmon的自定义性能计数器。下面的代码运行得很好,但是我有一个问题 在这个解决方案中,我有一个定时器来更新性能计数器的值,但是我不想运行这个可执行文件来获取我需要的数据。也就是说,我希望能够一次性安装计数器,然后对数据进行perfmon查询(就像它对所有预安装的计数器所做的那样) 我怎样才能做到这一点 using System; using System.Diagnostics; using System.Net.NetworkInformation; nam
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);
}
}
}
使用系统;
使用系统诊断;
使用System.Net.NetworkInformation;
命名空间性能计数器
{
类性能计数器
{
private const String categoryName=“自定义类别”;
private const String counterName=“接收的字节总数”;
private const String categoryHelp=“自定义性能计数器的类别”;
private const String counterHelp=“网络接口上接收的总字节数”;
private const String lanName=“Local Area Connection”;//将其更改为匹配您的网络连接
私有常量int sampleRateInMillis=1000;
私有常量int numberofSamples=100;
专用静态网络接口lan=null;
专用静态性能计数器性能计数器;
静态void Main(字符串[]参数)
{
setupLAN();
设置类别();
createCounters();
updatePerfCounters();
}
私有静态void setupCategory()
{
如果(!PerformanceCounterCategory.Exists(categoryName))
{
CounterCreationDataCollection CounterCreationDataCollection=新的CounterCreationDataCollection();
CounterCreationData totalBytesReceived=新的CounterCreationData();
totalBytesReceived.CounterType=PerformanceCounterType.NumberOfItems64;
totalBytesReceived.CounterName=计数器名称;
添加(totalBytesReceived);
创建(categoryName、categoryHelp、PerformanceCounterCategoryType.MultiInstance、counterCreationDataCollection);
}
其他的
WriteLine(“类别{0}存在”,categoryName);
}
私有静态void createCounters(){
perfCounter=新的性能计数器(categoryName、counterName、false);
perfCounter.RawValue=GetTotalBytes接收();
}
私有静态长GetTotalBytes接收()
{
返回lan.GetIPv4Statistics().BytesReceived;
}
专用静态void setupLAN()
{
NetworkInterface[]interfaces=NetworkInterface.GetAllNetworkInterfaces();
foreach(网络接口网络接口接口中的网络接口)
{
if(networkInterface.Name.Equals(lanName))
lan=网络接口;
}
}
私有静态void updatePerfCounters()
{
对于(int i=0;i
}在Win32中,性能计数器通过让PerfMon加载提供计数器值的DLL来工作 在.NET中,此DLL是一个存根,它使用共享内存与正在运行的.NET进程通信。该进程定期将新值推送到共享内存块,DLL将它们作为性能计数器使用 因此,基本上,您可能必须在本机代码中实现性能计数器DLL,因为.NET性能计数器假定有一个进程正在运行