private string GetProcessInstanceName(string categoryName, string counterName)
        {
#if MONO
            return(_pid.ToString());
#else
            try
            {
                if (PerformanceCounterCategory.Exists(categoryName))
                {
                    var category = new PerformanceCounterCategory(categoryName).ReadCategory();

                    if (category.Contains(counterName))
                    {
                        var instanceDataCollection = category[counterName];

                        foreach (InstanceData item in instanceDataCollection.Values)
                        {
                            int instancePid = (int)item.RawValue;
                            if (_pid.Equals(instancePid))
                            {
                                return(item.InstanceName);
                            }
                        }
                    }
                }
            }
            catch (InvalidOperationException)
            {
                _log.Trace("Unable to get performance counter category '{0}' instances.", categoryName);
            }

            return(null);
#endif
        }
Esempio n. 2
0
        private string GetProcessInstanceName(string categoryName, string counterName)
        {
            // On Unix or MacOS, use the PID as the instance name
            if (Runtime.IsUnixOrMac)
            {
                return(_pid.ToString());
            }

            // On Windows use the Performance Counter to get the name
            try
            {
                if (PerformanceCounterCategory.Exists(categoryName))
                {
                    var category = new PerformanceCounterCategory(categoryName).ReadCategory();

                    if (category.Contains(counterName))
                    {
                        var instanceDataCollection = category[counterName];

                        if (instanceDataCollection.Values != null)
                        {
                            foreach (InstanceData item in instanceDataCollection.Values)

                            {
                                var instancePid = (int)item.RawValue;
                                if (_pid.Equals(instancePid))
                                {
                                    return(item.InstanceName);
                                }
                            }
                        }
                    }
                }
            }
            catch (InvalidOperationException)
            {
                _log.Trace("Unable to get performance counter category '{category}' instances.", categoryName);
            }

            return(null);
        }