Azure:使用System.Diagnostics.PerformanceCounter

Azure:使用System.Diagnostics.PerformanceCounter,azure,system.diagnostics,performancecounter,Azure,System.diagnostics,Performancecounter,我知道Microsoft.WindowsAzure.Diagnostics性能监视。我正在寻找一些更实时的东西,比如使用System.Diagnostics.PerformanceCounter 其思想是,实时信息将在AJAX请求时发送 使用azure中可用的性能计数器: 以下代码可以工作(或者至少在Azure Compute Emulator中,我还没有在Azure的部署中尝试过): 在MSDN页面的下面是我想使用的另一个计数器: 网络接口(*)\每秒接收字节数 我尝试创建性能计数器: pro

我知道Microsoft.WindowsAzure.Diagnostics性能监视。我正在寻找一些更实时的东西,比如使用System.Diagnostics.PerformanceCounter 其思想是,实时信息将在AJAX请求时发送

使用azure中可用的性能计数器:

以下代码可以工作(或者至少在Azure Compute Emulator中,我还没有在Azure的部署中尝试过):

在MSDN页面的下面是我想使用的另一个计数器: 网络接口(*)\每秒接收字节数

我尝试创建性能计数器:

protected PerformanceCounter FDiagNetSent = new PerformanceCounter("Network Interface", "Bytes Received/sec", "*");
但随后我收到一个异常,表示“*”不是有效的实例名

这也不起作用:

protected PerformanceCounter FDiagNetSent = new PerformanceCounter("Network Interface(*)", "Bytes Received/sec");

是否不赞成在Azure中直接使用性能计数器?

您遇到的问题与Windows Azure无关,但通常与性能计数器有关。顾名思义,网络接口(*)\Bytes Received/sec是特定网络接口的性能计数器

要初始化性能计数器,您需要使用要从中获取度量的实例(网络接口)的名称对其进行初始化:

var counter = new PerformanceCounter("Network Interface",
        "Bytes Received/sec", "Intel[R] WiFi Link 1000 BGN");
正如您从代码中看到的,我正在指定网络接口的名称。在Windows Azure中,您无法控制服务器配置(硬件、Hyper-V虚拟网卡等),因此我不建议使用网络接口的名称

这就是为什么枚举实例名来初始化计数器可能更安全:

var counter = new PerformanceCounter("Network Interface",
        "Bytes Received/sec", "Intel[R] WiFi Link 1000 BGN");
var category = new PerformanceCounterCategory("Network Interface");
foreach (var instance in category.GetInstanceNames())
{
    var counter = new PerformanceCounter("Network Interface",
                                               "Bytes Received/sec", instance);
    ...
}