Exemplo n.º 1
0
        private static bool Exists(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);
            }

            return(PerformanceCounterCategory.Exists(performanceCounterCategoryAttribute.CategoryName));
        }
Exemplo n.º 2
0
        public ChoPerformanceCounterFactory(Type type)
        {
            ChoGuard.ArgumentNotNull(type, "Type");

            ChoPerformanceCounterCategoryAttribute performanceCounterCategoryAttribute = type.GetCustomAttribute <ChoPerformanceCounterCategoryAttribute>();

            if (performanceCounterCategoryAttribute == null)
            {
                throw new ApplicationException(String.Format("Type '{0}' is not performance counter type.", type.FullName));
            }

            CategoryName = performanceCounterCategoryAttribute.CategoryName;
            CategoryHelp = performanceCounterCategoryAttribute.CaregoryHelp;
            CategoryType = performanceCounterCategoryAttribute.CategoryType;
        }
        private void AddCounters(Type[] reflectedTypes, bool silent)
        {
            ChoGuard.ArgumentNotNullOrEmpty(reflectedTypes, "reflectedTypes");

            string performanceCounterCategoryName = null;

            foreach (Type type in reflectedTypes)
            {
                if (type == null)
                {
                    continue;
                }

                ChoPerformanceCounterCategoryAttribute performanceCounterCategoryAttribute = type.GetCustomAttribute <ChoPerformanceCounterCategoryAttribute>(true);

                if (performanceCounterCategoryAttribute == null && !silent)
                {
                    throw new ApplicationException("Missing performance counter category attribute.");
                }

                if (_performanceCounterCategoryAttribute == null)
                {
                    _performanceCounterCategoryAttribute = performanceCounterCategoryAttribute;
                }

                if (performanceCounterCategoryName.IsNullOrEmpty())
                {
                    performanceCounterCategoryName = performanceCounterCategoryAttribute.CategoryName;
                }
                else if (performanceCounterCategoryName != performanceCounterCategoryAttribute.CategoryName)
                {
                    continue;
                }

                if (performanceCounterCategoryAttribute != null)
                {
                    AddCountersFromType(type);
                }
            }
        }
Exemplo n.º 4
0
        internal bool SelfInstallCounters(Type type)
        {
            bool selfInstall = true;

            if (type != null)
            {
                ChoPerformanceCounterCategoryAttribute performanceCounterCategoryAttribute = type.GetCustomAttribute <ChoPerformanceCounterCategoryAttribute>();
                XmlNode rootNode = RootNode;
                if (performanceCounterCategoryAttribute != null && rootNode != null)
                {
                    XmlNode categoryNode = rootNode.SelectSingleNode("//PerformanceCounterCategory[@name='{0}']".FormatString(performanceCounterCategoryAttribute.CategoryName));
                    if (categoryNode != null)
                    {
                        XmlAttribute attribute = categoryNode.Attributes["selfInstall"];
                        if (attribute != null)
                        {
                            bool.TryParse(attribute.Value, out selfInstall);
                        }
                    }
                }
            }
            return(selfInstall);
        }
Exemplo n.º 5
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);
        }