C# Azure Worker角色中自定义性能计数器中的随机高值

C# Azure Worker角色中自定义性能计数器中的随机高值,c#,azure,performancecounter,azure-worker-roles,azure-diagnostics,C#,Azure,Performancecounter,Azure Worker Roles,Azure Diagnostics,我在使用Azure Worker角色创建和更新自定义性能计数器时遇到了一个奇怪的问题,以跟踪到我的SOAP WCF服务的打开连接数 在RoleEntryPoint.Run()方法中,我确保创建了计数器: if (PerformanceCounterCategory.Exists("WorkerRoleEndpointConnections")) return true; var soapCounter = new CounterCreationData { CounterNam

我在使用Azure Worker角色创建和更新自定义性能计数器时遇到了一个奇怪的问题,以跟踪到我的SOAP WCF服务的打开连接数

RoleEntryPoint.Run()方法中,我确保创建了计数器:

if (PerformanceCounterCategory.Exists("WorkerRoleEndpointConnections"))
    return true;

var soapCounter = new CounterCreationData
{
    CounterName = "# SOAP Connections",
    CounterHelp = "Current number of open SOAP connections",
    CounterType = PerformanceCounterType.NumberOfItems32
};
counters.Add(soapCounter);

PerformanceCounterCategory.Create("WorkerRoleEndpointConnections", "Connection statistics for endpoint types", PerformanceCounterCategoryType.MultiInstance, counters);
这似乎可以正常工作,因为在角色启动时正确创建了计数器。我已经通过RDP>Perfmon进行了检查

检查/创建计数器后,我立即使用
PerformanceCounter
对象创建计数器的单个引用:

m_SoapConnectionCounter = new PerformanceCounter
{
    CategoryName = "WorkerRoleEndpointConnections",
    CounterName = "# SOAP Connections",
    MachineName = ".",
    InstanceName = RoleEnvironment.CurrentRoleInstance.Id,
    ReadOnly = false,
    RawValue = 0L // Initialisation value added in an attempt to fix issue
};
然后在需要时使用以下工具进行更新:

public async Task UpdateSoapConnectionCounter(int connections)
{
    await UpdateCounter(m_SoapConnectionCounter, connections);
}

private async Task UpdateCounter(PerformanceCounter counter, int connections)
{
    await Task.Run(() =>
    {
        counter.RawValue = connections; // Implicit cast to long
    });
}
我的想法是,我只是在需要时以一种“火而忘”的方式覆盖该值

问题在于,这似乎只是偶尔起作用。计数器将随机显示一些略大于
int.MaxValue
的大值,例如
21474836353
。奇怪的是,一旦发生这种情况,它就永远不会回到“正常”值。

我尝试过删除计数器,但即使是新创建的计数器,它们似乎也会采用此值(有时从一开始),即使在创建
PerformanceCounter
对象时添加了初始化值零

我有点不知道问题出在哪里。起初我认为最初创建计数器时这只是一个问题,但现在我也观察到计数器更改为这些值,即0、1、2、1、21474836350

我只找到了,但唯一的建议是确保它已初始化,因为他们将问题归结为“用于存储性能计数器变量的未初始化内存块”——但我尝试过这样做,但没有成功

请注意,我不认为这是perfmon问题,因为我通过perfmon看到了这一点,并且我使用Azure诊断导出了计数器,两者都显示了值


有人有什么想法吗?

好的,经过大量调试后,结果表明问题是因为我有时给计数器赋值为负数


似乎将
RawValue
属性设置为负数会导致某种位掩蔽,因此它实际上会指定一个与int.MaxValue-(负值)等效的值。

我遇到了完全相同的问题,但在桌面上。我试图通过专门将原始值设置为-1
pc.RawValue=-1来重新编程但它没有。你有更多的细节吗?或者你能分享你为防止这种情况发生所做的事情吗?