本文整理汇总了C#中System.Diagnostics.PerformanceCounterCategory类的典型用法代码示例。如果您正苦于以下问题:C# PerformanceCounterCategory类的具体用法?C# PerformanceCounterCategory怎么用?C# PerformanceCounterCategory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PerformanceCounterCategory类属于System.Diagnostics命名空间,在下文中一共展示了PerformanceCounterCategory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetOrCreateCounterCategory
/// <summary>
/// Gets the or create counter category.
/// </summary>
/// <param name="categoryInfo">The category information.</param>
/// <param name="counters">The counters.</param>
/// <returns>PerformanceCounterCategory.</returns>
private static PerformanceCounterCategory GetOrCreateCounterCategory(
PerformanceCounterCategoryInfo categoryInfo, CounterCreationData[] counters)
{
var creationPending = true;
var categoryExists = false;
var categoryName = categoryInfo.CategoryName;
var counterNames = new HashSet<string>(counters.Select(info => info.CounterName));
PerformanceCounterCategory category = null;
if (PerformanceCounterCategory.Exists(categoryName))
{
categoryExists = true;
category = new PerformanceCounterCategory(categoryName);
var counterList = category.GetCounters();
if (category.CategoryType == categoryInfo.CategoryType && counterList.Length == counterNames.Count)
{
creationPending = counterList.Any(x => !counterNames.Contains(x.CounterName));
}
}
if (!creationPending) return category;
if (categoryExists)
PerformanceCounterCategory.Delete(categoryName);
var counterCollection = new CounterCreationDataCollection(counters);
category = PerformanceCounterCategory.Create(
categoryInfo.CategoryName,
categoryInfo.CategoryHelp,
categoryInfo.CategoryType,
counterCollection);
return category;
}
示例2: 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);
}
}
示例3: GetInstances
public IEnumerable<string> GetInstances(string category)
{
var counterCategory = new PerformanceCounterCategory(category);
var instances = counterCategory.GetInstanceNames();
return instances;
}
示例4: Enabling_performance_counters_should_result_in_performance_counters_being_created
public void Enabling_performance_counters_should_result_in_performance_counters_being_created()
{
//This will delete an recreate the categories.
PerformanceCategoryCreator.CreateCategories();
var outboundIntances = new PerformanceCounterCategory(OutboundPerfomanceCounters.CATEGORY).GetInstanceNames();
var inboundIntances = new PerformanceCounterCategory(InboundPerfomanceCounters.CATEGORY).GetInstanceNames();
Assert.Empty(outboundIntances);
Assert.Empty(inboundIntances);
var hostConfiguration = new RhinoQueuesHostConfiguration()
.EnablePerformanceCounters()
.Bus("rhino.queues://localhost/test_queue2", "test");
container = new WindsorContainer();
new RhinoServiceBusConfiguration()
.UseConfiguration(hostConfiguration.ToBusConfiguration())
.UseCastleWindsor(container)
.Configure();
bus = container.Resolve<IStartableServiceBus>();
bus.Start();
using (var tx = new TransactionScope())
{
bus.Send(bus.Endpoint, "test message.");
tx.Complete();
}
outboundIntances = new PerformanceCounterCategory(OutboundPerfomanceCounters.CATEGORY).GetInstanceNames();
inboundIntances = new PerformanceCounterCategory(InboundPerfomanceCounters.CATEGORY).GetInstanceNames();
Assert.NotEmpty(outboundIntances.Where(name => name.Contains("test_queue2")));
Assert.NotEmpty(inboundIntances.Where(name => name.Contains("test_queue2")));
}
示例5: 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;
}
}
}
示例6: 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;
}
}
示例7: EnumerateCountersFor
private static void EnumerateCountersFor(string category)
{
var sb = new StringBuilder();
var counterCategory = new PerformanceCounterCategory(category);
if(counterCategory.CategoryType == PerformanceCounterCategoryType.SingleInstance)
{
foreach (var counter in counterCategory.GetCounters())
{
sb.AppendLine(string.Format("{0}:{1}", category, counter.CounterName));
}
}
else
{
foreach (var counterInstance in counterCategory.GetInstanceNames())
{
foreach (var counter in counterCategory.GetCounters(counterInstance))
{
sb.AppendLine(string.Format("{0}:{1}:{2}", counterInstance, category, counter.CounterName));
}
}
}
Console.WriteLine(sb.ToString());
}
示例8: OptionsFormVisibleChanged
private void OptionsFormVisibleChanged(object sender, EventArgs e)
{
if (Visible == false)
{
return;
}
textBoxPhoneIp.Text = _options.PhoneIp;
textBoxPhonePort.Text = _options.PhonePort.ToString();
textBoxMyipHost.Text = _options.MyipHost;
textBoxMyipPort.Text = _options.MyipPort.ToString();
textBoxMyipUrl.Text = _options.MyipUrl;
textBoxMyipInterval.Text = _options.MyipInterval.ToString();
textBoxNpFile.Text = _options.NpFile;
checkBoxSendNp.Checked = _options.SendNp;
// set listBoxNetworkInterface
listBoxNetworkInterface.Items.Clear();
var instanceNames = new PerformanceCounterCategory("Network Interface").GetInstanceNames();
listBoxNetworkInterface.Items.AddRange(instanceNames);
if (instanceNames.Length > 0)
{
listBoxNetworkInterface.SelectedIndex = 0;
}
if (instanceNames.Length > _options.NetIndex)
{
listBoxNetworkInterface.SelectedIndex = _options.NetIndex;
}
}
示例9: 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);
}
示例10: 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);
}
}
示例11: PerfCounters
static PerfCounters()
{
var p = Process.GetCurrentProcess();
ProcessName = p.ProcessName;
ProcessId = p.Id;
CategoryProcess = new PerformanceCounterCategory("Process");
ProcessorTime = new PerformanceCounter("Process", "% Processor Time", ProcessName);
UserTime = new PerformanceCounter("Process", "% User Time", ProcessName);
PrivateBytes = new PerformanceCounter("Process", "Private Bytes", ProcessName);
VirtualBytes = new PerformanceCounter("Process", "Virtual Bytes", ProcessName);
VirtualBytesPeak = new PerformanceCounter("Process", "Virtual Bytes Peak", ProcessName);
WorkingSet = new PerformanceCounter("Process", "Working Set", ProcessName);
WorkingSetPeak = new PerformanceCounter("Process", "Working Set Peak", ProcessName);
HandleCount = new PerformanceCounter("Process", "Handle Count", ProcessName);
CategoryNetClrMemory = new PerformanceCounterCategory(".NET CLR Memory");
ClrBytesInAllHeaps = new PerformanceCounter(".NET CLR Memory", "# Bytes in all Heaps", ProcessName);
ClrTimeInGC = new PerformanceCounter(".NET CLR Memory", "% Time in GC", ProcessName);
ClrGen0Collections = new PerformanceCounter(".NET CLR Memory", "# Gen 0 Collections", p.ProcessName, true);
ClrGen1Collections = new PerformanceCounter(".NET CLR Memory", "# Gen 1 Collections", p.ProcessName, true);
ClrGen2Collections = new PerformanceCounter(".NET CLR Memory", "# Gen 1 Collections", p.ProcessName, true);
}
示例12: 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;
}
示例13: Initialize
public static void Initialize()
{
if (Log.IsInfoEnabled)
{
Log.InfoFormat("Start to initialize {0} performance counters", CategoryName);
}
if (!IsUserAdmin())
{
Log.Info("User has no Admin rights. CustomAuthResultCounters initialization skipped");
return;
}
try
{
PerformanceCounterCategory = GetOrCreateCategory(CategoryName, GetCounterCreationData());
}
catch (Exception e)
{
Log.WarnFormat("Exception happened during counters initialization. Excpetion {0}", e);
return;
}
if (Log.IsInfoEnabled)
{
Log.InfoFormat("{0} performance counters successfully initialized", CategoryName);
}
}
示例14: PerfCounterMBean
public PerfCounterMBean(string perfObjectName, string perfInstanceName, IEnumerable<string> perfCounterNames, bool useProcessInstanceName, bool useAllCounters)
{
_perfObjectName = perfObjectName;
if (useProcessInstanceName)
{
Process p = Process.GetCurrentProcess();
_perfInstanceName = p.ProcessName;
}
else
{
_perfInstanceName = perfInstanceName ?? "";
}
_category = new PerformanceCounterCategory(_perfObjectName);
if (useAllCounters)
{
foreach (PerformanceCounter counter in _category.GetCounters(_perfInstanceName))
{
_counters[counter.CounterName] = counter;
}
}
else if (perfCounterNames != null)
{
foreach (string counterName in perfCounterNames)
{
PerformanceCounter counter;
counter = new PerformanceCounter(_perfObjectName, counterName, _perfInstanceName, true);
_counters.Add(counterName, counter);
}
}
}
示例15: 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
}
}
}