コード例 #1
0
        /// <summary>
        /// Get a sample of the counter
        /// </summary>
        /// <param name="counterName">name of the counter</param>
        /// <returns>returns CounterSample.Empty in case there was an error, otherwise it returns the not calculated sample</returns>
        /// <exception cref="System.ObjectDisposedException" />
        public CounterSample NextSample(T counterName)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            PerformanceCounterContainer counter = null;

            if ((counter = this.GetContainer(counterName)) == null)
            {
                return(CounterSample.Empty);
            }
            try
            {
                return(counter.PerformanceCounterInstance.NextSample());
            }
            catch (InvalidOperationException ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            catch (System.PlatformNotSupportedException ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            catch (UnauthorizedAccessException ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            return(CounterSample.Empty);
        }
コード例 #2
0
        /// <summary>
        /// get the PerformanceCounterContainer associated with the given counterName.
        /// </summary>
        /// <param name="counterName">name of the counter</param>
        /// <returns>PerformanceCounterContainer instance in case there is such. Otherwise null.</returns>
        private PerformanceCounterContainer GetContainer(T counterName)
        {
            PerformanceCounterContainer counter = null;

            if (!this._counters.TryGetValue(counterName, out counter))
            {
                return(null);
            }
            return(counter);
        }
コード例 #3
0
        /// <summary>
        /// Internal Constructor for named instances (multi-instance counters)
        /// </summary>
        /// <param name="instanceName">name for this instance</param>
        /// <param name="categoryInfo">information about this category</param>
        /// <param name="enumCounterAttributes">enumerator attributes</param>
        /// <exception cref="System.NotSupportedException" />
        internal CounterHelperImpl(string instanceName, PerformanceCounterCategoryAttribute categoryInfo, Dictionary <T, PerformanceCounterAttribute> enumCounterAttributes)
            : this(enumCounterAttributes.Count, instanceName)
        {
            if ((categoryInfo.InstanceType == PerformanceCounterCategoryType.MultiInstance) &&
                (string.IsNullOrEmpty(instanceName)))
            {
                throw new NotSupportedException(Properties.Resources.CounterHelper_MultiInstanceNoInstanceNameErrorMessage);
            }

            PerformanceCounter performanceCounter, performanceCounterBase = null;

            Dictionary <T, PerformanceCounterAttribute> .Enumerator enumerator = enumCounterAttributes.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (categoryInfo.InstanceType == PerformanceCounterCategoryType.MultiInstance)
                {
                    performanceCounter = new PerformanceCounter(categoryInfo.Name,
                                                                enumerator.Current.Value.Name,
                                                                this._instanceName,
                                                                false);
                }
                else
                {
                    performanceCounter = new PerformanceCounter(categoryInfo.Name,
                                                                enumerator.Current.Value.Name,
                                                                false);
                }
                PerformanceCounterType?baseType = PerformanceHelper.GetBaseType(performanceCounter.CounterType);

                if (baseType != null)
                {
                    if (categoryInfo.InstanceType == PerformanceCounterCategoryType.MultiInstance)
                    {
                        performanceCounterBase = new PerformanceCounter(categoryInfo.Name,
                                                                        PerformanceHelper.GetCounterNameForBaseType(enumerator.Current.Value.Name),
                                                                        instanceName,
                                                                        false);
                    }
                    else
                    {
                        performanceCounterBase = new PerformanceCounter(categoryInfo.Name,
                                                                        PerformanceHelper.GetCounterNameForBaseType(enumerator.Current.Value.Name),
                                                                        false);
                    }
                    performanceCounterBase.RawValue = PerformanceHelper.getInitialValue(performanceCounter.CounterType);
                }
                else
                {
                    performanceCounterBase = null;
                }

                PerformanceCounterContainer performanceCounterContainer = new PerformanceCounterContainer(performanceCounter, performanceCounterBase, enumerator.Current.Value.IsBaseAutoIncreased);
                this._counters.Add(enumerator.Current.Key, performanceCounterContainer);
            }
        }
コード例 #4
0
        /// <summary>
        /// Get the PerformanceCounter Base Instance associated with the countername.
        ///
        /// </summary>
        /// <param name="counterName">name of the counter</param>
        /// <returns>returns an instance of PerformanceCounter in case there is such object associated with the countername given as a base counter. Otherwise, null</returns>
        public PerformanceCounter GetBaseInstance(T counterName)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            PerformanceCounterContainer counter = null;

            if ((counter = this.GetContainer(counterName)) == null)
            {
                return(null);
            }
            return(counter.PerformanceCounterBaseInstance);
        }
コード例 #5
0
        /// <summary>
        /// Reset to default value the instance counter
        /// </summary>
        /// <param name="counterName">the counter name</param>
        public void Reset(T counterName)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            PerformanceCounterContainer counter = null;

            if ((counter = this.GetContainer(counterName)) != null)
            {
                counter.PerformanceCounterInstance.RawValue = PerformanceHelper.getInitialValue(counter.PerformanceCounterInstance.CounterType);
                if (counter.IsBaseAutoIncreased && counter.PerformanceCounterBaseInstance != null)
                {
                    counter.PerformanceCounterBaseInstance.RawValue = PerformanceHelper.getInitialValue(counter.PerformanceCounterBaseInstance.CounterType);
                }
            }
        }
コード例 #6
0
        private bool _disposed; //false
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                this._disposed = true;
                if (this._counters != null && this._counters.Count > 0)
                {
                    PerformanceCounterContainer[] counters = new PerformanceCounterContainer[this._counters.Values.Count];
                    this._counters.Values.CopyTo(counters, 0);
                    this._counters.Clear();

                    foreach (PerformanceCounterContainer performanceCounterContainer in counters)
                    {
                        performanceCounterContainer.Dispose();
                    }
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// Decrement value of the counter by "value"
        /// </summary>
        /// <param name="value">value to decrement</param>
        /// <param name="counterName">name of the counter to be decremented</param>
        /// <returns>returns FAILURE  in case there was an error otherwise the final value of the counter</returns>
        /// <exception cref="System.ObjectDisposedException" />
        public long DecrementBy(T counterName, long value)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (value > 0)
            {
                value *= -1;
            }

            PerformanceCounterContainer counter = null;

            if ((counter = this.GetContainer(counterName)) == null)
            {
                return(FAILURE);
            }
            try
            {
                long rtnValue = counter.PerformanceCounterInstance.IncrementBy(value);
                if ((counter.PerformanceCounterBaseInstance != null) && (counter.IsBaseAutoIncreased))
                {
                    counter.PerformanceCounterBaseInstance.Decrement();
                }

                return(rtnValue);
            }
            catch (InvalidOperationException ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            catch (System.PlatformNotSupportedException ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            return(FAILURE);
        }
コード例 #8
0
        /// <summary>
        /// Get the value of a base counter
        /// </summary>
        /// <param name="counterName">name of the counter</param>
        /// <param name="value">value to be put on performance counter</param>
        /// <returns>returns FAILURE si hubo un error,in case there was an error, otherwise it returns the not calculated value</returns>
        /// <exception cref="System.ObjectDisposedException" />
        public long BaseRawValue(T counterName, long value)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            PerformanceCounterContainer counter = null;

            if ((counter = this.GetContainer(counterName)) == null)
            {
                return(FAILURE);
            }

            if (counter.PerformanceCounterBaseInstance == null)
            {
                return(FAILURE);
            }
            try
            {
                long rtnValue = counter.PerformanceCounterBaseInstance.RawValue = value;
                return(rtnValue);
            }
            catch (InvalidOperationException ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            catch (System.PlatformNotSupportedException ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            catch (UnauthorizedAccessException ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            return(FAILURE);
        }