public void RecognizesInstanceAndIgnoresSpaces()
        {
            var pattern = @"\ Process ( Idle  ) \ % Processor Time ";
            var sut = new PerformanceCounterRegEx();

            var result = sut.split(pattern);

            result.Category.ShouldBe("Process");
            result.Counter.ShouldBe("% Processor Time");
            result.Instance.ShouldBe("Idle");
        }
        public void RecognizesInstance()
        {
            var pattern = @"\Processor(_Total)\% Processor Time";
            var sut = new PerformanceCounterRegEx();

            var result = sut.split(pattern);

            result.Category.ShouldBe("Processor");
            result.Counter.ShouldBe("% Processor Time");
            result.Instance.ShouldBe("_Total");
        }
        public void RecognizesEasyPatterns()
        {
            var pattern = @"\Memory\Available Bytes";
            var sut = new PerformanceCounterRegEx();

            var result = sut.split(pattern);

            result.Category.ShouldBe("Memory");
            result.Counter.ShouldBe("Available Bytes");
            result.Instance.ShouldBeNullOrEmpty();
        }
        public void RecognizesEasyPatternsAndIgnoreSpaces()
        {
            var pattern = @"  Objects  \  Semaphores  ";
            var sut = new PerformanceCounterRegEx();

            var result = sut.split(pattern);

            result.Category.ShouldBe("Objects");
            result.Counter.ShouldBe("Semaphores");
            result.Instance.ShouldBeNullOrEmpty();
        }
示例#5
0
        private List <PerformanceCounter> getCounterlist(string counterName)
        {
            if (!counters.ContainsKey(counterName))
            {
                List <System.Diagnostics.PerformanceCounter> counterlist = new List <PerformanceCounter>();
                var counterData = DefaultPerfCounterRegEx.split(counterName);
                try
                {
                    PerformanceCounterCategory mycat = new PerformanceCounterCategory(counterData.Category);
                    var foo     = PerformanceCounterCategory.GetCategories();
                    var foolist = new List <string>();
                    foreach (var f in foo)
                    {
                        foolist.Add(f.CategoryName);
                    }
                    foolist.Sort();
                    switch (mycat.CategoryType)
                    {
                    case PerformanceCounterCategoryType.SingleInstance:
                        foreach (var counter in mycat.GetCounters())
                        {
                            if (!counter.CounterName.Equals(counterData.Counter, StringComparison.InvariantCultureIgnoreCase))
                            {
                                continue;
                            }
                            counterlist.Add(counter);
                            counter.NextValue();     // Initialize performance counters in order to avoid them to return 0.
                        }
                        break;

                    case PerformanceCounterCategoryType.MultiInstance:
                        if (counterData.Instance == null || counterData.Instance.Equals("*"))
                        {
                            foreach (var instance in mycat.GetInstanceNames())
                            {
                                foreach (var counter in mycat.GetCounters(instance))
                                {
                                    if (!counter.CounterName.Equals(counterData.Counter, StringComparison.InvariantCultureIgnoreCase))
                                    {
                                        continue;
                                    }
                                    counterlist.Add(counter);
                                    counter.NextValue();     // Initialize performance counters in order to avoid them to return 0.
                                }
                            }
                        }
                        else
                        {
                            foreach (var counter in mycat.GetCounters(counterData.Instance))
                            {
                                if (!counter.CounterName.Equals(counterData.Counter, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    continue;
                                }
                                counterlist.Add(counter);
                                counter.NextValue();     // Initialize performance counters in order to avoid them to return 0.
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception e)
                {
                    Log.Error(String.Format("Counter {0} will be ignored due to errors: {1}", counterName, e));
                    Log.Error(e);
                }
                finally
                {
                    counters.Add(counterName, counterlist);
                }
            }
            return(counters[counterName]);
        }