创建适用于 windows 7 开发机器的新性能计数器后,原始值的设置在部署到 windows server 2012 R2 时将不起作用。
自定义类别中的现有计数器都仍然有效,但不会设置新的。
我已经尝试删除整个类别并重新创建所有计数器,但它仍然具有未设置新计数器的相同结果。
const string counterCategoryName = "Statistics";
var requiredCounters = GetAllCounters();
// Check counters exist
PerformanceCounterCategory category = null;
if (PerformanceCounterCategory.Exists(counterCategoryName) == false // Category exists
|| !requiredCounters.All(counter =>
PerformanceCounterCategory.GetCategories()
.Single(z => z.CategoryName == counterCategoryName)
.CounterExists(counter.Name))) // All counters exist
{
if (PerformanceCounterCategory.Exists(counterCategoryName))
{
// This occurs in case there is a counter missing, we should delete the existing category and rebuild it with all the new counters
PerformanceCounterCategory.Delete(counterCategoryName);
}
var counterDatas = new CounterCreationDataCollection();
foreach (var counter in requiredCounters.Select(counterDesc => new CounterCreationData
{
CounterName = counterDesc.Name,
CounterHelp = counterDesc.Description,
CounterType = PerformanceCounterType.NumberOfItems64 //counterDesc.Type
}))
{
counterDatas.Add(counter);
}
category = PerformanceCounterCategory.Create(counterCategoryName, "Statistics",PerformanceCounterCategoryType.SingleInstance, counterDatas);
}
else
{
category = PerformanceCounterCategory.GetCategories()
.Single(z => z.CategoryName == counterCategoryName);
}