/// <summary>
        /// Add a performance counter object to our collection. If it doesn't exist, it will be created.
        /// </summary>
        /// <param name="categoryName">The name of the performance counter category (performance object) with which this performance counter is associated.</param>
        /// <param name="counterName">The name of the performance counter.</param>
        /// <param name="alias">An alias to use to determine the instance name instead of the instance of the supplied counter.</param>
        public void Add(string categoryName, string counterName, PerfCounterInstanceAlias alias)
        {
            //we can't have a null category our counter.
            if (string.IsNullOrEmpty(categoryName))
            {
                throw new ArgumentNullException(nameof(categoryName));
            }

            if (string.IsNullOrEmpty(counterName))
            {
                throw new ArgumentNullException(nameof(counterName));
            }

            //see if the performance counter metric already exists.  It quite probably does.
            PerfCounterMetric newMetric = PerfCounterMetric.AddOrGet(categoryName, counterName, alias);

            //now we can use our normal routine.
            Add(newMetric);
        }
Exemple #2
0
        /// <summary>Creates a new performance counter metric instance or returns an existing one from the provided definition information, or returns any existing instance if found.</summary>
        /// <remarks>If the metric definition doesn't exist, it will be created.  If the metric doesn't exist, it will be created.
        /// If the metric definition does exist, but is not a Performance Counter Metric (or a derived class) an exception will be thrown.</remarks>
        /// <param name="categoryName">The name of the performance counter category (performance object) with which this performance counter is associated.</param>
        /// <param name="counterName">The name of the performance counter.</param>
        /// <param name="alias">An alias to use to determine the instance name instead of the instance of the supplied counter.</param>
        /// <returns>The Performance Counter Metric object for the specified instance.</returns>
        public static PerfCounterMetric AddOrGet(string categoryName, string counterName, PerfCounterInstanceAlias alias)
        {
            //make sure we have all of the variables we require
            if (string.IsNullOrEmpty(categoryName))
            {
                throw new ArgumentNullException(nameof(categoryName), "No category name was provided and one is required.");
            }

            if (string.IsNullOrEmpty(counterName))
            {
                throw new ArgumentNullException(nameof(counterName), "No counter name was provided and one is required.");
            }

            //Go ahead and make the windows counter
            string             instanceName          = GetInstanceName(alias);
            PerformanceCounter newPerformanceCounter = GetPerformanceCounter(categoryName, counterName, instanceName);

            //and now pass it to our normal add or get routine.
            return(AddOrGet(newPerformanceCounter, alias));
        }
Exemple #3
0
 /// <summary>
 /// Create a new performance counter metric object from the provided windows performance counter
 /// </summary>
 /// <remarks>The new metric will automatically be added to the metric definition's metrics collection.</remarks>
 /// <param name="definition">The metric definition for the provided windows performance counter</param>
 /// <param name="counter">The windows performance counter to add as a metric</param>
 /// <param name="alias">An alias to use to determine the instance name instead of the instance of the supplied counter.</param>
 public PerfCounterMetric(PerfCounterMetricDefinition definition, PerformanceCounter counter, PerfCounterInstanceAlias alias)
     : base(definition, new PerfCounterMetricPacket(definition.Packet, counter))
 {
     m_InstanceAlias = alias;
     m_PollingState  = PerfCounterPollingState.Inactive;
 }
Exemple #4
0
 /// <summary>
 /// Create a new performance counter metric object from the provided raw data packet
 /// </summary>
 /// <remarks>The new metric will automatically be added to the metric definition's metrics collection.</remarks>
 /// <param name="definition">The object that defines this metric</param>
 /// <param name="packet">The raw data packet</param>
 internal PerfCounterMetric(PerfCounterMetricDefinition definition, PerfCounterMetricPacket packet)
     : base(definition, packet)
 {
     m_InstanceAlias = PerfCounterInstanceAlias.None;
     m_PollingState  = PerfCounterPollingState.Inactive;
 }
Exemple #5
0
        /// <summary>Creates a new performance counter metric instance or returns an existing one from the provided definition information, or returns any existing instance if found.</summary>
        /// <remarks>If the metric definition doesn't exist, it will be created.  If the metric doesn't exist, it will be created.
        /// If the metric definition does exist, but is not a Performance Counter Metric (or a derived class) an exception will be thrown.</remarks>
        /// <param name="newPerformanceCounter">The windows performance counter to add a definition for</param>
        /// <param name="alias">An alias to use to determine the instance name instead of the instance of the supplied counter.</param>
        /// <returns>The Performance Counter Metric object for the specified instance.</returns>
        public static PerfCounterMetric AddOrGet(PerformanceCounter newPerformanceCounter, PerfCounterInstanceAlias alias)
        {
            //we need to find the definition, adding it if necessary
            string            definitionKey = PerfCounterMetricDefinition.GetKey(newPerformanceCounter);
            IMetricDefinition definition;

            if (Log.Metrics.TryGetValue(definitionKey, out definition))
            {
                //if the metric definition exists, but is of the wrong type we have a problem.
                if ((definition is PerfCounterMetricDefinition) == false)
                {
                    throw new ArgumentException("A metric already exists with the provided type, category, and counter name but it is not compatible with being a performance counter metric.  This indicates a programming error in a client application or Gibraltar.");
                }
            }
            else
            {
                //we didn't find one, make a new one
                definition = new PerfCounterMetricDefinition(Log.Metrics, newPerformanceCounter);
            }

            //now we have our definition, proceed to create a new metric if it doesn't exist
            //Interesting note:  here is where we basically lock in an alias to be its initial value.
            string  metricKey = GetKey(newPerformanceCounter);
            IMetric metric;

            //see if we can get the metric already.  If not, we'll create it
            lock (((MetricCollection)definition.Metrics).Lock) //make sure the get & add are atomic
            {
                if (definition.Metrics.TryGetValue(metricKey, out metric) == false)
                {
                    metric = new PerfCounterMetric((PerfCounterMetricDefinition)definition, newPerformanceCounter, alias);
                }
            }

            return((PerfCounterMetric)metric);
        }