コード例 #1
0
        public void CanGetCounterHelpText()
        {
            PerformanceCounterAttribute attribute = new PerformanceCounterAttribute("counter", "CategoryHelp", PerformanceCounterType.NumberOfItems64);
            string translatedHelp = PerformanceCounterInstallerBuilder.GetCounterHelp(attribute.CounterHelp, this.GetType().Assembly);

            Assert.AreEqual(Resources.CategoryHelp, translatedHelp);
        }
        void CollectPerformanceCounters(Type instrumentedType,
                                        PerformanceCounterInstaller installer)
        {
            foreach (FieldInfo field in instrumentedType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                object[] attributes = field.GetCustomAttributes(typeof(PerformanceCounterAttribute), false);
                if (attributes.Length == 1)
                {
                    PerformanceCounterAttribute attribute = (PerformanceCounterAttribute)attributes[0];

                    CounterCreationData counter = GetExistingCounter(installer, attribute.CounterName);
                    if (counter == null)
                    {
                        installer.Counters.Add(
                            new CounterCreationData(attribute.CounterName, GetCounterHelp(attribute.CounterHelp, instrumentedType.Assembly), attribute.CounterType));
                        if (attribute.HasBaseCounter())
                        {
                            installer.Counters.Add(
                                new CounterCreationData(attribute.BaseCounterName, GetCounterHelp(attribute.BaseCounterHelp, instrumentedType.Assembly), attribute.BaseCounterType));
                        }
                    }
                    else
                    {
                        if (counter.CounterType != attribute.CounterType || !counter.CounterHelp.Equals(GetCounterHelp(attribute.CounterHelp, instrumentedType.Assembly)))
                        {
                            throw new InvalidOperationException(
                                      string.Format(
                                          CultureInfo.CurrentCulture,
                                          Resources.ExceptionPerformanceCounterRedefined,
                                          counter.CounterName,
                                          installer.CategoryName,
                                          instrumentedType.FullName));
                        }

                        // ignore new definition if equal
                    }
                }
            }
        }