【发布时间】:2016-09-08 07:20:10
【问题描述】:
我在使用 Azure 辅助角色创建和更新自定义性能计数器时遇到一个奇怪的问题,以跟踪与我的 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
我只找到one post which indicates a similar issue,但唯一的建议是确保它已初始化,因为他们将问题归结为“用于存储性能计数器变量的未初始化内存块” - 但我已经尝试过这没有成功。
请注意,我认为这不是 perfmon 问题,因为我通过 perfmon 看到了这个问题,而且我使用 Azure 诊断导出了计数器并且都显示了值。
有人有什么想法吗?
【问题讨论】:
标签: c# azure performancecounter azure-worker-roles azure-diagnostics