Esempio n. 1
0
        /// <summary>
        ///     Removes the counter (category) from the system
        /// </summary>
        public static void Delete(string categoryName)
        {
            CheckValidCategory(categoryName);
            string machineName = ".";

            categoryName = categoryName.ToLowerInvariant();

            Mutex mutex = null;

            try
            {
                NetFrameworkUtils.EnterMutex(PerfMutexName, ref mutex);
                if (!PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.CantDeleteCategory);
                }

                SharedPerformanceCounter.RemoveAllInstances(categoryName);

                PerformanceCounterLib.UnregisterCategory(categoryName);
                PerformanceCounterLib.CloseAllLibraries();
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
        }
        public static void Delete(string categoryName)
        {
            CheckValidCategory(categoryName);
            string machineName = ".";

            new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Administer, machineName, categoryName).Demand();
            SharedUtils.CheckNtEnvironment();
            categoryName = categoryName.ToLower(CultureInfo.InvariantCulture);
            Mutex mutex = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                SharedUtils.EnterMutex("netfxperf.1.0", ref mutex);
                if (!PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.GetString("CantDeleteCategory"));
                }
                SharedPerformanceCounter.RemoveAllInstances(categoryName);
                PerformanceCounterLib.UnregisterCategory(categoryName);
                PerformanceCounterLib.CloseAllLibraries();
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
        }
        /// <devdoc>
        ///     Returns true if the category is registered in the machine.
        /// </devdoc>
        public static bool Exists(string categoryName, string machineName)
        {
            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));
            }

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

            permission.Demand();

            if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
            {
                return(true);
            }

            return(PerformanceCounterLib.CategoryExists(machineName, categoryName));
        }
Esempio n. 4
0
        private static void DeleteCategory(string categoryName, string machineName)
        {
            CheckValidCategory(categoryName);

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

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

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

            permission.Demand();

            SharedUtils.CheckNtEnvironment();

            Mutex mutex = SharedUtils.EnterMutex(perfMutexName);

            try {
                if (!PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.GetString(SR.CantDeleteCategory));
                }

                PerformanceCounterLib.UnregisterCategory(machineName, categoryName);
            }
            finally {
                mutex.ReleaseMutex();
                mutex.Close();
            }
        }
Esempio n. 5
0
        /// <include file='doc\PerformanceCounter.uex' path='docs/doc[@for="PerformanceCounter.Initialize"]/*' />
        /// <devdoc>
        ///     Intializes required resources
        /// </devdoc>
        private void Initialize()
        {
            if (!initialized && !DesignMode)
            {
                lock (this) {
                    if (!initialized)
                    {
                        if (this.categoryName == String.Empty)
                        {
                            throw new InvalidOperationException(SR.GetString(SR.CategoryNameMissing));
                        }
                        if (this.counterName == String.Empty)
                        {
                            throw new InvalidOperationException(SR.GetString(SR.CounterNameMissing));
                        }

                        if (this.ReadOnly)
                        {
                            PerformanceCounterPermission permission = new PerformanceCounterPermission(
                                PerformanceCounterPermissionAccess.Browse, this.machineName, this.categoryName);

                            permission.Demand();

                            if (!PerformanceCounterLib.CounterExists(machineName, categoryName, counterName))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.CounterExists, categoryName, counterName));
                            }

                            this.initialized = true;
                        }
                        else
                        {
                            PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Instrument, this.machineName, this.categoryName);
                            permission.Demand();

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

                            SharedUtils.CheckNtEnvironment();

                            if (!PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.NotCustomCounter));
                            }

                            this.sharedCounter = new SharedPerformanceCounter(categoryName.ToLower(CultureInfo.InvariantCulture), counterName.ToLower(CultureInfo.InvariantCulture), instanceName.ToLower(CultureInfo.InvariantCulture));
                            this.initialized   = true;
                        }
                    }
                }
            }
        }
        public static PerformanceCounterCategory Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData)
        {
            if (categoryType < PerformanceCounterCategoryType.Unknown || categoryType > PerformanceCounterCategoryType.MultiInstance)
            {
                throw new ArgumentOutOfRangeException("categoryType");
            }
            if (counterData == null)
            {
                throw new ArgumentNullException("counterData");
            }

            CheckValidCategory(categoryName);
            if (categoryHelp != null)
            {
                // null categoryHelp is a valid option - it gets set to "Help Not Available" later on.
                CheckValidHelp(categoryHelp);
            }
            string machineName = ".";

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

            permission.Demand();

            SharedUtils.CheckNtEnvironment();

            Mutex mutex = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                SharedUtils.EnterMutex(perfMutexName, ref mutex);
                if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName) || PerformanceCounterLib.CategoryExists(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.GetString(SR.PerformanceCategoryExists, categoryName));
                }

                CheckValidCounterLayout(counterData);
                PerformanceCounterLib.RegisterCategory(categoryName, categoryType, categoryHelp, counterData);
                return(new PerformanceCounterCategory(categoryName, machineName));
            }
            finally {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
        }
 public static bool Exists(string categoryName, string machineName)
 {
     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 }));
     }
     new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Browse, machineName, categoryName).Demand();
     return(PerformanceCounterLib.IsCustomCategory(machineName, categoryName) || PerformanceCounterLib.CategoryExists(machineName, categoryName));
 }
        public static PerformanceCounterCategory Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData)
        {
            PerformanceCounterCategory category;

            if ((categoryType < PerformanceCounterCategoryType.Unknown) || (categoryType > PerformanceCounterCategoryType.MultiInstance))
            {
                throw new ArgumentOutOfRangeException("categoryType");
            }
            if (counterData == null)
            {
                throw new ArgumentNullException("counterData");
            }
            CheckValidCategory(categoryName);
            if (categoryHelp != null)
            {
                CheckValidHelp(categoryHelp);
            }
            string machineName = ".";

            new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Administer, machineName, categoryName).Demand();
            SharedUtils.CheckNtEnvironment();
            Mutex mutex = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                SharedUtils.EnterMutex("netfxperf.1.0", ref mutex);
                if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName) || PerformanceCounterLib.CategoryExists(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.GetString("PerformanceCategoryExists", new object[] { categoryName }));
                }
                CheckValidCounterLayout(counterData);
                PerformanceCounterLib.RegisterCategory(categoryName, categoryType, categoryHelp, counterData);
                category = new PerformanceCounterCategory(categoryName, machineName);
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
            return(category);
        }
Esempio n. 9
0
        public static PerformanceCounterCategory Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData)
        {
            if (categoryType < PerformanceCounterCategoryType.Unknown || categoryType > PerformanceCounterCategoryType.MultiInstance)
            {
                throw new ArgumentOutOfRangeException(nameof(categoryType));
            }
            if (counterData == null)
            {
                throw new ArgumentNullException(nameof(counterData));
            }

            CheckValidCategory(categoryName);
            if (categoryHelp != null)
            {
                // null categoryHelp is a valid option - it gets set to "Help Not Available" later on.
                CheckValidHelp(categoryHelp);
            }
            string machineName = ".";

            Mutex mutex = null;

            try
            {
                NetFrameworkUtils.EnterMutex(PerfMutexName, ref mutex);
                if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName) || PerformanceCounterLib.CategoryExists(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.Format(SR.PerformanceCategoryExists, categoryName));
                }

                CheckValidCounterLayout(counterData);
                PerformanceCounterLib.RegisterCategory(categoryName, categoryType, categoryHelp, counterData);
                return(new PerformanceCounterCategory(categoryName, machineName));
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
        }
        /// <summary>
        ///     Returns true if the category is registered in the machine.
        /// </summary>
        public static bool Exists(string categoryName, string machineName)
        {
            ArgumentNullException.ThrowIfNull(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));
            }

            if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
            {
                return(true);
            }

            return(PerformanceCounterLib.CategoryExists(machineName, categoryName));
        }
Esempio n. 11
0
        private void InitializeImpl()
        {
            bool lockTaken = false;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                Monitor.Enter(this.InstanceLockObject, ref lockTaken);
                if (!this.initialized)
                {
                    string categoryName = this.categoryName;
                    string machineName  = this.machineName;
                    if (categoryName == string.Empty)
                    {
                        throw new InvalidOperationException(SR.GetString("CategoryNameMissing"));
                    }
                    if (this.counterName == string.Empty)
                    {
                        throw new InvalidOperationException(SR.GetString("CounterNameMissing"));
                    }
                    if (this.ReadOnly)
                    {
                        new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Browse, machineName, categoryName).Demand();
                        if (!PerformanceCounterLib.CounterExists(machineName, categoryName, this.counterName))
                        {
                            throw new InvalidOperationException(SR.GetString("CounterExists", new object[] { categoryName, this.counterName }));
                        }
                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(machineName, categoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (string.IsNullOrEmpty(this.instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString("MultiInstanceOnly", new object[] { categoryName }));
                            }
                        }
                        else if ((categoryType == PerformanceCounterCategoryType.SingleInstance) && !string.IsNullOrEmpty(this.instanceName))
                        {
                            throw new InvalidOperationException(SR.GetString("SingleInstanceOnly", new object[] { categoryName }));
                        }
                        if (this.instanceLifetime != PerformanceCounterInstanceLifetime.Global)
                        {
                            throw new InvalidOperationException(SR.GetString("InstanceLifetimeProcessonReadOnly"));
                        }
                        this.initialized = true;
                    }
                    else
                    {
                        new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Write, machineName, categoryName).Demand();
                        if ((machineName != ".") && (string.Compare(machineName, PerformanceCounterLib.ComputerName, StringComparison.OrdinalIgnoreCase) != 0))
                        {
                            throw new InvalidOperationException(SR.GetString("RemoteWriting"));
                        }
                        SharedUtils.CheckNtEnvironment();
                        if (!PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
                        {
                            throw new InvalidOperationException(SR.GetString("NotCustomCounter"));
                        }
                        PerformanceCounterCategoryType type2 = PerformanceCounterLib.GetCategoryType(machineName, categoryName);
                        if (type2 == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (string.IsNullOrEmpty(this.instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString("MultiInstanceOnly", new object[] { categoryName }));
                            }
                        }
                        else if ((type2 == PerformanceCounterCategoryType.SingleInstance) && !string.IsNullOrEmpty(this.instanceName))
                        {
                            throw new InvalidOperationException(SR.GetString("SingleInstanceOnly", new object[] { categoryName }));
                        }
                        if (string.IsNullOrEmpty(this.instanceName) && (this.InstanceLifetime == PerformanceCounterInstanceLifetime.Process))
                        {
                            throw new InvalidOperationException(SR.GetString("InstanceLifetimeProcessforSingleInstance"));
                        }
                        this.sharedCounter = new SharedPerformanceCounter(categoryName.ToLower(CultureInfo.InvariantCulture), this.counterName.ToLower(CultureInfo.InvariantCulture), this.instanceName.ToLower(CultureInfo.InvariantCulture), this.instanceLifetime);
                        this.initialized   = true;
                    }
                }
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(this.InstanceLockObject);
                }
            }
        }
Esempio n. 12
0
        /// <summary>
        ///     Intializes required resources
        /// </summary>
        private void InitializeImpl()
        {
            bool tookLock = false;

            try
            {
                Monitor.Enter(InstanceLockObject, ref tookLock);

                if (!_initialized)
                {
                    string currentCategoryName = _categoryName;
                    string currentMachineName  = _machineName;

                    if (currentCategoryName.Length == 0)
                    {
                        throw new InvalidOperationException(SR.CategoryNameMissing);
                    }
                    if (_counterName.Length == 0)
                    {
                        throw new InvalidOperationException(SR.CounterNameMissing);
                    }

                    if (ReadOnly)
                    {
                        if (!PerformanceCounterLib.CounterExists(currentMachineName, currentCategoryName, _counterName))
                        {
                            throw new InvalidOperationException(SR.Format(SR.CounterExists, currentCategoryName, _counterName));
                        }

                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.MultiInstanceOnly, currentCategoryName));
                            }
                        }
                        else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
                        {
                            if (!string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.SingleInstanceOnly, currentCategoryName));
                            }
                        }

                        if (_instanceLifetime != PerformanceCounterInstanceLifetime.Global)
                        {
                            throw new InvalidOperationException(SR.InstanceLifetimeProcessonReadOnly);
                        }

                        _initialized = true;
                    }
                    else
                    {
                        if (currentMachineName != "." && !string.Equals(currentMachineName, PerformanceCounterLib.ComputerName, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new InvalidOperationException(SR.RemoteWriting);
                        }

                        if (!PerformanceCounterLib.IsCustomCategory(currentMachineName, currentCategoryName))
                        {
                            throw new InvalidOperationException(SR.NotCustomCounter);
                        }

                        // check category type
                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.MultiInstanceOnly, currentCategoryName));
                            }
                        }
                        else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
                        {
                            if (!string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.SingleInstanceOnly, currentCategoryName));
                            }
                        }

                        if (string.IsNullOrEmpty(_instanceName) && InstanceLifetime == PerformanceCounterInstanceLifetime.Process)
                        {
                            throw new InvalidOperationException(SR.InstanceLifetimeProcessforSingleInstance);
                        }

                        _sharedCounter = new SharedPerformanceCounter(currentCategoryName.ToLowerInvariant(), _counterName.ToLowerInvariant(), _instanceName.ToLowerInvariant(), _instanceLifetime);
                        _initialized   = true;
                    }
                }
            }
            finally
            {
                if (tookLock)
                {
                    Monitor.Exit(InstanceLockObject);
                }
            }
        }
        /// <devdoc>
        ///     Intializes required resources
        /// </devdoc>
        //[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
        private void InitializeImpl()
        {
            bool tookLock = false;

            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                Monitor.Enter(InstanceLockObject, ref tookLock);

                if (!initialized)
                {
                    string currentCategoryName = categoryName;
                    string currentMachineName  = machineName;

                    if (currentCategoryName == String.Empty)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.CategoryNameMissing));
                    }
                    if (this.counterName == String.Empty)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.CounterNameMissing));
                    }

                    if (this.ReadOnly)
                    {
                        PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, currentMachineName, currentCategoryName);

                        permission.Demand();

                        if (!PerformanceCounterLib.CounterExists(currentMachineName, currentCategoryName, counterName))
                        {
                            throw new InvalidOperationException(SR.GetString(SR.CounterExists, currentCategoryName, counterName));
                        }

                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (String.IsNullOrEmpty(instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.MultiInstanceOnly, currentCategoryName));
                            }
                        }
                        else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
                        {
                            if (!String.IsNullOrEmpty(instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.SingleInstanceOnly, currentCategoryName));
                            }
                        }

                        if (instanceLifetime != PerformanceCounterInstanceLifetime.Global)
                        {
                            throw new InvalidOperationException(SR.GetString(SR.InstanceLifetimeProcessonReadOnly));
                        }

                        this.initialized = true;
                    }
                    else
                    {
                        PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Write, currentMachineName, currentCategoryName);
                        permission.Demand();

                        if (currentMachineName != "." && String.Compare(currentMachineName, PerformanceCounterLib.ComputerName, StringComparison.OrdinalIgnoreCase) != 0)
                        {
                            throw new InvalidOperationException(SR.GetString(SR.RemoteWriting));
                        }

                        SharedUtils.CheckNtEnvironment();

                        if (!PerformanceCounterLib.IsCustomCategory(currentMachineName, currentCategoryName))
                        {
                            throw new InvalidOperationException(SR.GetString(SR.NotCustomCounter));
                        }

                        // check category type
                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (String.IsNullOrEmpty(instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.MultiInstanceOnly, currentCategoryName));
                            }
                        }
                        else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
                        {
                            if (!String.IsNullOrEmpty(instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.SingleInstanceOnly, currentCategoryName));
                            }
                        }

                        if (String.IsNullOrEmpty(instanceName) && InstanceLifetime == PerformanceCounterInstanceLifetime.Process)
                        {
                            throw new InvalidOperationException(SR.GetString(SR.InstanceLifetimeProcessforSingleInstance));
                        }

                        this.sharedCounter = new SharedPerformanceCounter(currentCategoryName.ToLower(CultureInfo.InvariantCulture), counterName.ToLower(CultureInfo.InvariantCulture), instanceName.ToLower(CultureInfo.InvariantCulture), instanceLifetime);
                        this.initialized   = true;
                    }
                }
            } finally {
                if (tookLock)
                {
                    Monitor.Exit(InstanceLockObject);
                }
            }
        }
Esempio n. 14
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();
            }
        }