创建适用于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);
}