public static void EnsureCategory(IICPerformanceCountersAttribute categoryAttr, IEnumerable <IICPerformanceCounter> counters)
        {
            bool needCreate;

            if (!PerformanceCounterCategory.Exists(categoryAttr.CategoryName))
            {
                needCreate = true;
            }
            else
            {
                needCreate = false;
                foreach (IICPerformanceCounter counter in counters)
                {
                    if (!PerformanceCounterCategory.CounterExists(counter._rawAttr.CounterName, categoryAttr.CategoryName))
                    {
                        needCreate = true;
                        break;
                    }
                }

                if (needCreate)
                {
                    PerformanceCounterCategory.Delete(categoryAttr.CategoryName);
                }
            }

            if (needCreate)
            {
                CreateCategory(categoryAttr, counters);
            }
        }
Example #2
0
        private static T CreateCounters <T>(string instance)
        {
            T result = Activator.CreateInstance <T>();

            try
            {
                IICPerformanceCountersAttribute categoryAttr = AttributeHelper.
                                                               GetAttribute <IICPerformanceCountersAttribute>(typeof(T));

                List <IICPerformanceCounter> counters = new List <IICPerformanceCounter>();

                FieldInfo[] fields = typeof(T).GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

                foreach (FieldInfo fieldInfo in fields)
                {
                    IICPerformanceCounterAttribute counterAttr = AttributeHelper.
                                                                 GetAttribute <IICPerformanceCounterAttribute>(fieldInfo);

                    IICPerformanceCounter perfCounter = new IICPerformanceCounter();

                    perfCounter._rawAttr = counterAttr;
                    counters.Add(perfCounter);

                    fieldInfo.SetValue(result, perfCounter);
                }

                IICPerformanceCounterMananger.CreateCounters(instance, categoryAttr, counters);
            }
            catch (Exception ex)
            {
                //SystemLog.Warn(LogEventID.PerformanceCounterFailed, ex, "PerformanceCounter<{0}>({1}) Create Failed.", typeof(T).Name, instance);
            }
            return(result);
        }
        public static void DoCreateCounters(string instance, IICPerformanceCountersAttribute categoryAttr, IEnumerable <IICPerformanceCounter> counters)
        {
            switch (categoryAttr.CategoryType)
            {
            case PerformanceCounterCategoryType.SingleInstance:
                instance = string.Empty;
                break;

            case PerformanceCounterCategoryType.MultiInstance:
                if (string.IsNullOrEmpty(instance))
                {
                    instance = Process.GetCurrentProcess().ProcessName;
                }
                break;

            default:
                break;
            }

            foreach (IICPerformanceCounter counter in counters)
            {
                if (string.IsNullOrEmpty(instance))
                {
                    counter._counter = new PerformanceCounter(categoryAttr.CategoryName, counter._rawAttr.CounterName, false);

                    if (counter._baseAttr != null)
                    {
                        counter._baseCounter = new PerformanceCounter(categoryAttr.CategoryName, counter._baseAttr.CounterName, false);
                    }
                }
                else
                {
                    counter._counter = new PerformanceCounter(categoryAttr.CategoryName, counter._rawAttr.CounterName, instance, false);

                    if (counter._baseAttr != null)
                    {
                        counter._baseCounter = new PerformanceCounter(categoryAttr.CategoryName, counter._baseAttr.CounterName, instance, false);
                    }
                }
                counter._rawAttr  = null;
                counter._baseAttr = null;
            }
        }
        public static void CreateCounters(string instance, IICPerformanceCountersAttribute categoryAttr, IEnumerable <IICPerformanceCounter> counters)
        {
            try
            {
                foreach (IICPerformanceCounter counter in counters)
                {
                    EvoluteCounter(counter);
                }

                EnsureCategory(categoryAttr, counters);
                DoCreateCounters(instance, categoryAttr, counters);

                foreach (IICPerformanceCounter counter in counters)
                {
                    counter.Reset();
                }
            }
            catch (Exception ex)
            {
                //  SystemLog.Warn(LogEventID.PerformanceCounterFailed, ex, "PerformanceCounter<{0}> Create Failed", categoryAttr.CategoryName);
            }
        }
        public static void CreateCategory(IICPerformanceCountersAttribute categoryAttr, IEnumerable <IICPerformanceCounter> counters)
        {
            CounterCreationDataCollection ccdc = new CounterCreationDataCollection();

            foreach (IICPerformanceCounter counter in counters)
            {
                CounterCreationData ccd = new CounterCreationData();
                ccd.CounterType = counter._rawAttr.CounterType;
                ccd.CounterName = counter._rawAttr.CounterName;
                ccd.CounterHelp = counter._rawAttr.CounterHelp;
                ccdc.Add(ccd);

                if (counter._baseAttr != null)
                {
                    CounterCreationData baseCcd = new CounterCreationData();
                    baseCcd.CounterType = counter._baseAttr.CounterType;
                    baseCcd.CounterName = counter._baseAttr.CounterName;
                    baseCcd.CounterHelp = counter._baseAttr.CounterHelp;
                    ccdc.Add(baseCcd);
                }
            }

            PerformanceCounterCategory.Create(categoryAttr.CategoryName, categoryAttr.CategoryHelp, categoryAttr.CategoryType, ccdc);
        }
Example #6
0
 public IICPerformanceCounterCategory(string categoryName, PerformanceCounterCategoryType categoryType, string categoryHelp)
 {
     _categoryAttribute = new IICPerformanceCountersAttribute(categoryName, categoryType, categoryHelp);
 }