本文整理汇总了C#中System.Diagnostics.PerformanceCounterCategory.CounterExists方法的典型用法代码示例。如果您正苦于以下问题:C# PerformanceCounterCategory.CounterExists方法的具体用法?C# PerformanceCounterCategory.CounterExists怎么用?C# PerformanceCounterCategory.CounterExists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Diagnostics.PerformanceCounterCategory
的用法示例。
在下文中一共展示了PerformanceCounterCategory.CounterExists方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCounter
PerformanceCounter GetCounter(string category, string counter,
PerformanceCounterType type = PerformanceCounterType.AverageCount64,
string machine = ".", string instance = "_Total")
{
if (!PerformanceCounterCategory.Exists(category))
{
// create category
var counterInfos = new CounterCreationDataCollection();
var counterInfo = new CounterCreationData()
{
CounterType = type,
CounterName = counter,
};
counterInfos.Add(counterInfo);
PerformanceCounterCategory
.Create(category, category, counterInfos);
// check creation
var counters
= new PerformanceCounterCategory(category, machine);
if (!counters.CounterExists(counter))
Debug.Fail("Counter was not created");
if (!counters.InstanceExists(instance))
Debug.Fail("Instance not found");
}
// get counter
var perfCounter = new PerformanceCounter
{
CategoryName = category,
CounterName = counter,
MachineName = machine,
ReadOnly = false,
};
perfCounter.IncrementBy(10);
return perfCounter;
}
示例2: SafelyGetPerformanceCounter
public static long? SafelyGetPerformanceCounter(string categoryName, string counterName, string processName)
{
try
{
if (PerformanceCounterCategory.Exists(categoryName) == false)
return null;
var category = new PerformanceCounterCategory(categoryName);
var instances = category.GetInstanceNames();
var ravenInstance = instances.FirstOrDefault(x => x == processName);
if (ravenInstance == null || !category.CounterExists(counterName))
{
return null;
}
using (var counter = new PerformanceCounter(categoryName, counterName, ravenInstance, readOnly: true))
{
return counter.NextSample().RawValue;
}
}
catch (Exception e)
{
//Don't log anything here, it's up to the calling code to decide what to do
return null;
}
}
示例3: GetDatabaseTransactionVersionSizeInBytes
public long GetDatabaseTransactionVersionSizeInBytes()
{
if (getDatabaseTransactionVersionSizeInBytesErrorValue != 0)
return getDatabaseTransactionVersionSizeInBytesErrorValue;
try
{
const string categoryName = "Database ==> Instances";
if (PerformanceCounterCategory.Exists(categoryName) == false)
return getDatabaseTransactionVersionSizeInBytesErrorValue = -1;
var category = new PerformanceCounterCategory(categoryName);
var instances = category.GetInstanceNames();
var ravenInstance = instances.FirstOrDefault(x => x.Contains(uniquePrefix));
const string counterName = "Version Buckets Allocated";
if (ravenInstance == null || !category.CounterExists(counterName))
{
return getDatabaseTransactionVersionSizeInBytesErrorValue = -2;
}
using (var counter = new PerformanceCounter(categoryName, counterName, ravenInstance, readOnly: true))
{
var value = counter.NextValue();
return (long)(value * StorageConfigurator.GetVersionPageSize());
}
}
catch (Exception e)
{
if (reportedGetDatabaseTransactionCacheSizeInBytesError == false)
{
reportedGetDatabaseTransactionCacheSizeInBytesError = true;
log.WarnException("Failed to get Version Buckets Allocated value, this error will only be reported once.", e);
}
return getDatabaseTransactionVersionSizeInBytesErrorValue = -3;
}
}
示例4: AssertCategoryIsCorrect
static void AssertCategoryIsCorrect(PerformanceCounterCategory category,
string categoryName)
{
Assert.IsNotNull(category);
Assert.AreEqual(categoryName, category.CategoryName);
Assert.AreEqual(PerformanceCounterCategoryType.MultiInstance, category.CategoryType);
Assert.IsTrue(
category.CounterExists(PerformanceCounterCallHandler.NumberOfCallsCounterName));
Assert.IsTrue(
category.CounterExists(PerformanceCounterCallHandler.CallsPerSecondCounterName));
Assert.IsTrue(
category.CounterExists(PerformanceCounterCallHandler.AverageCallDurationCounterName));
Assert.IsTrue(
category.CounterExists(
PerformanceCounterCallHandler.AverageCallDurationBaseCounterName));
Assert.IsTrue(
category.CounterExists(PerformanceCounterCallHandler.TotalExceptionsCounterName));
Assert.IsTrue(
category.CounterExists(PerformanceCounterCallHandler.ExceptionsPerSecondCounterName));
}
示例5: InitializeCounters
private void InitializeCounters()
{
switch (this.sharedDataRef.Status)
{
case PerformanceCounterCategoryStatus.Uninitialized:
try
{
foreach (KeyValuePair<string, PerformanceCounterCategoryData> pair in this.sharedDataRef.Categories)
{
if (!PerformanceCounterCategory.Exists(pair.Key))
{
Trace.TraceInformation("Creating performance counters for category '{0}'.", pair.Key);
PerformanceCounterCategory.Create(pair.Key, "", pair.Value.CategoryType, pair.Value.Counters);
}
else
{
PerformanceCounterCategory category = new PerformanceCounterCategory(pair.Key);
bool invalid = category.CategoryType != pair.Value.CategoryType;
foreach (CounterCreationData counterData in pair.Value.Counters)
{
if (!category.CounterExists(counterData.CounterName))
{
invalid = true;
break;
}
PerformanceCounter counter = CreatePerformanceCounter(pair.Key, counterData.CounterName, pair.Value.CategoryType, true);
if (counter.CounterType != counterData.CounterType)
{
invalid = true;
break;
}
}
if (invalid)
{
Trace.TraceInformation("Recreating performance counters for category '{0}'.", pair.Key);
PerformanceCounterCategory.Delete(pair.Key);
PerformanceCounterCategory.Create(pair.Key, "", pair.Value.CategoryType, pair.Value.Counters);
}
}
}
}
catch (Exception e)
{
Trace.TraceError("Cannot initialize performance counter {0}.{1}: {2}",
this.CategoryName, this.CounterName, e.Message);
this.sharedDataRef.Status = PerformanceCounterCategoryStatus.Failed;
return;
}
this.sharedDataRef.Status = PerformanceCounterCategoryStatus.Initialized;
break;
case PerformanceCounterCategoryStatus.Failed:
return;
}
}
示例6: CopyFromComponent
public override void CopyFromComponent(IComponent component)
{
if (!(component is PerformanceCounter))
{
throw new ArgumentException(Res.GetString("NotAPerformanceCounter"));
}
PerformanceCounter counter = (PerformanceCounter) component;
if ((counter.CategoryName == null) || (counter.CategoryName.Length == 0))
{
throw new ArgumentException(Res.GetString("IncompletePerformanceCounter"));
}
if (!this.CategoryName.Equals(counter.CategoryName) && !string.IsNullOrEmpty(this.CategoryName))
{
throw new ArgumentException(Res.GetString("NewCategory"));
}
PerformanceCounterType counterType = PerformanceCounterType.NumberOfItems32;
string counterHelp = string.Empty;
if (string.IsNullOrEmpty(this.CategoryName))
{
this.CategoryName = counter.CategoryName;
}
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
string machineName = counter.MachineName;
if (PerformanceCounterCategory.Exists(this.CategoryName, machineName))
{
string name = @"SYSTEM\CurrentControlSet\Services\" + this.CategoryName + @"\Performance";
RegistryKey key = null;
try
{
if ((machineName == ".") || (string.Compare(machineName, SystemInformation.ComputerName, StringComparison.OrdinalIgnoreCase) == 0))
{
key = Registry.LocalMachine.OpenSubKey(name);
}
else
{
key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, @"\\" + machineName).OpenSubKey(name);
}
if (key == null)
{
throw new ArgumentException(Res.GetString("NotCustomPerformanceCategory"));
}
object obj2 = key.GetValue("Library");
if (((obj2 == null) || !(obj2 is string)) || (string.Compare((string) obj2, "netfxperf.dll", StringComparison.OrdinalIgnoreCase) != 0))
{
throw new ArgumentException(Res.GetString("NotCustomPerformanceCategory"));
}
PerformanceCounterCategory category = new PerformanceCounterCategory(this.CategoryName, machineName);
this.CategoryHelp = category.CategoryHelp;
if (category.CounterExists(counter.CounterName))
{
counterType = counter.CounterType;
counterHelp = counter.CounterHelp;
}
this.CategoryType = category.CategoryType;
}
finally
{
if (key != null)
{
key.Close();
}
}
}
}
CounterCreationData data = new CounterCreationData(counter.CounterName, counterHelp, counterType);
this.Counters.Add(data);
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:68,代码来源:PerformanceCounterInstaller.cs