本文整理汇总了C#中System.Diagnostics.PerformanceCounterCategory.GetInstanceNames方法的典型用法代码示例。如果您正苦于以下问题:C# PerformanceCounterCategory.GetInstanceNames方法的具体用法?C# PerformanceCounterCategory.GetInstanceNames怎么用?C# PerformanceCounterCategory.GetInstanceNames使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Diagnostics.PerformanceCounterCategory
的用法示例。
在下文中一共展示了PerformanceCounterCategory.GetInstanceNames方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RefreshCounters
/// <summary>
/// Re-creates the _counters list if the number
/// of valid PerformanceCounters has changed.
/// </summary>
private void RefreshCounters()
{
PerformanceCounterCategory cat = new PerformanceCounterCategory("Network Interface");
if (cat.GetInstanceNames().Length != _counters.Count)
{
_counters.Clear();
foreach (string instance in cat.GetInstanceNames())
{
_counters.Add(new PerformanceCounter("Network Interface", _counterName, instance));
}
}
}
示例2: GetInstances
public IEnumerable<string> GetInstances(string category)
{
var counterCategory = new PerformanceCounterCategory(category);
var instances = counterCategory.GetInstanceNames();
return instances;
}
示例3: LogicalDisk
// Try to discover GUID from buggy Performance Monitor instance names.
// Note: Discover drive GUID comparing free space is uggly, but MS gave me no choice.
static LogicalDisk()
{
// ===== WMI =====
Win32_Volume[] vols = Win32_Volume.GetAllVolumes();
// Free megabytes and volume GUID relation
Dictionary<ulong, Guid> wmiFree = new Dictionary<ulong, Guid>(vols.Length);
// Volume name and volume GUID relation
Dictionary<string, Guid> wmiName = new Dictionary<string, Guid>(vols.Length);
foreach (Win32_Volume v in vols) {
if (v.Automount &&
v.DriveType == System.IO.DriveType.Fixed) {
if (v.IsMounted) {
wmiName.Add(v.Name.TrimEnd('\\'), v.DeviceGuid);
} else {
wmiFree.Add(v.FreeSpace / MB_MULT, v.DeviceGuid);
}
}
}
perfMonGuid = new Dictionary<Guid, string>(wmiFree.Count + wmiName.Count);
// ===== PERFORMANCE MONITOR ======
PerformanceCounterCategory perfCat = new PerformanceCounterCategory(
Localization.GetName(COUNTER_LOGICAL_DISK));
// TODO: Find a faster way to get instance names.
string[] instances = perfCat.GetInstanceNames();
// Free megabytes and Performance Monitor instance name
Dictionary<ulong, string> perfFree = new Dictionary<ulong, string>(instances.Length);
foreach (string item in instances) {
if (item == "_Total")
continue;
Guid volId = Guid.Empty;
if (wmiName.TryGetValue(item, out volId)) {
perfMonGuid.Add(volId, item);
} else {
PerformanceCounter p = new PerformanceCounter(
Localization.GetName(COUNTER_LOGICAL_DISK),
Localization.GetName(COUNTER_FREE_MB),
item);
perfFree.Add((ulong)p.RawValue, item);
p.Close();
p.Dispose();
}
}
ulong[] warray = new ulong[wmiFree.Count];
ulong[] pmarray = new ulong[perfFree.Count];
if (warray.Length != pmarray.Length)
throw new NotSupportedException(MSG_EXCEPTION);
wmiFree.Keys.CopyTo(warray, 0);
perfFree.Keys.CopyTo(pmarray, 0);
Array.Sort<ulong>(warray);
Array.Sort<ulong>(pmarray);
for (int i = 0; i < warray.Length; i++) {
perfMonGuid.Add(wmiFree[warray[i]], perfFree[pmarray[i]]);
}
}
示例4: NetworkIO
public NetworkIO()
{
_dict = new Dictionary<string, double>();
PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");
var interfaces = GetNetworkInterfaces();
var categoryList = category.GetInstanceNames();
foreach (string name in categoryList)
{
var nicName = name.Replace('[', '(').Replace(']', ')');
if (!interfaces.Select(t => t.Description).Contains(nicName) || nicName.ToLower().Contains("loopback"))
continue;
try
{
NetworkAdapter adapter = new NetworkAdapter(interfaces.First(t => t.Description.Contains(nicName)).Name);
adapter.NetworkBytesReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", name);
adapter.NetworkBytesSend = new PerformanceCounter("Network Interface", "Bytes Sent/sec", name);
_adappterList.Add(adapter); // Add it to ArrayList adapter
}
catch
{
//pass
}
}
}
示例5: GetPerformanceCounterCategories
public static List<PerformanceCounterCategoryModel> GetPerformanceCounterCategories()
{
var counterCategories = new List<PerformanceCounterCategoryModel>();
PerformanceCounterCategory.GetCategories().ToList().ForEach((i) =>
counterCategories.Add(new PerformanceCounterCategoryModel { Name = i.CategoryName })
);
counterCategories = counterCategories.OrderBy(x => x.Name).ToList();
counterCategories.ForEach((i) => {
try {
var cat = new PerformanceCounterCategory(i.Name);
var instances = cat.GetInstanceNames();
if(instances.Length > 0) {
foreach(var instance in instances) {
i.Instances.Add(instance);
}
}
}
catch {
// sometimes this freaks out when an instance can't be examined
}
});
return counterCategories;
}
示例6: WatchCpuAndMemory
public void WatchCpuAndMemory()
{
var pc = new PerformanceCounter("Processor Information", "% Processor Time");
var cat = new PerformanceCounterCategory("Processor Information");
var cpuInstances = cat.GetInstanceNames();
var cpus = new Dictionary<string, CounterSample>();
var memoryCounter = new PerformanceCounter("Memory", "Available MBytes");
foreach (var s in cpuInstances)
{
pc.InstanceName = s;
cpus.Add(s, pc.NextSample());
}
var t = DateTime.Now;
while (t.AddMinutes(1) > DateTime.Now)
{
Trace.WriteLine(string.Format("Memory:{0}MB", memoryCounter.NextValue()));
foreach (var s in cpuInstances)
{
pc.InstanceName = s;
Trace.WriteLine(string.Format("CPU:{0} - {1:f}", s, Calculate(cpus[s], pc.NextSample())));
cpus[s] = pc.NextSample();
}
//Trace.Flush();
System.Threading.Thread.Sleep(1000);
}
}
示例7: TryGetInstanceName
public static bool TryGetInstanceName(Process process, out string instanceName)
{
try
{
PerformanceCounterCategory processCategory = new PerformanceCounterCategory(CategoryName);
string[] instanceNames = processCategory.GetInstanceNames();
foreach (string name in instanceNames)
{
if (name.StartsWith(process.ProcessName))
{
using (
PerformanceCounter processIdCounter = new PerformanceCounter(CategoryName, ProcessIdCounter,
name, true))
{
if (process.Id == (int) processIdCounter.RawValue)
{
instanceName = name;
return true;
}
}
}
}
instanceName = null;
return false;
}
catch
{
instanceName = null;
return false;
}
}
示例8: Configure
public void Configure()
{
var counters = CounterList;
counters.Add("Processor Load", "Processor", "% Processor Time", "_Total");
counters.Add("Memory Usage", "Memory", "% Committed Bytes In Use");
counters.Add("IIS Requests/sec", "Web Service", "Total Method Requests/sec", "_Total");
//this one is very unreliable
counters.Add("ASP.NET Request/Sec", "ASP.NET Applications", "Requests/Sec", "__Total__");
counters.Add("ASP.NET Current Requests", "ASP.NET", "Requests Current");
counters.Add("ASP.NET Queued Requests", "ASP.NET", "Requests Queued");
counters.Add("ASP.NET Requests Wait Time", "ASP.NET", "Request Wait Time");
PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");
String[] instanceNames = category.GetInstanceNames();
foreach (string name in instanceNames)
{
counters.Add("Net IO Total: " + name, "Network Interface", "Bytes Total/sec", name);
counters.Add("Net IO Received: " + name, "Network Interface", "Bytes Received/sec", name);
counters.Add("Net IO Sent: " + name, "Network Interface", "Bytes Sent/sec", name);
}
}
示例9: TryToInitializeCounters
private void TryToInitializeCounters()
{
if (!countersInitialized)
{
PerformanceCounterCategory category = new PerformanceCounterCategory(".NET CLR Networking 4.0.0.0");
var instanceNames = category.GetInstanceNames().Where(i => i.Contains(string.Format("p{0}", pid)));
if (instanceNames.Any())
{
bytesSentPerformanceCounter = new PerformanceCounter();
bytesSentPerformanceCounter.CategoryName = ".NET CLR Networking 4.0.0.0";
bytesSentPerformanceCounter.CounterName = "Bytes Sent";
bytesSentPerformanceCounter.InstanceName = instanceNames.First();
bytesSentPerformanceCounter.ReadOnly = true;
bytesReceivedPerformanceCounter = new PerformanceCounter();
bytesReceivedPerformanceCounter.CategoryName = ".NET CLR Networking 4.0.0.0";
bytesReceivedPerformanceCounter.CounterName = "Bytes Received";
bytesReceivedPerformanceCounter.InstanceName = instanceNames.First();
bytesReceivedPerformanceCounter.ReadOnly = true;
countersInitialized = true;
}
}
}
示例10: GetStandardValues
/// <summary>
/// Returns a collection of standard values for the data type this type converter is designed for when provided with a format context.
/// </summary>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context that can be used to extract additional information about the environment from which this converter is invoked. This parameter or properties of this parameter can be null.</param>
/// <returns>
/// A <see cref="T:System.ComponentModel.TypeConverter.StandardValuesCollection"/> that holds a standard set of valid values, or null if the data type does not support a standard set of values.
/// </returns>
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
INuGenCounter counter = (context == null) ? null : (context.Instance as INuGenCounter);
string machineName = ".";
string categoryName = string.Empty;
if (counter != null)
{
machineName = counter.MachineName;
categoryName = counter.CategoryName;
}
try
{
PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName, machineName);
string[] instances = category.GetInstanceNames();
Array.Sort(instances, Comparer.Default);
return new TypeConverter.StandardValuesCollection(instances);
}
catch
{
}
return null;
}
示例11: SetVariables
private static void SetVariables(int eth = 0)
{
performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
instance = performanceCounterCategory.GetInstanceNames()[eth]; // 1st NIC !
performanceCounterSent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);
performanceCounterReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance);
}
示例12: Initialize
private void Initialize()
{
var category = new PerformanceCounterCategory(Category);
var instances = category.GetInstanceNames();
foreach (var instanceName in instances)
{
_counters.Add(new PerformanceCounter(Category, CounterName, instanceName));
}
PerformanceCounterHelper.InitializeCounters(_counters.ToArray());
}
示例13: GetCounterNames
public IEnumerable<string> GetCounterNames(string category)
{
var cat = new PerformanceCounterCategory(category);
var instances = cat.CategoryType == PerformanceCounterCategoryType.SingleInstance ? new string[] { null } : cat.GetInstanceNames();
foreach (var instance in instances)
{
var coutners = string.IsNullOrEmpty(instance) ? cat.GetCounters().Select(x => x.CounterName) : cat.GetCounters(instance).Select(x => x.CounterName);
foreach (var counter in coutners)
{
yield return counter;
}
}
}
示例14: CpuPlugin
public CpuPlugin(ApplicationConfiguration config)
{
using (Profiler.Step("CPU Init"))
{
_config = config;
var category = new PerformanceCounterCategory("Processor");
_counters = (from name in category.GetInstanceNames()
select category.GetCounters(name))
.SelectMany(x => x)
.ToArray();
}
}
示例15: SetupCounters
public void SetupCounters()
{
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
string instance = performanceCounterCategory.GetInstanceNames()[0]; // 1st NIC !
performanceCounterSent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);
performanceCounterReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance);
}