Esempio n. 1
0
        // Will cause an update, raw value
        /// <summary>
        ///     Obtains a counter sample and returns the raw value for it.
        /// </summary>
        public CounterSample NextSample()
        {
            string currentCategoryName = _categoryName;
            string currentMachineName  = _machineName;

            Initialize();


            using (CategorySample categorySample = PerformanceCounterLib.GetCategorySample(currentMachineName, currentCategoryName))
            {
                CounterDefinitionSample counterSample = categorySample.GetCounterDefinitionSample(_counterName);
                _counterType = counterSample._counterType;
                if (!categorySample._isMultiInstance)
                {
                    if (_instanceName != null && _instanceName.Length != 0)
                    {
                        throw new InvalidOperationException(SR.Format(SR.InstanceNameProhibited, _instanceName));
                    }

                    return(counterSample.GetSingleValue());
                }
                else
                {
                    if (_instanceName == null || _instanceName.Length == 0)
                    {
                        throw new InvalidOperationException(SR.InstanceNameRequired);
                    }

                    return(counterSample.GetInstanceValue(_instanceName));
                }
            }
        }
Esempio n. 2
0
        public CounterSample NextSample()
        {
            string categoryName = this.categoryName;
            string machineName  = this.machineName;

            new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Browse, machineName, categoryName).Demand();
            this.Initialize();
            CategorySample          categorySample          = PerformanceCounterLib.GetCategorySample(machineName, categoryName);
            CounterDefinitionSample counterDefinitionSample = categorySample.GetCounterDefinitionSample(this.counterName);

            this.counterType = counterDefinitionSample.CounterType;
            if (!categorySample.IsMultiInstance)
            {
                if ((this.instanceName != null) && (this.instanceName.Length != 0))
                {
                    throw new InvalidOperationException(SR.GetString("InstanceNameProhibited", new object[] { this.instanceName }));
                }
                return(counterDefinitionSample.GetSingleValue());
            }
            if ((this.instanceName == null) || (this.instanceName.Length == 0))
            {
                throw new InvalidOperationException(SR.GetString("InstanceNameRequired"));
            }
            return(counterDefinitionSample.GetInstanceValue(this.instanceName));
        }
        // Will cause an update, raw value
        /// <devdoc>
        ///     Obtains a counter sample and returns the raw value for it.
        /// </devdoc>
        public CounterSample NextSample()
        {
            string currentCategoryName = categoryName;
            string currentMachineName  = machineName;

            PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, currentMachineName, currentCategoryName);

            permission.Demand();

            Initialize();
            CategorySample          categorySample = PerformanceCounterLib.GetCategorySample(currentMachineName, currentCategoryName);
            CounterDefinitionSample counterSample  = categorySample.GetCounterDefinitionSample(this.counterName);

            this.counterType = counterSample.CounterType;
            if (!categorySample.IsMultiInstance)
            {
                if (instanceName != null && instanceName.Length != 0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.InstanceNameProhibited, this.instanceName));
                }

                return(counterSample.GetSingleValue());
            }
            else
            {
                if (instanceName == null || instanceName.Length == 0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.InstanceNameRequired));
                }

                return(counterSample.GetInstanceValue(this.instanceName));
            }
        }
Esempio n. 4
0
        // Will cause an update, raw value
        /// <include file='doc\PerformanceCounter.uex' path='docs/doc[@for="PerformanceCounter.NextSample"]/*' />
        /// <devdoc>
        ///     Obtains a counter sample and returns the raw value for it.
        /// </devdoc>
        public CounterSample NextSample()
        {
            Initialize();
            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(this.machineName, this.categoryName);

            try {
                CounterDefinitionSample counterSample = categorySample.GetCounterDefinitionSample(this.counterName);
                this.counterType = counterSample.CounterType;
                if (counterSample.IsSingleInstance)
                {
                    if (instanceName != null && instanceName.Length != 0)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.InstanceNameProhibited, this.instanceName));
                    }

                    return(counterSample.GetSingleValue());
                }
                else
                {
                    if (instanceName == null || instanceName.Length == 0)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.InstanceNameRequired));
                    }

                    return(counterSample.GetInstanceValue(this.instanceName));
                }
            }
            finally {
                categorySample.Dispose();
            }
        }
 public InstanceDataCollectionCollection ReadCategory()
 {
     if (this.categoryName == null)
     {
         throw new InvalidOperationException(SR.GetString("CategoryNameNotSet"));
     }
     return(PerformanceCounterLib.GetCategorySample(this.machineName, this.categoryName).ReadCategory());
 }
Esempio n. 6
0
        /// <summary>
        ///     Reads all the counter and instance data of this performance category.  Note that reading the entire category
        ///     at once can be as efficient as reading a single counter because of the way the system provides the data.
        /// </summary>
        public InstanceDataCollectionCollection ReadCategory()
        {
            if (_categoryName == null)
            {
                throw new InvalidOperationException(SR.Format(SR.CategoryNameNotSet));
            }

            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(_machineName, _categoryName);

            return(categorySample.ReadCategory());
        }
 public bool InstanceExists(string instanceName)
 {
     if (instanceName == null)
     {
         throw new ArgumentNullException("instanceName");
     }
     if (this.categoryName == null)
     {
         throw new InvalidOperationException(SR.GetString("CategoryNameNotSet"));
     }
     return(PerformanceCounterLib.GetCategorySample(this.machineName, this.categoryName).InstanceNameTable.ContainsKey(instanceName));
 }
        /// <summary>
        ///     Returns true if the instance already exists for this category.
        /// </summary>
        public bool InstanceExists(string instanceName)
        {
            ArgumentNullException.ThrowIfNull(instanceName);

            if (_categoryName == null)
            {
                throw new InvalidOperationException(SR.CategoryNameNotSet);
            }

            using (CategorySample categorySample = PerformanceCounterLib.GetCategorySample(_machineName, _categoryName))
            {
                return(categorySample._instanceNameTable.ContainsKey(instanceName));
            }
        }
Esempio n. 9
0
        /// <summary>
        ///     Returns true if the instance already exists for this category.
        /// </summary>
        public bool InstanceExists(string instanceName)
        {
            if (instanceName == null)
            {
                throw new ArgumentNullException(nameof(instanceName));
            }

            if (_categoryName == null)
            {
                throw new InvalidOperationException(SR.Format(SR.CategoryNameNotSet));
            }

            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(_machineName, _categoryName);

            return(categorySample._instanceNameTable.ContainsKey(instanceName));
        }
Esempio n. 10
0
        /// <include file='doc\PerformanceCounterCategory.uex' path='docs/doc[@for="PerformanceCounterCategory.ReadCategory"]/*' />
        /// <devdoc>
        ///     Reads all the counter and instance data of this performance category.  Note that reading the entire category
        ///     at once can be as efficient as reading a single counter because of the way the system provides the data.
        /// </devdoc>
        public InstanceDataCollectionCollection ReadCategory()
        {
            if (this.categoryName == null)
            {
                throw new InvalidOperationException(SR.GetString(SR.CategoryNameNotSet));
            }

            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(this.machineName, this.categoryName);

            try {
                return(categorySample.ReadCategory());
            }
            finally {
                categorySample.Dispose();
            }
        }
        internal static string[] GetCounterInstances(string categoryName, string machineName)
        {
            new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Browse, machineName, categoryName).Demand();
            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(machineName, categoryName);

            if (categorySample.InstanceNameTable.Count == 0)
            {
                return(new string[0]);
            }
            string[] array = new string[categorySample.InstanceNameTable.Count];
            categorySample.InstanceNameTable.Keys.CopyTo(array, 0);
            if ((array.Length == 1) && (array[0].CompareTo("systemdiagnosticsperfcounterlibsingleinstance") == 0))
            {
                return(new string[0]);
            }
            return(array);
        }
Esempio n. 12
0
        /// <summary>
        ///     Returns the instance names for a given category
        /// </summary>
        /// <internalonly/>
        internal static string[] GetCounterInstances(string categoryName, string machineName)
        {
            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(machineName, categoryName);

            if (categorySample._instanceNameTable.Count == 0)
            {
                return(Array.Empty <string>());
            }

            string[] instanceNames = new string[categorySample._instanceNameTable.Count];
            categorySample._instanceNameTable.Keys.CopyTo(instanceNames, 0);
            if (instanceNames.Length == 1 && instanceNames[0] == PerformanceCounterLib.SingleInstanceName)
            {
                return(Array.Empty <string>());
            }

            return(instanceNames);
        }
Esempio n. 13
0
        /// <include file='doc\PerformanceCounterCategory.uex' path='docs/doc[@for="PerformanceCounterCategory.GetCounterInstances"]/*' />
        /// <devdoc>
        ///     Returns the instance names for a given category
        /// </devdoc>
        /// <internalonly/>
        internal static string[] GetCounterInstances(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.Browse, machineName, categoryName);

            permission.Demand();

            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(machineName, categoryName);

            try {
                if (categorySample.InstanceNameTable.Count == 0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.NoInstanceInformation, categoryName));
                }

                string[] instanceNames = new string[categorySample.InstanceNameTable.Count];
                categorySample.InstanceNameTable.Keys.CopyTo(instanceNames, 0);
                if (instanceNames.Length == 1 && instanceNames[0].CompareTo(PerformanceCounterLib.SingleInstanceName) == 0)
                {
                    return(new string[0]);
                }

                return(instanceNames);
            }
            finally {
                categorySample.Dispose();
            }
        }
        /// <devdoc>
        ///     Returns the instance names for a given category
        /// </devdoc>
        /// <internalonly/>
        internal static string[] GetCounterInstances(string categoryName, string machineName)
        {
            PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, machineName, categoryName);

            permission.Demand();

            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(machineName, categoryName);

            if (categorySample.InstanceNameTable.Count == 0)
            {
                return(new string[0]);
            }

            string[] instanceNames = new string[categorySample.InstanceNameTable.Count];
            categorySample.InstanceNameTable.Keys.CopyTo(instanceNames, 0);
            if (instanceNames.Length == 1 && instanceNames[0].CompareTo(PerformanceCounterLib.SingleInstanceName) == 0)
            {
                return(new string[0]);
            }

            return(instanceNames);
        }
Esempio n. 15
0
        /// <include file='doc\PerformanceCounterCategory.uex' path='docs/doc[@for="PerformanceCounterCategory.InstanceExists"]/*' />
        /// <devdoc>
        ///     Returns true if the instance already exists for this category.
        /// </devdoc>
        public bool InstanceExists(string instanceName)
        {
            if (instanceName == null)
            {
                throw new ArgumentNullException("instanceName");
            }

            if (this.categoryName == null)
            {
                throw new InvalidOperationException(SR.GetString(SR.CategoryNameNotSet));
            }

            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(machineName, categoryName);

            try {
                IEnumerator valueEnum = categorySample.CounterTable.Values.GetEnumerator();
                return(categorySample.InstanceNameTable.ContainsKey(instanceName));
            }
            finally {
                categorySample.Dispose();
            }
        }