服务计数器

    /// <summary>

    /// CLR Version: 2.0.50727.5448

    /// NameSpace: Performence.Impl

    /// FileName: PerformenceCounter

    ///

    /// Created at 2014/03/18 10:23:38

    /// </summary>

    public class PerformenceCounter

    {

        #region fields



        private static readonly PerformenceCounter _current = new PerformenceCounter();

        private static readonly IDictionary<string, PerformanceCounter> PerfCounters = new Dictionary<string, PerformanceCounter>();



        private const string CategoryName = "PerformenceCounter.Service.Search";



        private const string SendPerSecond = "send/sec";

        private const string SendTotal = "send total";

        private const string SendFailedPerSecond = "failed/sec";

        private const string SendFailedTotal = "failed total";

        private const string ExceptionPerSecond = "error/sec";

        private const string ExcerptionTotal = "error total";

        private const string CacheHitPerSecond = "cache hit/sec";

        private const string CacheHitTotal = "cache hit total";



        private const string CacheFailedTotal = "cache failed total";



        #endregion



        #region .ctor



        #endregion



        #region properties



        public static PerformenceCounter Current

        {

            get { return _current; }

        }



        #endregion



        #region methods



        private void IncremnetPerCounter(string perfCounterName)

        {

            GetPerformanceCounter(perfCounterName).Increment();

        }



        private PerformanceCounter GetPerformanceCounter(string perfCounterName)

        {

            return PerfCounters[perfCounterName];

        }



        /// <summary>

        /// 创建计数器

        /// </summary>

        private void CreateCounters()

        {

            // 检查性能计数器是否存在

            Clear();



            CounterCreationDataCollection counterCollection = new CounterCreationDataCollection();



            CounterCreationData ccd1 = new CounterCreationData(SendPerSecond, "每秒发送数", PerformanceCounterType.RateOfCountsPerSecond64);

            counterCollection.Add(ccd1);



            CounterCreationData ccd2 = new CounterCreationData(SendTotal, "发送总数", PerformanceCounterType.NumberOfItems64);

            counterCollection.Add(ccd2);



            CounterCreationData ccd3 = new CounterCreationData(SendFailedPerSecond, "每秒发送失败数", PerformanceCounterType.RateOfCountsPerSecond64);

            counterCollection.Add(ccd3);



            CounterCreationData ccd4 = new CounterCreationData(SendFailedTotal, "发送失败总数", PerformanceCounterType.NumberOfItems64);

            counterCollection.Add(ccd4);



            CounterCreationData ccd5 = new CounterCreationData(ExceptionPerSecond, "每秒错误数(发送后超时或者异常数)", PerformanceCounterType.RateOfCountsPerSecond64);

            counterCollection.Add(ccd5);



            CounterCreationData ccd6 = new CounterCreationData(ExcerptionTotal, "错误总数(发送后超时或者异常数)", PerformanceCounterType.NumberOfItems64);

            counterCollection.Add(ccd6);



            CounterCreationData ccd7 = new CounterCreationData(CacheHitPerSecond, "每秒缓存命中数", PerformanceCounterType.RateOfCountsPerSecond64);

            counterCollection.Add(ccd7);



            CounterCreationData ccd8 = new CounterCreationData(CacheHitTotal, "缓存命中总数", PerformanceCounterType.NumberOfItems64);

            counterCollection.Add(ccd8);



            CounterCreationData ccd9 = new CounterCreationData(CacheFailedTotal, "缓存失效总数", PerformanceCounterType.NumberOfItems64);

            counterCollection.Add(ccd9);



            PerformanceCounterCategory.Create(CategoryName, "PerformanceCounter.Service.Search 发送计数器", PerformanceCounterCategoryType.SingleInstance, counterCollection);

        }



        public void Initiailize()

        {

            //创建计数器

            CreateCounters();



            // 添加到缓存中

            PerfCounters.Add(SendPerSecond, new PerformanceCounter(CategoryName, SendPerSecond, false));

            PerfCounters.Add(SendTotal, new PerformanceCounter(CategoryName, SendTotal, false));

            PerfCounters.Add(SendFailedPerSecond, new PerformanceCounter(CategoryName, SendFailedPerSecond, false));

            PerfCounters.Add(SendFailedTotal, new PerformanceCounter(CategoryName, SendFailedTotal, false));

            PerfCounters.Add(ExceptionPerSecond, new PerformanceCounter(CategoryName, ExceptionPerSecond, false));

            PerfCounters.Add(ExcerptionTotal, new PerformanceCounter(CategoryName, ExcerptionTotal, false));

            PerfCounters.Add(CacheHitPerSecond, new PerformanceCounter(CategoryName, CacheHitPerSecond, false));

            PerfCounters.Add(CacheHitTotal, new PerformanceCounter(CategoryName, CacheHitTotal, false));

        }



        /// <summary>

        /// 清理性能计数器

        /// </summary>

        public void Clear()

        {

            if (PerformanceCounterCategory.Exists(CategoryName))

            {

                PerformanceCounterCategory.Delete(CategoryName);

            }

        }





        /// <summary>

        /// 发送计数

        /// </summary>

        public void IncrementSendTotal()

        {

            IncremnetPerCounter(SendTotal);

            IncremnetPerCounter(SendPerSecond);

        }



        /// <summary>

        /// 失败计数

        /// </summary>

        public void IncrementSendFaildTotal()

        {

            IncremnetPerCounter(SendFailedTotal);

            IncremnetPerCounter(SendFailedPerSecond);

        }



        /// <summary>

        /// 异常计数

        /// </summary>

        public void IncrementExcetionTotal()

        {

            IncremnetPerCounter(ExcerptionTotal);

            IncremnetPerCounter(ExceptionPerSecond);

        }



        /// <summary>

        /// 缓存命中计数

        /// </summary>

        public void IncrementCacheHitTotal()

        {

            IncremnetPerCounter(CacheHitTotal);

            IncremnetPerCounter(CacheHitPerSecond);

        }



        /// <summary>

        /// 缓存失效计数

        /// </summary>

        public void IncrementCacheFaildTotal()

        {

            IncremnetPerCounter(CacheFailedTotal);

        }



        #endregion

    }

 计数器:

   PerformenceCounter.Current.Initiailize();

   PerformenceCounter.Current.IncrementSendTotal();

   PerformenceCounter.Current.IncrementCacheHitTotal();

   PerformenceCounter.Current.IncrementCacheFaildTotal();

   PerformenceCounter.Current.IncrementSendFaildTotal();   

   //当没有缓存命中 直接记异常
  PerformenceCounter.Current.IncrementExcetionTotal();

你可能感兴趣的:(服务)