示例#1
0
        internal static PerformanceCounterCategory Create(string categoryName, string categoryHelp, CounterCreationDataCollection counterData, string machineName, string localizedIniFilePath)
        {
            CheckValidCategory(categoryName);

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

            if (machineName != "." && String.Compare(machineName, PerformanceCounterLib.ComputerName, true, CultureInfo.InvariantCulture) != 0)
            {
                throw new NotSupportedException(SR.GetString(SR.RemoteCounterAdmin));
            }

            PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Administer, machineName, categoryName);

            permission.Demand();

            SharedUtils.CheckNtEnvironment();

            Mutex mutex = SharedUtils.EnterMutex(perfMutexName);

            try {
                if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName) || PerformanceCounterLib.CategoryExists(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.GetString(SR.PerformanceCategoryExists));
                }

                // Ensure that there are no duplicate counter names being created
                Hashtable h = new Hashtable();
                for (int i = 0; i < counterData.Count; i++)
                {
                    // Ensure that all counter help strings aren't null or empty
                    if (counterData[i].CounterName == null || counterData[i].CounterName.Length == 0)
                    {
                        throw new ArgumentException(SR.GetString(SR.InvalidCounterName));
                    }

                    int currentSampleType = (int)counterData[i].CounterType;
                    if ((currentSampleType == NativeMethods.PERF_AVERAGE_BULK) ||
                        (currentSampleType == NativeMethods.PERF_COUNTER_QUEUELEN_TYPE) ||
                        (currentSampleType == NativeMethods.PERF_COUNTER_LARGE_QUEUELEN_TYPE) ||
                        (currentSampleType == NativeMethods.PERF_100NSEC_MULTI_TIMER) ||
                        (currentSampleType == NativeMethods.PERF_100NSEC_MULTI_TIMER_INV) ||
                        (currentSampleType == NativeMethods.PERF_COUNTER_MULTI_TIMER) ||
                        (currentSampleType == NativeMethods.PERF_COUNTER_MULTI_TIMER_INV) ||
                        (currentSampleType == NativeMethods.PERF_RAW_FRACTION) ||
                        (currentSampleType == NativeMethods.PERF_SAMPLE_FRACTION) ||
                        (currentSampleType == NativeMethods.PERF_SAMPLE_COUNTER) ||
                        (currentSampleType == NativeMethods.PERF_AVERAGE_TIMER))
                    {
                        if (counterData.Count <= (i + 1))
                        {
                            throw new InvalidOperationException(SR.GetString(SR.CounterLayout));
                        }
                        else
                        {
                            currentSampleType = (int)counterData[i + 1].CounterType;


                            if (currentSampleType != NativeMethods.PERF_AVERAGE_BASE &&
                                currentSampleType != NativeMethods.PERF_COUNTER_MULTI_BASE &&
                                currentSampleType != NativeMethods.PERF_RAW_BASE &&
                                currentSampleType != NativeMethods.PERF_SAMPLE_BASE)
                            {
                                throw new InvalidOperationException(SR.GetString(SR.CounterLayout));
                            }
                        }
                    }
                    else if (currentSampleType == NativeMethods.PERF_AVERAGE_BASE ||
                             currentSampleType == NativeMethods.PERF_COUNTER_MULTI_BASE ||
                             currentSampleType == NativeMethods.PERF_RAW_BASE ||
                             currentSampleType == NativeMethods.PERF_SAMPLE_BASE)
                    {
                        if (i == 0)
                        {
                            throw new InvalidOperationException(SR.GetString(SR.CounterLayout));
                        }
                        else
                        {
                            currentSampleType = (int)counterData[i - 1].CounterType;

                            if (
                                (currentSampleType != NativeMethods.PERF_AVERAGE_BULK) &&
                                (currentSampleType != NativeMethods.PERF_COUNTER_QUEUELEN_TYPE) &&
                                (currentSampleType != NativeMethods.PERF_COUNTER_LARGE_QUEUELEN_TYPE) &&
                                (currentSampleType != NativeMethods.PERF_100NSEC_MULTI_TIMER) &&
                                (currentSampleType != NativeMethods.PERF_100NSEC_MULTI_TIMER_INV) &&
                                (currentSampleType != NativeMethods.PERF_COUNTER_MULTI_TIMER) &&
                                (currentSampleType != NativeMethods.PERF_COUNTER_MULTI_TIMER_INV) &&
                                (currentSampleType != NativeMethods.PERF_RAW_FRACTION) &&
                                (currentSampleType != NativeMethods.PERF_SAMPLE_FRACTION) &&
                                (currentSampleType != NativeMethods.PERF_SAMPLE_COUNTER) &&
                                (currentSampleType != NativeMethods.PERF_AVERAGE_TIMER))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.CounterLayout));
                            }
                        }
                    }

                    if (h.ContainsKey(counterData[i].CounterName))
                    {
                        throw new ArgumentException(SR.GetString(SR.DuplicateCounterName, counterData[i].CounterName));
                    }
                    else
                    {
                        h.Add(counterData[i].CounterName, String.Empty);

                        // Ensure that all counter help strings aren't null or empty
                        if (counterData[i].CounterHelp == null || counterData[i].CounterHelp.Length == 0)
                        {
                            counterData[i].CounterHelp = counterData[i].CounterName + " help";
                        }
                    }
                }

                if (localizedIniFilePath == null)
                {
                    PerformanceCounterLib.RegisterCategory(machineName, categoryName, categoryHelp, counterData);
                }
                else
                {
                    PerformanceCounterLib.RegisterCategory(machineName, categoryName, categoryHelp, counterData, localizedIniFilePath);
                }

                return(new PerformanceCounterCategory(categoryName, machineName));
            }
            finally {
                mutex.ReleaseMutex();
                mutex.Close();
            }
        }