protected virtual void InitializePerformanceCounters()
        {
            try
            {
                _memoryCategory = new PerformanceCounterCategory(MemoryCategoryName);

                var instanceName = GetInstanceName();
                _fullInstanceName = instanceName.Item2;
                _instanceName     = instanceName.Item1;

                _gen0Size        = new PerformanceCounterWrapper(MemoryCategoryName, "Gen 0 heap size", _instanceName);
                _gen1Size        = new PerformanceCounterWrapper(MemoryCategoryName, "Gen 1 heap size", _instanceName);
                _gen2Size        = new PerformanceCounterWrapper(MemoryCategoryName, "Gen 2 heap size", _instanceName);
                _lohSize         = new PerformanceCounterWrapper(MemoryCategoryName, "Large Object Heap size", _instanceName);
                _contentionCount = new PerformanceCounterWrapper(ThreadingCategoryName, "Total # of Contentions", _instanceName);
            }
            catch (UnauthorizedAccessException ex) when(ex.Message.Contains("'Global'"))
            {
                // Catching error UnauthorizedAccessException: Access to the registry key 'Global' is denied.
                // The 'Global' part seems consistent across localizations

                Log.Error(ex, "The process does not have sufficient permissions to read performance counters. Please refer to https://dtdg.co/net-runtime-metrics to learn how to grant those permissions.");
                throw;
            }
            catch (Exception ex)
            {
                Log.Error(ex, "An error occured while initializing the performance counters");
                throw;
            }
        }
 private void InitializePerformanceCounters(string instanceName)
 {
     _gen0Size        = new PerformanceCounterWrapper(MemoryCategoryName, "Gen 0 heap size", instanceName);
     _gen1Size        = new PerformanceCounterWrapper(MemoryCategoryName, "Gen 1 heap size", instanceName);
     _gen2Size        = new PerformanceCounterWrapper(MemoryCategoryName, "Gen 2 heap size", instanceName);
     _lohSize         = new PerformanceCounterWrapper(MemoryCategoryName, "Large Object Heap size", instanceName);
     _contentionCount = new PerformanceCounterWrapper(ThreadingCategoryName, "Total # of Contentions", instanceName);
 }
        private void TryUpdateGauge(string path, PerformanceCounterWrapper counter)
        {
            var value = counter.GetValue(_instanceName);

            if (value != null)
            {
                _statsd.Gauge(path, value);
            }
        }
        private void TryUpdateCounter(string path, PerformanceCounterWrapper counter, ref long?lastValue)
        {
            var value = counter.GetValue(_instanceName);

            if (value == null)
            {
                return;
            }

            if (lastValue == null)
            {
                lastValue = value;
                return;
            }

            _statsd.Counter(path, value - lastValue);
            lastValue = value;
        }