コード例 #1
0
        /// <summary>
        /// Return a SimpleCounter for use by the caller
        /// </summary>
        /// <param name="counterInstanceName">Performance counter instance name</param>
        /// <param name="count">the value for a single sample being averaged</param>
        /// <param name="lockType">some type used internally for locking</param>
        /// <param name="isMonitoringActive">caller can forward external monitoring control</param>
        public static SimpleCounter GetSimpleCounter(string counterInstanceName, System.Type lockType, bool isMonitoringActive)
        {
            SimpleCounter simpleCounter = null;

            try
            {
                // access counter in hashlist if it is there, else null
                CounterBase counter = (CounterBase)PerformanceCounterFacade.Counters[counterInstanceName];
                // allocate counter if needed
                simpleCounter = (SimpleCounter)PerformanceCounterFacade.SetCounter(lockType, ref counter, "SimpleCounter", counterInstanceName, isMonitoringActive);
                // persist static, threadsafe counter (null if perf monitoring off) for reuse by validator instances
                PerformanceCounterFacade.Counters[counterInstanceName] = simpleCounter;
            }
            catch (Exception ex)
            {
                NameValueCollection nvc = new NameValueCollection(2);
                nvc.Add("Method", "MonitorAverageCount");
                nvc.Add("Probable cause", "PerfCounters not registered or wrong category in config file");
                new LoggableException(ex, nvc);
            }
            return(simpleCounter);
        }
コード例 #2
0
 /// <summary>
 /// Resets the counter
 /// </summary>
 /// <returns></returns>
 public long Reset(SimpleCounter simple)
 {
     return(simple.theCounter.IncrementBy(-simple.theCounter.RawValue));
 }
コード例 #3
0
        /// <summary>
        /// Synchonize creation and destruction of performance counter references
        /// </summary>
        /// <param name="pc">reference to perfcounter variable from caller</param>
        /// <param name="counterType">underlying perf counter type for which new instance might be created</param>
        /// <param name="instancename">instance name to be assigned for new counter instance</param>
        /// <param name="si">sampling interval for new instance</param>
        /// <param name="estsamplesize">estimated initial sample size</param>
        /// <returns>true if perf counter ref is non null and monitoring should be done</returns>
        /// <remarks>This was done in order to have monitoring respond to config setting and dynamically allocate
        /// and deallocate performance counters according to the setting. It also keeps a lot of
        /// potentially messy code in one place</remarks>
        public static CounterBase SetCounter(Type senderType, ref CounterBase pc, string counterType, string instancename, bool isMonitoringOn)
        {
            // if they should continue not monitoring then exit
            if (!isMonitoringOn && pc == null)
            {
                return(null);
            }
            // if they should continue monitoring then exit
            if (isMonitoringOn && pc != null)
            {
                return(pc);
            }

            // either they are monitoring and should stop or they aren't and they should start

            CounterBase newpc = null;


            if (isMonitoringOn)
            {
                if (pc == null)
                {
                    switch (counterType)
                    {
                    case "SimpleCounter":
                        newpc = new SimpleCounter(instancename);
                        break;

                    case "Average":
                        newpc = new AverageCounter(instancename);
                        break;

                    case "Percent":
                        newpc = new PercentCounter(instancename);
                        break;

                    case "AverageResponse":
                        newpc = new AverageResponseTimeCounter(instancename);
                        break;

                    case "Rate":
                        newpc = new RateCounter(instancename);
                        break;

                    default:
                        break;
                    }
                }
            }

            // since perf counters are static shared objects we must lock
            lock (senderType)
            {
                // change their monitoring state from on to off or from off to on
                if (newpc == null)
                {
                    pc.theCounter.RemoveInstance();
                }
                pc = newpc;
            }

            return(newpc);
        }