예제 #1
0
        /// <summary>
        /// adds a new performance counter instance
        /// </summary>
        /// <param name="key">Id to identify this entry by</param>
        /// <param name="category"></param>
        /// <param name="counterName"></param>
        /// <param name="instanceName"></param>
        /// <param name="readOnly"></param>
        /// <param name="machineName"></param>
        public PerformanceCounterItem Add(string key, string category, string counterName,
                                          string instanceName = null, bool readOnly = true,
                                          string machineName  = null)
        {
            PerformanceCounter perfCounter;

            if (instanceName != null)
            {
                perfCounter = new PerformanceCounter(category, counterName, instanceName, readOnly);
            }
            else
            {
                perfCounter = new PerformanceCounter(category, counterName, readOnly);
            }

            if (machineName != null)
            {
                perfCounter.MachineName = machineName;
            }
            if (string.IsNullOrEmpty(machineName) && !string.IsNullOrEmpty(MachineName))
            {
                perfCounter.MachineName = MachineName;
            }

            var item = new PerformanceCounterItem(key, perfCounter)
            {
                Description = perfCounter.CounterHelp
            };

            PerfCounterItems.Add(key, item);

            return(item);
        }
예제 #2
0
        /// <summary>
        /// Summarizes a number of entries that start with a given string
        /// and adds a new item with the summarized value. The detail
        /// values are removed from the collection optionally
        ///
        /// Call this method after a perf run has completed
        /// </summary>
        public PerformanceCounterItem SummarizeCounters(string startsWith, string newId, bool removeItems = false)
        {
            decimal summaryValue = PerfCounterItems.Where(item => item.Value.Id.StartsWith(startsWith)).Sum(itm => itm.Value.LastValue);

            var summarizedItem = new PerformanceCounterItem(newId, null)
            {
                LastValue = summaryValue
            };

            PerfCounterItems.Add(newId, summarizedItem);

            if (removeItems)
            {
                var netIds = PerfCounterItems.Where(item => item.Value.Id.StartsWith(startsWith)).ToList();
                for (int i = 0; i < netIds.Count; i++)
                {
                    var id = netIds[i].Key;
                    PerfCounterItems.Remove(id);
                }
            }

            return(summarizedItem);
        }
예제 #3
0
 public PerformanceCounterItem Add(string key, PerformanceCounterItem item)
 {
     item.Id = key;
     PerfCounterItems.Add(key, item);
     return(item);
 }