Ejemplo n.º 1
0
        public float NextValue()
        {
            try
            {
                return(_innerCounter.NextValue());
            }
            catch
            {
                ChangeCounter(_nullPerformanceCounter);

                return(_innerCounter.NextValue());
            }
        }
Ejemplo n.º 2
0
        private void SetCounterProperties(string instanceName)
        {
            var canLoadCounters = true;

            foreach (var property in CounterProperties)
            {
                PerformanceCounterAttribute attribute = GetPerformanceCounterAttribute(property);
                if (attribute == null)
                {
                    continue;
                }

                IPerformanceCounter counter = null;
                if (canLoadCounters)
                {
                    counter = LoadCounter(CategoryName, attribute.Name, instanceName, isReadOnly: false);

                    if (counter == null)
                    {
                        // We failed to load the counter so skip the rest
                        canLoadCounters = false;
                    }
                }

                counter = counter ?? NoOpCounter;

                // Initialize the counter sample
                counter.NextValue();

                property.SetValue(this, counter, null);
            }
        }
Ejemplo n.º 3
0
        private HardwareInformation GetGlobalCpuUsageWithPerfCounter()
        {
            var cpuIdle = all_Cpu_Idle.NextValue();

            return(new HardwareInformation()
            {
                MainValue = Math.Round(100.0 - cpuIdle, 2),
                ShortName = "CPU",
                UnitSymbol = "%"
            });
        }
Ejemplo n.º 4
0
        private IPerformanceCounter CreateCounter(string category, string instance, string counterName)
        {
            IPerformanceCounter counter = null;

            try
            {
                counter = _counterFactory.Create(category, counterName, instance);

                // First call to NextValue returns always 0 -> perforn it without taking value.
                counter.NextValue();
            }
            catch (InvalidOperationException ex)
            {
                string msg = string.Format("Failed to create counter: {0}, {1}, {2}", counter?.CategoryName,
                                           counter?.CounterName,
                                           counter?.InstanceName);

                Logger.Error(ex, msg);
                throw new InvalidOperationException(msg, ex);
            }

            return(counter);
        }