c# - Azure 辅助角色中自定义性能计数器的随机高值

标签 c# azure performancecounter azure-worker-roles azure-diagnostics

我在自定义性能计数器方面遇到了一个奇怪的问题,我正在从 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 诊断导出了计数器,并且都显示了该值。

有人有什么想法吗?

最佳答案

好的,经过多次调试后发现问题是因为有时我为计数器分配了负数。

似乎将 RawValue 属性设置为负数会导致某种位掩码,因此它实际上分配了一个相当于 int.MaxValue -(负值)的值。

关于c# - Azure 辅助角色中自定义性能计数器的随机高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39384799/

相关文章:

ios - Azure 移动服务推送不起作用

c# - 使用 API 删除和重新创建数据库时,Azure Cosmos DB 上的存储设置不断从“无限制”更改为“固定”

c# - 维护多个实例的处理顺序

.net - 以编程方式监视磁盘事件 (Windows)

c# - 如何对性能计数器类别进行版本控制?

c# - 使用不同 UI 语言的性能计数器

c# - 在 Visual Studio 中如何为调试和发布版本添加特殊情况?

c# - 如何解决我的代码中当前的问题?

c# - WCFTestClient 通过禁用 TLS 1.0 和 TLS 1.1 给出错误消息

c# - 在电子邮件正文中动态获取收件人的电子邮件地址