使用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));
}
}
上面的演示代码只需读取性能计数器的当前原始数据并将其记录下来,然后递增一。
从日志中,我发现不同实例的原始数据完全相同: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);
}
查看此链接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更改为其他选项。当然也会有多实例的选项
简短回答:否。
以防你们正在寻找同一个问题的答案。