private static void updateUsedMemoryCounter(PdhCounterPathElement pathElement)
        {
            MEMORYSTATUSEX memStatus = new MEMORYSTATUSEX();
            if (Interop.Interop.GlobalMemoryStatusEx(memStatus))
            {
                ulong installedMemory = memStatus.ullTotalPhys / 1024 / 1024;
                PerformanceCounter usedMemCounter = pathElement.InstanceName == null ?
                   new PerformanceCounter(pathElement.ObjectName, pathElement.CounterName, true) :
                   new PerformanceCounter(pathElement.ObjectName, pathElement.CounterName, pathElement.InstanceName, true);

                var itr = _handler.GetPathElements(new List<string>() { "\\Memory\\Available MBytes" });
                var pe = itr.ElementAt(0);
                PerformanceCounter availMem = pathElement.InstanceName == null ?
                   new PerformanceCounter(pe.ObjectName, pe.CounterName, true) :
                   new PerformanceCounter(pe.ObjectName, pe.CounterName, pe.InstanceName, true);

                usedMemCounter.ReadOnly = false;
                usedMemCounter.RawValue = (long)(installedMemory - Convert.ToUInt64(availMem.NextValue()));
                usedMemCounter.Close();
            }
        }
        private MetricInfo RegisterMetric(PdhCounterPathElement pathElement, IDictionary<String, MetricInfo> existingMetrics)
        {
            string contextName = CleanName(pathElement.ObjectName);
            string metricName = CleanName(pathElement.CounterName);
            string instanceName = CleanName(pathElement.InstanceName);

            MetricTags tags = default(MetricTags);
            if (instanceName != null)
            {
                tags = "instance=" + instanceName;
            }
            string keyName = MetricInfo.TagName(contextName + metricName, tags);
            MetricInfo mInfo;
            if (existingMetrics.TryGetValue(keyName, out mInfo))
            {
                existingMetrics.Remove(keyName);
                return mInfo;
            }

            PerfCounterGauge pcGauge = new PerfCounterGauge(pathElement.ObjectName, pathElement.CounterName, pathElement.InstanceName);
            TimerMetric timer = null;
            PerformanceCounterType type = pcGauge.performanceCounter.CounterType;
            switch (type)
            {
                //these types of counters are not usable
                case PerformanceCounterType.AverageBase:
                case PerformanceCounterType.CounterMultiBase:
                case PerformanceCounterType.RawBase:
                case PerformanceCounterType.SampleBase:
                    _log.Error(String.Format("Don't know how to handle metric of type {0} for {1}", type.ToString(), metricName));
                    return null;

                //timers
                case PerformanceCounterType.AverageTimer32:
                case PerformanceCounterType.ElapsedTime:
                    timer = new TimerMetric(SamplingType.FavourRecent);
                    break;
            }
            mInfo = new MetricInfo(contextName, metricName, tags, timer, pcGauge);
            if (mInfo.Timer != null)
            {
                _timers[keyName] = mInfo;
            }
            return mInfo;
        }