示例#1
0
        /// <summary>
        ///     Returns true if the instance already exists for this category and machine specified.
        /// </summary>
        public static bool InstanceExists(string instanceName, string categoryName, string machineName)
        {
            if (instanceName == null)
            {
                throw new ArgumentNullException(nameof(instanceName));
            }

            if (categoryName == null)
            {
                throw new ArgumentNullException(nameof(categoryName));
            }

            if (categoryName.Length == 0)
            {
                throw new ArgumentException(SR.Format(SR.InvalidParameter, nameof(categoryName), categoryName), nameof(categoryName));
            }

            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.Format(SR.InvalidParameter, nameof(machineName), machineName), nameof(machineName));
            }

            PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName, machineName);

            return(category.InstanceExists(instanceName));
        }
        public static bool InstanceExists(string instanceName, string categoryName, string machineName)
        {
            if (instanceName == null)
            {
                throw new ArgumentNullException("instanceName");
            }
            if (categoryName == null)
            {
                throw new ArgumentNullException("categoryName");
            }
            if (categoryName.Length == 0)
            {
                throw new ArgumentException(SR.GetString("InvalidParameter", new object[] { "categoryName", categoryName }));
            }
            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString("InvalidParameter", new object[] { "machineName", machineName }));
            }
            PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName, machineName);

            return(category.InstanceExists(instanceName));
        }
示例#3
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;
        }
示例#4
0
        private void UniquifyInstanceName(ref string instanceName)
        {
            // Through experimentation it has been learned that performance counter instances
            // with names greater than 64 characters use a shared counter.  We have not
            // investigated how to use a shared counter, so limit to 64 characters for now
            //
            // We trim to 60 in order to allow space to uniquify the name _NNN 
            // (up to 1000 instances of the same name)
            if (instanceName.Length > 60)
            {
//                string msg = string.Format("Trying to create performance counter instance " +
//                    "greater than 60 characters - {0}, length: {1}.  Instance will be " +
//                    "trimmed to function correctly.", instanceName, instanceName.Length);
//
//                EventLog.WriteEntry("Rtp", msg, EventLogEntryType.Warning, (int)RtpEL.ID.PerformanceCounterTooLong);

                instanceName = instanceName.Substring(0, 60);
            }

            // Check to see if the instanceName already exists and choose a different one
            PerformanceCounterCategory pcc = new PerformanceCounterCategory(categoryName);

            if(pcc.InstanceExists(instanceName))
            {
                int i = 0;
                while(pcc.InstanceExists(instanceName + "_" + (++i)));

                instanceName += "_" + i;
            }        
        }
        /// <devdoc>
        ///     Returns true if the instance already exists for this category and machine specified.
        /// </devdoc>
        public static bool InstanceExists(string instanceName, string categoryName, string machineName) {
            if (instanceName == null)
                throw new ArgumentNullException("instanceName");

            if (categoryName == null)
                throw new ArgumentNullException("categoryName");

            if (categoryName.Length == 0)
                throw new ArgumentException(SR.GetString(SR.InvalidParameter, "categoryName", categoryName));

            if (!SyntaxCheck.CheckMachineName(machineName))
                throw new ArgumentException(SR.GetString(SR.InvalidParameter, "machineName", machineName));

            PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName, machineName);
            return category.InstanceExists(instanceName);
        }
        public DataTable EnumeratePerformanceCounters()
        {
            DataTable dt = new DataTable();
            DataRow dr = null;

            // define the table's schema
            dt.Columns.Add(new DataColumn("PerfCategoryName", typeof(string)));
            dt.Columns.Add(new DataColumn("PerfCounterName", typeof(string)));
            dt.Columns.Add(new DataColumn("PerfInstanceName", typeof(string)));
            dt.Columns.Add(new DataColumn("PerfCategoryDesc", typeof(string)));

            PerformanceCounterCategory[] perfCounterCats = PerformanceCounterCategory.GetCategories();
            PerformanceCounterCategory perfCounterCat = null;
            PerformanceCounter[] perfCounters = null;

            // PerformanceCounterCategoryType - MultiInstance, SingleInstance, Unknown
            string catName = string.Empty;
            string catDesc = string.Empty;
            string counterName = string.Empty;
            string[] instanceNames = null;

            for (int count = 0; count < perfCounterCats.Length; count++)
            {
                catName = perfCounterCats[count].CategoryName;
                catDesc = perfCounterCats[count].CategoryHelp;

                perfCounterCat = new PerformanceCounterCategory(catName);

                instanceNames = perfCounterCat.GetInstanceNames();

                if (instanceNames.Length > 0)
                {
                    foreach (string instanceName in instanceNames)
                    {
                        try
                        {
                            if (perfCounterCat.InstanceExists(instanceName))
                            {
                                perfCounters = perfCounterCat.GetCounters(instanceName);

                                foreach (PerformanceCounter perfCounter in perfCounters)
                                {
                                    counterName = perfCounter.CounterName;

                                    dr = dt.NewRow();
                                    dr[0] = catName;
                                    dr[1] = counterName;
                                    dr[2] = instanceName;
                                    dr[3] = catDesc;
                                    dt.Rows.Add(dr);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Trace.Write(ex.Message + ex.StackTrace, TRACE_CATEGORY);
                        }
                    }
                }
                else
                {
                    foreach (PerformanceCounter perfCounter in perfCounterCat.GetCounters())
                    {
                        counterName = perfCounter.CounterName;

                        dr = dt.NewRow();
                        dr[0] = catName;
                        dr[1] = counterName;
                        dr[2] = BCCUIHelper.Constants.SC303_INSTANCE_NOT_FOUND;
                        dr[3] = catDesc;
                        dt.Rows.Add(dr);
                    }
                }
            }

            return dt;
        }