/// <summary>
        /// Populates the CounterCreationDataCollection for a given type.
        /// </summary>
        /// <param name="type">Type to search for performance counters.</param>
        private void AddCountersFromType(Type type)
        {
            foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
            {
                // ignore member if it is not a performance counter
                if (fieldInfo.FieldType != typeof(ChoPerformanceCounter))
                {
                    continue;
                }

                // get the performance counter attribute
                ChoPerformanceCounterAttribute performanceCounterAttribute = fieldInfo.GetCustomAttribute <ChoPerformanceCounterAttribute>(false);
                // ignore it if it has no performance counter attribute set
                if (performanceCounterAttribute == null)
                {
                    continue;
                }

                // only create a counter with multiple instances once
                if (!performanceCounterAttribute.CounterInstanceName.IsNullOrEmpty() && _counterCreationData.ContainsKey(performanceCounterAttribute.CounterName))
                {
                    continue;
                }

                if (_counterCreationData.ContainsKey(performanceCounterAttribute.CounterName))
                {
                    continue;
                }

                foreach (CounterCreationData counterCreationData in performanceCounterAttribute.CreateCounters())
                {
                    _counterCreationData.Add(counterCreationData.CounterName, counterCreationData);
                }
            }

            foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
            {
                if (!propertyInfo.CanWrite || propertyInfo.GetIndexParameters().Length > 0)
                {
                    continue;
                }

                // ignore member if it is not a performance counter
                if (propertyInfo.PropertyType != typeof(ChoPerformanceCounter))
                {
                    continue;
                }

                // get the performance counter attribute
                ChoPerformanceCounterAttribute performanceCounterAttribute = propertyInfo.GetCustomAttribute <ChoPerformanceCounterAttribute>(false);
                // ignore it if it has no performance counter attribute set
                if (performanceCounterAttribute == null)
                {
                    continue;
                }

                // only create a counter with multiple instances once
                if (!performanceCounterAttribute.CounterInstanceName.IsNullOrEmpty() && _counterCreationData.ContainsKey(performanceCounterAttribute.CounterName))
                {
                    continue;
                }

                if (_counterCreationData.ContainsKey(performanceCounterAttribute.CounterName))
                {
                    continue;
                }

                foreach (CounterCreationData counterCreationData in performanceCounterAttribute.CreateCounters())
                {
                    _counterCreationData.Add(counterCreationData.CounterName, counterCreationData);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Creates instances for all performance counter members in the given type.
        /// </summary>
        /// <remarks>
        /// The type must have the PerformanceCounterCategory attribute set. Each performance counter
        /// member must be static and tagged with a PerformanceCounter attribute.
        /// </remarks>
        /// <param name="type">Type to instantiate counters</param>
        /// <param name="instance">Instance to assign performance counters to.</param>
        /// <returns><b>True</b> if counters were created successfully, <b>false</b> otherwise.</returns>
        private static bool CreateCounters(Type type, object instance)
        {
            // get category attribute
            ChoPerformanceCounterCategoryAttribute performanceCounterCategoryAttribute = type.GetCustomAttribute <ChoPerformanceCounterCategoryAttribute>(true);

            // we don't have performance counter category, we are done
            if (performanceCounterCategoryAttribute == null)
            {
                return(false);
            }

            string categoryName = performanceCounterCategoryAttribute.CategoryName;

            bool result = false;

            try
            {
                if (PerformanceCounterCategory.Exists(categoryName))
                {
                    // get the category type
                    PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName);

                    foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
                    {
                        if (fieldInfo.FieldType != typeof(ChoPerformanceCounter))
                        {
                            continue;
                        }

                        try
                        {
                            ChoPerformanceCounterAttribute performanceCounterAttribute = fieldInfo.GetCustomAttribute <ChoPerformanceCounterAttribute>();
                            if (performanceCounterAttribute == null)
                            {
                                continue;
                            }

                            if (performanceCounterAttribute.MachineName.IsNullOrEmpty() ||
                                (!performanceCounterAttribute.MachineName.IsNullOrEmpty() && String.Compare(performanceCounterAttribute.MachineName, Environment.MachineName, true) == 0))
                            {
                                if (fieldInfo.IsStatic && fieldInfo.GetValue(instance) != null)
                                {
                                    continue;
                                }

                                string instanceName = ChoPerformanceCounter.DefaultInstanceName;
                                // use a default instance name if the the counter does not have one and the category is marked MultiInstance
                                if (category.CategoryType == PerformanceCounterCategoryType.MultiInstance)
                                {
                                    instanceName = performanceCounterAttribute.CounterInstanceName.IsNullOrEmpty() ? instanceName : performanceCounterAttribute.CounterInstanceName;
                                }

                                // assign the performance counter
                                fieldInfo.SetValue(instance, new ChoPerformanceCounter(categoryName, performanceCounterAttribute.CounterName, performanceCounterAttribute.CounterType, instanceName, false));
                            }
                        }
                        catch (Exception innerEx)
                        {
                            ChoTrace.Error(innerEx);
                        }
                    }

                    foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
                    {
                        if (propertyInfo.PropertyType != typeof(ChoPerformanceCounter))
                        {
                            continue;
                        }

                        try
                        {
                            if (!propertyInfo.CanWrite || propertyInfo.GetIndexParameters().Length > 0)
                            {
                                continue;
                            }

                            ChoPerformanceCounterAttribute performanceCounterAttribute = propertyInfo.GetCustomAttribute <ChoPerformanceCounterAttribute>();
                            if (performanceCounterAttribute == null)
                            {
                                continue;
                            }

                            if (performanceCounterAttribute.MachineName.IsNullOrEmpty() ||
                                (!performanceCounterAttribute.MachineName.IsNullOrEmpty() && String.Compare(performanceCounterAttribute.MachineName, Environment.MachineName, true) == 0))
                            {
                                if (propertyInfo.GetSetMethod(true).IsStatic&& propertyInfo.GetValue(instance, null) != null)
                                {
                                    continue;
                                }

                                // use a default instance name if the the counter does not have one and the category is marked MultiInstance
                                string instanceName = ChoPerformanceCounter.DefaultInstanceName;
                                // use a default instance name if the the counter does not have one and the category is marked MultiInstance
                                if (category.CategoryType == PerformanceCounterCategoryType.MultiInstance)
                                {
                                    instanceName = performanceCounterAttribute.CounterInstanceName.IsNullOrEmpty() ? instanceName : performanceCounterAttribute.CounterInstanceName;
                                }

                                // assign the performance counter
                                propertyInfo.SetValue(instance, new ChoPerformanceCounter(categoryName, performanceCounterAttribute.CounterName, performanceCounterAttribute.CounterType, instanceName, false), null);
                            }
                        }
                        catch (Exception innerEx)
                        {
                            ChoTrace.Error(innerEx);
                        }
                    }
                    result = true;
                }
            }
            catch (Exception outerEx)
            {
                ChoTrace.Error(outerEx);
            }

            return(result);
        }