2
我在Windows 7上增加了自定義性能計數器時遇到了一些麻煩。創建了類別,創建了計數器,但調用了「Increment()」或設置了「RawValue」沒有看似的效果。例如,Increment()的返回值仍然爲0L。遞增自定義性能計數器.net 4.0 Windows 7
以下代碼是控制檯應用程序,需要以管理員身份運行以創建類別。調試通過顯示以上症狀。
任何幫助,非常感謝。
class Program
{
private static string _category = "MyCustomCounters";
static void Main(string[] args)
{
var deleteIfExists = false;
if (args.Any())
{
if (args.Count() > 0)
{
_category = args[0];
}
if (args.Count() > 1)
{
deleteIfExists = args[1].ToUpper() == bool.TrueString.ToUpper();
}
}
CreateCustomCounters(_category, deleteIfExists);
}
private static void CreateCustomCounters(string category, bool deleteIfExists)
{
if (deleteIfExists && PerformanceCounterCategory.Exists(category))
{
PerformanceCounterCategory.Delete(category);
}
if (!PerformanceCounterCategory.Exists(category))
{
CounterCreationDataCollection counterCollection = new CounterCreationDataCollection();
// add a counter tracking operations per second
CounterCreationData opsPerSec = new CounterCreationData();
opsPerSec.CounterName = "# requests /sec";
opsPerSec.CounterHelp = "Number of requests executed per second";
opsPerSec.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
counterCollection.Add(opsPerSec);
// add a counter tracking operations per second
CounterCreationData operationTotal = new CounterCreationData();
operationTotal.CounterName = "Total # requests";
operationTotal.CounterHelp = "Total number of requests executed";
operationTotal.CounterType = PerformanceCounterType.NumberOfItems32;
counterCollection.Add(operationTotal);
PerformanceCounterCategory.Create(category,
"A custom counter category that tracks execution", PerformanceCounterCategoryType.SingleInstance,
counterCollection);
}
else
{
Console.Error.WriteLine("Counter already exists, try specifying parameters for categoryname and or insisting on deleting");
}
using (var _totalOperationsCounter = new PerformanceCounter(_category, "Total # requests", false))
{
_totalOperationsCounter.ReadOnly = false;
_totalOperationsCounter.RawValue = 10;
var count = _totalOperationsCounter.Increment();
}
}
}