Beispiel #1
0
        /// <summary>
        /// Adds the performance counter definition to the set.  The definitions
        /// will be indexed by counter name.
        /// </summary>
        /// <param name="counter">The counter definition.</param>
        /// <remarks>
        /// <note>
        /// It is OK to add a performance counter with the same name
        /// as an existing one.  In this case, the call will be ignored.
        /// </note>
        /// </remarks>
        public void Add(PerfCounter counter)
        {
            string      key = counter.Name;
            PerfCounter existing;

            if (counters.TryGetValue(key, out existing))
            {
                counter.Counter = existing.Counter;
                return;
            }

            counters.Add(key, counter);
        }
Beispiel #2
0
        /// <summary>
        /// Returns a named instance of a performance counter.  Note that the
        /// lookup is case insensitive and that the method will create the
        /// underlying Windows performance counter if it doesn't already exist.
        /// </summary>
        /// <param name="name">The performance counter name.</param>
        /// <param name="instance">The instance name (or <c>null</c>).</param>
        /// <remarks>
        /// <note>
        /// Passing <paramref name="instance" /> as <c>null</c> or the empty string is
        /// equivalent to passing no instance parameter.
        /// </note>
        /// </remarks>
        public PerfCounter this[string name, string instance]
        {
            get
            {
                if (!multiInstance)
                {
                    throw new InvalidOperationException("Counter set is configured as single-instance so an instance name cannot be specified.");
                }

                if (instance == null || instance == string.Empty)
                {
                    throw new ArgumentException("Invalid instance name.");
                }

                PerfCounter counter;
                PerfCounter template;
                string      key = string.Format("{0}[{1}]", name, instance);

                if (!counters.TryGetValue(name, out template))
                {
                    throw new ArgumentException("Counter does not exist.", "name");
                }

                if (counters.TryGetValue(key, out counter))
                {
                    return(counter);
                }

                try
                {
                    // Instantiate the performance counter and add it
                    // to the set.

                    counter = new PerfCounter(template.Name, template.Help, instance, template.Type);
                    counters.Add(key, counter);

                    if (!enabled)
                    {
                        return(counter);
                    }

                    counter.Counter = new PerformanceCounter(categoryName, template.Name, instance, false);

                    // Associate any related counters

                    if (template.RelatedCounters != null && counter.Related == null)
                    {
                        var           list = new List <PerfCounter>();
                        PerfCounter   related;
                        PerfCounter[] relatedList;

                        for (int i = 0; i < template.RelatedCounters.Length; i++)
                        {
                            related = this[template.RelatedCounters[i], instance];
                            if (related != null)
                            {
                                list.Add(related);
                            }
                        }

                        relatedList = new PerfCounter[list.Count];
                        for (int i = 0; i < list.Count; i++)
                        {
                            relatedList[i] = list[i];
                        }

                        counter.Related = relatedList;
                    }
                }
                catch (Exception e)
                {
                    // I'm going to fail gracefully if the performance counter could not
                    // be created.  The PerfCounter class handles null counters by implementing
                    // a process local simulation.

                    counter.Counter = null;
                    SysLog.LogException(e);
                }

                return(counter);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Returns a single instance performance counter with the
        /// name passed.  Note that the lookup is case insensitive and that
        /// the method will create the underlying Windows performance counter
        /// if it doesn't already exist.
        /// </summary>
        /// <param name="name">The performance counter name.</param>
        /// <remarks>
        /// <note>
        /// This method verifies that the performance counters
        /// are installed on the system and goes ahead and installs them
        /// if necessary.
        /// </note>
        /// </remarks>
        public PerfCounter this[string name]
        {
            get
            {
                PerfCounter counter;

                if (!installed)
                {
                    Install();
                }

                if (multiInstance)
                {
                    throw new InvalidOperationException("Counter set is configured as multi-instance so an instance name must be specified.");
                }

                if (!counters.TryGetValue(name, out counter))
                {
                    throw new ArgumentException("Counter does not exist.", "name");
                }

                if (!enabled)
                {
                    return(counter);
                }

                try
                {
                    // Instantiate the performance counter if necessary.

                    if (counter.Counter == null && !installFailed && !PerfCounter.ProcessLocalOnly)
                    {
                        counter.Counter = new PerformanceCounter(categoryName, counter.Name, false);
                    }

                    // Associate any related counters

                    if (counter.RelatedCounters != null && counter.Related == null)
                    {
                        var           list = new List <PerfCounter>();
                        PerfCounter   related;
                        PerfCounter[] relatedList;

                        for (int i = 0; i < counter.RelatedCounters.Length; i++)
                        {
                            related = this[counter.RelatedCounters[i]];

                            if (related != null)
                            {
                                list.Add(related);
                            }
                        }

                        relatedList = new PerfCounter[list.Count];
                        for (int i = 0; i < list.Count; i++)
                        {
                            relatedList[i] = list[i];
                        }

                        counter.Related = relatedList;
                    }
                }
                catch (Exception e)
                {
                    // I'm going to fail gracefully if the performance counter could not
                    // be created.  The PerfCounter class handles null counters by simply
                    // ignoring calls to the counter modification members.

                    counter.Counter = null;
                    SysLog.LogException(e);
                }

                return(counter);
            }
        }