Exemple #1
0
    static void Main(string[] args)
    {
        if (args.Length <= 1)
        {
            System.Console.WriteLine("Please enter CounterGroup and Counters");
            System.Console.WriteLine("Usage: executable <CounterGroup> <Counter1> <CounterN>");
            return;
        }

        if (args.Length == 2)
        {
            if (args[0].Equals("delete"))
            {
                if (PerformanceCounterCategory.Exists(args[1]))
                {
                    PerformanceCounterCategory.Delete(args[1]);
                    Console.Out.WriteLine("Deleting perfGroup: " + args[1]);
                }
                return;
            }
        }

        CounterCreationDataCollection col = new CounterCreationDataCollection();

        String metrics_name = args[0];

        for (int i = 1; i < args.Length; i++)
        {
            Console.Out.WriteLine("Adding counters: " + args[i]);
            CounterCreationData counter = new CounterCreationData();
            counter.CounterName = args[i];
            counter.CounterHelp = args[i];
            counter.CounterType = PerformanceCounterType.NumberOfItems32;
            col.Add(counter);
        }

        if (PerformanceCounterCategory.Exists(metrics_name))
        {
            PerformanceCounterCategory.Delete(metrics_name);
            Console.Out.WriteLine("Deleting perfGroup:" + metrics_name);
        }

        PerformanceCounterCategory category = PerformanceCounterCategory.Create(metrics_name,
                                                                                "Perf Category Description ", col);

        Console.Out.WriteLine("Creating perfGroup:" + metrics_name);
    }
Exemple #2
0
        private static void CounterInitialize()
        {
            try { PerformanceCounterCategory.Delete("FileSorter"); } catch (Exception) { }

            CounterCreationDataCollection counters = new CounterCreationDataCollection();

            CounterCreationData fileDetectedCount = new CounterCreationData()
            {
                CounterName = "FileDetectedCount",
                CounterType = PerformanceCounterType.NumberOfItems32
            };
            CounterCreationData ruleFoundCount = new CounterCreationData()
            {
                CounterName = "RuleFoundCount",
                CounterType = PerformanceCounterType.NumberOfItems32
            };
            CounterCreationData fileMovedCount = new CounterCreationData()
            {
                CounterName = "FileMovedCount",
                CounterType = PerformanceCounterType.NumberOfItems32
            };

            counters.Add(fileDetectedCount);
            counters.Add(ruleFoundCount);
            counters.Add(fileMovedCount);

            try
            {
                PerformanceCounterCategory.Create(
                    "FileSorter", "...",
                    PerformanceCounterCategoryType.MultiInstance, counters);
            }
            catch (Exception) { }

            using (var counter = new PerformanceCounter("FileSorter", "FileDetectedCount", "FileSorter project", false))
            {
                counter.RawValue = 0;
            }
            using (var counter = new PerformanceCounter("FileSorter", "RuleFoundCount", "FileSorter project", false))
            {
                counter.RawValue = 0;
            }
            using (var counter = new PerformanceCounter("FileSorter", "FileMovedCount", "FileSorter project", false))
            {
                counter.RawValue = 0;
            }
        }
Exemple #3
0
        public static void RegisterContract(Type contractType)
        {
            var categoryAttr = contractType.GetCustomAttribute <PerfCounterCategoryAttribute>();

            if (null == categoryAttr)
            {
                throw new InvalidContractException($"Expected class with a {typeof(PerfCounterCategoryAttribute)} attribute");
            }


            if (PerformanceCounterCategory.Exists(categoryAttr.Name))
            {
                return;
                //throw new CategoryAlreadyExistsException($"Category {categoryAttr.Name} already exists");
            }

            var counterCreationDataCollection = new CounterCreationDataCollection();

            foreach (var prop in contractType.GetProperties())
            {
                var attr = prop.GetCustomAttribute <PerfCounterAttribute>();
                if (null == attr)
                {
                    continue;
                }

                if (prop.GetGetMethod() == null)
                {
                    throw new InvalidContractException($"Property {prop.Name} must have a getter and a setter to be valid.");
                }

                if (prop.PropertyType.BaseType.FullName != typeof(PerformanceCounterBase).FullName)
                {
                    throw new InvalidContractException($"Property {prop.Name} must be of {typeof(PerformanceCounterBase).FullName} type.");
                }

                if (prop.PropertyType.CustomAttributes == null || !prop.PropertyType.CustomAttributes.Any(x => x.AttributeType.FullName == typeof(CounterTypeAttribute).FullName))
                {
                    throw new InvalidContractException($"Property {prop.Name} must have a {typeof(CounterTypeAttribute).FullName} attribute.");
                }

                var counterList = GetCounterCreationData(prop.PropertyType, attr);
                counterCreationDataCollection.AddRange(counterList.ToArray());
            }

            PerformanceCounterCategory.Create(categoryAttr.Name, categoryAttr.Help, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection);
        }
    public static void Main()
    {
        try
        {
            //<Snippet1>
            string myCategoryName;
            int    numberOfCounters;
            Console.Write("Enter the number of counters : ");
            numberOfCounters = int.Parse(Console.ReadLine());
            CounterCreationData[] myCounterCreationData =
                new CounterCreationData[numberOfCounters];
            for (int i = 0; i < numberOfCounters; i++)
            {
                Console.Write("Enter the counter name for {0} counter : ", i);
                myCounterCreationData[i]             = new CounterCreationData();
                myCounterCreationData[i].CounterName = Console.ReadLine();
            }
            CounterCreationDataCollection myCounterCollection =
                new CounterCreationDataCollection(myCounterCreationData);
            Console.Write("Enter the category Name:");
            myCategoryName = Console.ReadLine();
            // Check if the category already exists or not.
            if (!PerformanceCounterCategory.Exists(myCategoryName))
            {
                CounterCreationDataCollection myNewCounterCollection =
                    new CounterCreationDataCollection(myCounterCollection);
                PerformanceCounterCategory.Create(myCategoryName, "Sample Category",
                                                  PerformanceCounterCategoryType.SingleInstance, myNewCounterCollection);

                Console.WriteLine("The list of counters in 'CounterCollection' are : ");
                for (int i = 0; i < myNewCounterCollection.Count; i++)
                {
                    Console.WriteLine("Counter {0} is '{1}'", i, myNewCounterCollection[i].CounterName);
                }
            }
            else
            {
                Console.WriteLine("The category already exists");
            }
            //</Snippet1>
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: {0}.", e.Message);
            return;
        }
    }
        private static bool CreatePerformanceCounters()
        {
            if (!PerformanceCounterCategory.Exists("MyCategory"))
            {
                CounterCreationDataCollection counters = new CounterCreationDataCollection
                {
                    new CounterCreationData("# operations executed", "Total number of operations executed", PerformanceCounterType.NumberOfItems32),
                    new CounterCreationData("# operations / sec", "Number of operations executed per second", PerformanceCounterType.RateOfCountsPerSecond32)
                };

                PerformanceCounterCategory.Create("MyCategory", "Sample category", PerformanceCounterCategoryType.SingleInstance, counters);

                return(true);
            }

            return(false);
        }
Exemple #6
0
        private static bool CreatePerformanceCounters()
        {
            if (!PerformanceCounterCategory.Exists("MyCategory"))
            {
                CounterCreationDataCollection counters = new CounterCreationDataCollection
                {
                    new CounterCreationData(
                        "# operations executed",
                        "Total number of operations executed",
                        PerformanceCounterType.NumberOfItems32),
                };
                PerformanceCounterCategory.Create("MyCategory", "Sample category for Codeproject", counters);

                return(true);
            }
            return(false);
        }
Exemple #7
0
        public void ProfileActivated()
        {
            Logger.Debug("Starting installation of  monitoring");

            if (PerformanceCounterCategory.Exists(CategoryName))
            {
                Logger.Warn(String.Format("Category {0} already exists, going to delete it first", CategoryName));
                PerformanceCounterCategory.Delete(CategoryName);
            }

            CounterCreationDataCollection counterData = new CounterCreationDataCollection();

            counterData.AddRange(this.InstallAverageMessageProcessTimeCounter());
            counterData.AddRange(this.InstalledFailedMessageProcessingCounter());

            PerformanceCounterCategory.Create(CategoryName, "NServiceBus Monitoring", PerformanceCounterCategoryType.MultiInstance, counterData);
        }
Exemple #8
0
        private void Init(IEnumerable <AkkaMetric> akkaMetrics)
        {
            var ccdc = new CounterCreationDataCollection();

            foreach (var akkaMetric in akkaMetrics)
            {
                akkaMetric.RegisterIn(ccdc);
            }

            if (PerformanceCounterCategory.Exists(PerformanceCountersCategoryName))
            {
                PerformanceCounterCategory.Delete(PerformanceCountersCategoryName);
            }

            PerformanceCounterCategory.Create(PerformanceCountersCategoryName, "",
                                              PerformanceCounterCategoryType.MultiInstance, ccdc);
        }
        public static void installPerformanceCounter()
        {
            CounterCreationDataCollection CounterDatas = new System.Diagnostics.CounterCreationDataCollection();
            CounterCreationData           cdCounter1   = new System.Diagnostics.CounterCreationData();

            try {
                cdCounter1.CounterName = "Messages Per Second";
                cdCounter1.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
                CounterDatas.Add(cdCounter1);
                if (!((PerformanceCounterCategory.Exists("netSmtpMail"))))
                {
                    PerformanceCounterCategory.Create("netSmtpMail", "", CounterDatas);
                }
            } catch (Exception e) {
                throw (e);
            }
        }
Exemple #10
0
        public static void PerformanceCounterCategory_Create_Obsolete_CCD()
        {
            var name     = nameof(PerformanceCounterCategory_Create_Obsolete_CCD) + "_Counter";
            var category = name + "_Category";

            CounterCreationData           ccd  = new CounterCreationData(name, "counter help", PerformanceCounterType.NumberOfItems32);
            CounterCreationDataCollection ccdc = new CounterCreationDataCollection();

            ccdc.Add(ccd);

            Helpers.DeleteCategory(category);

            PerformanceCounterCategory.Create(category, "category help", ccdc);

            Assert.True(PerformanceCounterCategory.Exists(category));
            PerformanceCounterCategory.Delete(category);
        }
        private static void CreatePerfCounters(List <Type> handlers)
        {
            if (PerformanceCounterCategory.Exists("SvnBridge") == false)
            {
                var creationDataCollection = new CounterCreationDataCollection();
                foreach (Type type in handlers)
                {
                    string handlerName = type.Name.Replace("Handler", "");
                    var    item        = new CounterCreationData(handlerName, "Track the number of " + handlerName,
                                                                 PerformanceCounterType.NumberOfItems64);

                    creationDataCollection.Add(item);
                }
                PerformanceCounterCategory.Create("SvnBridge", "Performance counters for Svn Bridge",
                                                  PerformanceCounterCategoryType.SingleInstance, creationDataCollection);
            }
        }
        private static void RegisterCategory()
        {
            if (PerformanceCounterCategory.Exists(ConfigurationManager.AppSettings["PerformanceCategoryName"]))
            {
                Console.WriteLine("Performance category already exists");
                return;
            }

            var counterCreationDataCollection = new CounterCreationDataCollection();
            var successfullLoginCounter       = new CounterCreationData
            {
                CounterName = ConfigurationManager.AppSettings["SuccessfullLoginCounterName"],
                CounterType = PerformanceCounterType.NumberOfItems32
            };

            counterCreationDataCollection.Add(successfullLoginCounter);

            var failedLoginCounter = new CounterCreationData
            {
                CounterName = ConfigurationManager.AppSettings["FailedLoginCounterName"],
                CounterType = PerformanceCounterType.NumberOfItems32
            };

            counterCreationDataCollection.Add(failedLoginCounter);

            var logoutCounter = new CounterCreationData
            {
                CounterName = ConfigurationManager.AppSettings["LogoutCounterName"],
                CounterType = PerformanceCounterType.NumberOfItems32
            };

            counterCreationDataCollection.Add(logoutCounter);

            try
            {
                PerformanceCounterCategory.Create(
                    ConfigurationManager.AppSettings["PerformanceCategoryName"], "",
                    PerformanceCounterCategoryType.SingleInstance,
                    counterCreationDataCollection);
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("Do not have permissions to create performance category");
            }
            Console.WriteLine("Performance category created");
        }
Exemple #13
0
        /// <summary>
        /// Creates the or initialize perf counters.
        /// </summary>
        private void CreateOrInitializePerfCounters()
        {
            if (!PerformanceCounterCategory.Exists(CategoryName))
            {
                CounterDataCollection.AddRange(new[] {
                    new CounterCreationData(TotalJniExceptions, string.Empty, PerformanceCounterType.NumberOfItems64),
                    new CounterCreationData(TotalRequests, string.Empty, PerformanceCounterType.NumberOfItems64),
                    new CounterCreationData(TotalSuccessExecution, string.Empty, PerformanceCounterType.NumberOfItems64),
                    new CounterCreationData(CachedJniMethodCall, string.Empty, PerformanceCounterType.NumberOfItems64),
                    new CounterCreationData(ProxyGeneration, string.Empty, PerformanceCounterType.NumberOfItems64),
                    new CounterCreationData(DbJniMethodCall, string.Empty, PerformanceCounterType.NumberOfItems64),
                });

                PerformanceCounterCategory.Create(CategoryName, CategoryName, PerformanceCounterCategoryType.MultiInstance, CounterDataCollection);
            }
            AddCountersToInstance();
        }
Exemple #14
0
        public static void CreateCategory()
        {
            if (PerformanceCounterCategory.Exists(Category))
            {
                PerformanceCounterCategory.Delete(Category);
            }

            var counters = new CounterCreationDataCollection();
            var data     = new CounterCreationData(
                "# Test counter", "Test counter.",
                PerformanceCounterType.NumberOfItems32);

            counters.Add(data);
            PerformanceCounterCategory.Create(
                Category, "Test counter for Mimax, Inc.",
                PerformanceCounterCategoryType.SingleInstance, counters);
        }
Exemple #15
0
        public SaveMetrics()
        {
            if (!PerformanceCounterCategory.Exists(PerformanceCategoryName))
            {
                CounterCreationDataCollection counters = new CounterCreationDataCollection();

                counters.Add(
                    new CounterCreationData("Save - Count", "Number of world saves.", PerformanceCounterType.NumberOfItems32));

                counters.Add(
                    new CounterCreationData(
                        "Save - Items/sec", "Number of items saved per second.", PerformanceCounterType.RateOfCountsPerSecond32));

                counters.Add(
                    new CounterCreationData(
                        "Save - Mobiles/sec", "Number of mobiles saved per second.", PerformanceCounterType.RateOfCountsPerSecond32));

                counters.Add(
                    new CounterCreationData(
                        "Save - Serialized bytes/sec",
                        "Amount of world-save bytes serialized per second.",
                        PerformanceCounterType.RateOfCountsPerSecond32));

                counters.Add(
                    new CounterCreationData(
                        "Save - Written bytes/sec",
                        "Amount of world-save bytes written to disk per second.",
                        PerformanceCounterType.RateOfCountsPerSecond32));

#if !MONO
                PerformanceCounterCategory.Create(
                    PerformanceCategoryName, PerformanceCategoryDesc, PerformanceCounterCategoryType.SingleInstance, counters);
#endif
            }

            numberOfWorldSaves = new PerformanceCounter(PerformanceCategoryName, "Save - Count", false);

            itemsPerSecond   = new PerformanceCounter(PerformanceCategoryName, "Save - Items/sec", false);
            mobilesPerSecond = new PerformanceCounter(PerformanceCategoryName, "Save - Mobiles/sec", false);

            serializedBytesPerSecond = new PerformanceCounter(PerformanceCategoryName, "Save - Serialized bytes/sec", false);
            writtenBytesPerSecond    = new PerformanceCounter(PerformanceCategoryName, "Save - Written bytes/sec", false);

            // increment number of world saves
            numberOfWorldSaves.Increment();
        }
Exemple #16
0
        private void StartForm_Load(object sender, System.EventArgs e)
        {
            // Ermitteln der Basis-Frequenz des Motherboard
            if (QueryPerformanceFrequency(ref performanceFrequency) == false)
            {
                MessageBox.Show("Die Basis-Motherboardfrequenz konnte nicht ermittelt werden",
                                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            // Ermitteln, ob die Leistungsindikatoren-Kategorie bereits existiert
            // und Löschen derselben falls dies der Fall ist
            if (PerformanceCounterCategory.Exists("Performance-Counter-Test"))
            {
                PerformanceCounterCategory.Delete("Performance-Counter-Test");
            }

            // Erzeugen einer neuen Leistungsindikatoren-Kategorie mit
            // zwei Leistungsindikatoren
            CounterCreationDataCollection ccdCol = new CounterCreationDataCollection();
            CounterCreationData           ccd1   = new CounterCreationData("Demo1", "Demo-Counter", PerformanceCounterType.NumberOfItems32);
            CounterCreationData           ccd2   = new CounterCreationData("Demo2", "Demo-Counter", PerformanceCounterType.CounterDelta32);
            CounterCreationData           ccd3   = new CounterCreationData("Demo3", "Demo-Counter", PerformanceCounterType.CounterDelta32);
            // CounterCreationData ccd4 = new CounterCreationData("Demo3Base", "Basis für Demo3", PerformanceCounterType.AverageBase);
            CounterCreationData ccd5 = new CounterCreationData("Demo4", "Demo-Counter", PerformanceCounterType.AverageTimer32);
            CounterCreationData ccd6 = new CounterCreationData("Demo4Base", "Basis für Demo4", PerformanceCounterType.AverageBase);

            ccdCol.Add(ccd1);
            ccdCol.Add(ccd2);
            ccdCol.Add(ccd3);
            //ccdCol.Add(ccd4);
            ccdCol.Add(ccd5);
            ccdCol.Add(ccd6);

            PerformanceCounterCategory.Create("Performance-Counter-Test", "Performance-Counter für Testzwecke", ccdCol);

            pc1 = new PerformanceCounter("Performance-Counter-Test", "Demo1", false);
            pc2 = new PerformanceCounter("Performance-Counter-Test", "Demo2", false);
            pc3 = new PerformanceCounter("Performance-Counter-Test", "Demo3", false);
            pc4 = new PerformanceCounter("Performance-Counter-Test", "Demo3Base", false);
            pc5 = new PerformanceCounter("Performance-Counter-Test", "Demo4", false);
            pc6 = new PerformanceCounter("Performance-Counter-Test", "Demo4Base", false);

            //pc3.NextValue();
            pc3.RawValue = 0;
            pc5.RawValue = 0;
        }
Exemple #17
0
        static JobPerformance()
        {
            var counterData = new CounterCreationDataCollection();

            var bCategoryExists = PerformanceCounterCategory.Exists(PerfCategoryName);

            if (!bCategoryExists || !PerformanceCounterCategory.CounterExists(PerfActiveItemsName, PerfCategoryName))
            {
                var counter = new CounterCreationData
                {
                    CounterName = PerfActiveItemsName,
                    CounterType = PerformanceCounterType.NumberOfItems32
                };
                counterData.Add(counter);
            }


            if (!bCategoryExists || !PerformanceCounterCategory.CounterExists(PerfTotalItemsName, PerfCategoryName))
            {
                var counter = new CounterCreationData
                {
                    CounterName = PerfTotalItemsName,
                    CounterType = PerformanceCounterType.NumberOfItems32
                };
                counterData.Add(counter);
            }

            if (!bCategoryExists || !PerformanceCounterCategory.CounterExists(PerfRemainingItemsName, PerfCategoryName))
            {
                var counter = new CounterCreationData
                {
                    CounterName = PerfRemainingItemsName,
                    CounterType = PerformanceCounterType.NumberOfItems32
                };
                counterData.Add(counter);
            }

            if (counterData.Count > 0)
            {
                PerformanceCounterCategory.Create(
                    PerfCategoryName,
                    "Performance information for the execution list in the ManagedBuilder",
                    PerformanceCounterCategoryType.MultiInstance,
                    counterData);
            }
        }
        /// <summary>
        /// Installs forwarding counters
        /// </summary>
        public static bool InstallCounters()
        {
            string message = String.Empty;

            try
            {
                log.InfoFormat("Creating performance counter category {0}", ForwardingCounters.PerformanceCategoryName);
                Console.WriteLine("Creating performance counter category " + ForwardingCounters.PerformanceCategoryName);
                CounterCreationDataCollection counterDataCollection = new CounterCreationDataCollection();

                for (int i = 0; i < ForwardingCounters.PerformanceCounterNames.Length; i++)
                {
                    counterDataCollection.Add(new CounterCreationData(ForwardingCounters.PerformanceCounterNames[i], ForwardingCounters.PerformanceCounterHelp[i], ForwardingCounters.PerformanceCounterTypes[i]));
                    message = "Creating perfomance counter " + ForwardingCounters.PerformanceCounterNames[i];
                    Console.WriteLine(message);
                    if (log.IsDebugEnabled)
                    {
                        log.Debug(message);
                    }
                }

                PerformanceCounterCategory.Create(ForwardingCounters.PerformanceCategoryName, "Counters for the MySpace Data Relay", PerformanceCounterCategoryType.MultiInstance, counterDataCollection);
                return(true);
            }
            catch (System.Security.SecurityException)
            {
                message = "Cannot automatically create Performance Counters for Relay Forwarder. Please run installutil against MySpace.DataRelay.RelayComponent.Forwarding.dll";
                Console.WriteLine(message);
                if (log.IsWarnEnabled)
                {
                    log.Warn(message);
                }
                return(false);
            }
            catch (Exception ex)
            {
                message = "Error creating Perfomance Counter Category " + ForwardingCounters.PerformanceCategoryName + ": " + ex.ToString() + ". Counter category will not be used.";
                Console.WriteLine(message);
                if (log.IsErrorEnabled)
                {
                    log.Error(message);
                }
                return(false);
            }
        }
        static Manager()
        {
#if PERFORMANCE_COUNTERS
            // create performance counters' category if necessary
            if (!PerformanceCounterCategory.Exists(ServerCategoryName))
            {
                var perfomanceCountersCollection = new CounterCreationDataCollection();

                // connections
                perfomanceCountersCollection.Add(
                    new CounterCreationData(connectionsCounterName,
                                            "Number of client's connections", PerformanceCounterType.NumberOfItems32));
                perfomanceCountersCollection.Add(
                    new CounterCreationData(maxConnectionsCounterName,
                                            "Maximum number of client's connections", PerformanceCounterType.NumberOfItems32));
                // bytes received
                perfomanceCountersCollection.Add(
                    new CounterCreationData(bytesReceivedPerSecCounterName,
                                            "Received bytes rate", PerformanceCounterType.RateOfCountsPerSecond32));
                perfomanceCountersCollection.Add(
                    new CounterCreationData(bytesReceivedCounterName,
                                            "Received bytes", PerformanceCounterType.NumberOfItems32));
                // bytes sent
                perfomanceCountersCollection.Add(
                    new CounterCreationData(bytesSentPerSecCounterName,
                                            "Sent bytes rate", PerformanceCounterType.RateOfCountsPerSecond32));
                perfomanceCountersCollection.Add(
                    new CounterCreationData(bytesSentCounterName,
                                            "Sent bytes", PerformanceCounterType.NumberOfItems32));
                // bytes total
                perfomanceCountersCollection.Add(
                    new CounterCreationData(bytesTotalPerSecCounterName,
                                            "Total bytes rate", PerformanceCounterType.RateOfCountsPerSecond32));
                perfomanceCountersCollection.Add(
                    new CounterCreationData(bytesTotalCounterName,
                                            "Total bytes", PerformanceCounterType.NumberOfItems32));

                PerformanceCounterCategory.Create(ServerCategoryName, "",
                                                  PerformanceCounterCategoryType.MultiInstance, perfomanceCountersCollection);
            }

            // create global performance counters
            CreatePerformanceCounters(globalPerformanceCounters, GlobalInstanceName);
#endif
        }
        //key:计数器名称,value:计数器描述
        public bool CreatePerformanceCounter(Dictionary <string, PerformanceCounterType> dictCounterInfo)
        {
            try {
                foreach (string counterName in dictCounterInfo.Keys)
                {
                    AddSimplePerformanceCounter(counterName, "", dictCounterInfo[counterName]);
                }

                PerformanceCounterCategory.Create(categoryName, "", PerformanceCounterCategoryType.MultiInstance, counterCollection);

                return(true);
            }
            catch (Exception e) {
                Console.Write(e.Message);
            }

            return(false);
        }
Exemple #21
0
        public static PerformanceCounter CreateCounter(string categoryName, PerformanceCounterType counterType)
        {
            string counterName = categoryName + "_Counter";

            CounterCreationDataCollection ccdc = new CounterCreationDataCollection();
            CounterCreationData           ccd  = new CounterCreationData();

            ccd.CounterType = counterType;
            ccd.CounterName = counterName;
            ccdc.Add(ccd);

            Helpers.DeleteCategory(categoryName);
            PerformanceCounterCategory.Create(categoryName, "description", PerformanceCounterCategoryType.SingleInstance, ccdc);

            Helpers.VerifyPerformanceCounterCategoryCreated(categoryName);

            return(new PerformanceCounter(categoryName, counterName, readOnly: false));
        }
 public static void Create()
 {
     if (!PerformanceCounterCategory.Exists("Membership"))
     {
         var ccd = new CounterCreationData("Logins", "Number of logins",
                                           PerformanceCounterType.NumberOfItems32);
         var ccdc = new CounterCreationDataCollection();
         ccdc.Add(ccd);
         ccd = new CounterCreationData("Ave Users", "Average number of users",
                                       PerformanceCounterType.AverageCount64);
         ccdc.Add(ccd);
         ccd = new CounterCreationData("Ave Users base", "Average number of users base",
                                       PerformanceCounterType.AverageBase);
         ccdc.Add(ccd);
         PerformanceCounterCategory.Create("Membership", "Website Membership system",
                                           PerformanceCounterCategoryType.MultiInstance, ccdc);
     }
 }
        private void EnsurePerfCategoryExist()
        {
            CounterCreationDataCollection ccdc = new CounterCreationDataCollection();
            CounterCreationData           ccd  = new CounterCreationData();

            ccd.CounterType = PerformanceCounterType.NumberOfItems32;
            ccd.CounterName = "HardProcedureQueries";
            ccdc.Add(ccd);

            ccd             = new CounterCreationData();
            ccd.CounterType = PerformanceCounterType.NumberOfItems32;
            ccd.CounterName = "SoftProcedureQueries";
            ccdc.Add(ccd);
            if (!PerformanceCounterCategory.Exists(Resources.PerfMonCategoryName))
            {
                PerformanceCounterCategory.Create(Resources.PerfMonCategoryName, "", new PerformanceCounterCategoryType(), ccdc);
            }
        }
Exemple #24
0
        /// <summary>
        ///   Ensures the Performance Counters exist.
        /// </summary>
        public static void EnsureExist()
        {
            if (!PerformanceCounterCategory.Exists(CategoryName))
            {
                CounterCreationDataCollection counters = new CounterCreationDataCollection();

                foreach (CounterCreationData counterCreationData in CounterDefinitions)
                {
                    counters.Add(counterCreationData);
                }

                PerformanceCounterCategory.Create(
                    CategoryName,
                    PerformanceCounterNameResources.PERFORMANCE_COUNTER_CATEGORY_HELP,
                    PerformanceCounterCategoryType.SingleInstance,
                    counters);
            }
        }
Exemple #25
0
        /// <summary>
        /// new
        /// </summary>
        static Counter()
        {
            if (PerformanceCounterCategory.Exists(CATEGORY_NAME))
            {
                return;
            }

            //try create counter category
            var coll = new CounterCreationDataCollection();

            coll.Add(new CounterCreationData("Post", "", PerformanceCounterType.RateOfCountsPerSecond32));
            coll.Add(new CounterCreationData("Executing", "", PerformanceCounterType.NumberOfItems32));
            coll.Add(new CounterCreationData("TimeTaken", "", PerformanceCounterType.NumberOfItems32));
            coll.Add(new CounterCreationData("Failed", "", PerformanceCounterType.RateOfCountsPerSecond32));

            try { PerformanceCounterCategory.Create(CATEGORY_NAME, "", PerformanceCounterCategoryType.MultiInstance, coll); }
            catch (Exception ex) { Trace.TraceError(ex.ToString()); }
        }
Exemple #26
0
        static void CleanUpCategory(CategoryConfiguration categoryConfiguration)
        {
            int missing = categoryConfiguration.Counters
                          .Where(counter => !PerformanceCounterCategory.CounterExists(counter.Name, categoryConfiguration.Name))
                          .Count();

            if (missing > 0)
            {
                PerformanceCounterCategory.Delete(categoryConfiguration.Name);

                PerformanceCounterCategory.Create(
                    categoryConfiguration.Name,
                    categoryConfiguration.Help,
                    PerformanceCounterCategoryType.MultiInstance,
                    new CounterCreationDataCollection(
                        categoryConfiguration.Counters.Select(x => (CounterCreationData)x).ToArray()));
            }
        }
        private void SetupCategory()
        {
            if (!PerformanceCounterCategory.Exists(_ctpCategoryName))
            {
                CounterCreationDataCollection counters = new CounterCreationDataCollection();

                for (int i = 0; i < _ctpPerformanceCounters.Length; i++)
                {
                    _ctpPerformanceCounters[i].AddCounterToCollection(counters);
                }

                PerformanceCounterCategory.Create(
                    _ctpCategoryName,
                    _ctpCategoryHelp,
                    PerformanceCounterCategoryType.MultiInstance,
                    counters);
            }
        }
Exemple #28
0
        PerformanceCounter GetCounter(string category, string counter,
                                      PerformanceCounterType type = PerformanceCounterType.AverageCount64,
                                      string machine = ".", string instance = "_Total")
        {
            if (!PerformanceCounterCategory.Exists(category))
            {
                // create category
                var counterInfos = new CounterCreationDataCollection();
                var counterInfo  = new CounterCreationData()
                {
                    CounterType = type,
                    CounterName = counter,
                };
                counterInfos.Add(counterInfo);
                PerformanceCounterCategory
                .Create(category, category, counterInfos);

                // check creation
                var counters
                    = new PerformanceCounterCategory(category, machine);
                if (!counters.CounterExists(counter))
                {
                    Debug.Fail("Counter was not created");
                }
                if (!counters.InstanceExists(instance))
                {
                    Debug.Fail("Instance not found");
                }
            }

            // get counter

            var perfCounter = new PerformanceCounter
            {
                CategoryName = category,
                CounterName  = counter,
                MachineName  = machine,
                ReadOnly     = false,
            };

            perfCounter.IncrementBy(10);

            return(perfCounter);
        }
Exemple #29
0
        static void Main(string[] args)
        {
            try
            {
                if (PerformanceCounterCategory.Exists(MusicStorePerformanceConstants.CategoryName))
                {
                    PerformanceCounterCategory.Delete(MusicStorePerformanceConstants.CategoryName);
                }

                CounterCreationDataCollection counters = new CounterCreationDataCollection();

                counters.Add(new CounterCreationData()
                {
                    CounterName = MusicStorePerformanceConstants.LoginCounterName,
                    CounterType = PerformanceCounterType.NumberOfItems32
                });

                counters.Add(new CounterCreationData()
                {
                    CounterName = MusicStorePerformanceConstants.LogoffCounterName,
                    CounterType = PerformanceCounterType.NumberOfItems32
                });

                counters.Add(new CounterCreationData()
                {
                    CounterName = MusicStorePerformanceConstants.PaymentsCounterName,
                    CounterType = PerformanceCounterType.NumberOfItems32
                });

                PerformanceCounterCategory.Create(
                    MusicStorePerformanceConstants.CategoryName,
                    string.Empty,
                    PerformanceCounterCategoryType.SingleInstance,
                    counters);

                Console.WriteLine($"Perfomance counters added. Category name: {MusicStorePerformanceConstants.CategoryName}");
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("You don't have needed permissions. Run app as administrator.");
            }

            Console.ReadLine();
        }
Exemple #30
0
        /// <summary>
        /// Installs performance counters provided by the the argument
        /// </summary>
        /// <param name="data">Data to create performance counters</param>
        public static PmcInstallationResult InstallPerformanceCounters(PmcCreationData data)
        {
            var errors     = new Dictionary <string, Exception>();
            var categories = new List <string>();

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            // --- Install categories one-by-one
            foreach (var category in data.Categories.Values.Where(category => !category.IsPredefined))
            {
                var counterData = new CounterCreationDataCollection();
                foreach (var counter in data.GetCounters(category.Name)
                         .Values.Where(counter => !counter.IsPredefined))
                {
                    counterData.Add(new CounterCreationData(counter.Name, counter.Help, counter.Type));
                }
                var categoryName = AppConfigurationManager.ProviderSettings.InstancePrefix + category.Name;
                try
                {
                    // --- Delete the whole category
                    if (PerformanceCounterCategory.Exists(categoryName))
                    {
                        PerformanceCounterCategory.Delete(categoryName);
                    }

                    // --- Add the whole category again
                    PerformanceCounterCategory.Create(
                        categoryName,
                        category.Help,
                        category.Type,
                        counterData);
                    categories.Add(category.Name);
                }
                catch (Exception ex)
                {
                    // --- Administer errors
                    errors[category.Name] = ex;
                }
            }
            return(new PmcInstallationResult(categories, errors));
        }