是否可以在性能计数器的两个角色实例之间共享RawValue?

时间:2015-04-08 08:08:00

标签: c# azure performancecounter azure-diagnostics

使用Azure,我们有一个辅助角色,它分配了两个实例。在它们内部,我们有一个性能计数器来记录某些操作的计数:

static Service()
{
    Counter = new PerformanceCounter(CustomCounterCategory, CustomCounterName, "instance", false);
}

public static void DoSomething()
{
    while (true)
    {
        Trace.TraceInformation("[{0}]Raw value is {1}", RoleEnvironment.CurrentRoleInstance.Id, Counter.RawValue);
        Counter.Increment();
        Thread.Sleep(TimeSpan.FromSeconds(5));
    }
}

上面的演示代码只是读取性能计数器的当前原始数据并记录它,然后递增1。

从日志记录中,我发现不同实例的原始数据完全相同:0, 1, 2, 3 ...。那么以何种方式共享两个实例的原始数据,使得性能计数器存在于不同的角色实例中?

更新

以下是我创建性能计数器类别的方法:

if (!PerformanceCounterCategory.Exists(Service.CustomCounterCategory))
{
    var counterCollection = new CounterCreationDataCollection();

    var operationTotal1 = new CounterCreationData
    {
        CounterName = Service.CustomCounterName,
        CounterHelp = "help",
        CounterType = PerformanceCounterType.NumberOfItems32
    };

    counterCollection.Add(operationTotal1);
    PerformanceCounterCategory.Create(
        Service.CustomCounterCategory, 
        "CategoryDescription",
        PerformanceCounterCategoryType.MultiInstance, 
        counterCollection);
}

2 个答案:

答案 0 :(得分:0)

请参阅此链接https://msdn.microsoft.com/en-us/library/azure/dn535595.aspx

PerformanceCounterCategory.Create(
     "MyCustomCounterCategory",
     "My Custom Counter Category",
     PerformanceCounterCategoryType.SingleInstance, counterCollection);

   Trace.WriteLine("Custom counter category created.");

尝试将singleInstance更改为其他选项。当然还有多实例选项

答案 1 :(得分:0)

简短回答:

以防您正在寻找同一问题的答案。