private void WriteMetric(string name, UpdateMode mode = UpdateMode.Increment, double?value = null)
        {
            if (!isInitialized.Value)
            {
                return;
            }

            PerfCounterConfigData cd = GetCounter(name);

            if (cd == null || cd.PerfCounter == null)
            {
                return;
            }

            StatisticName statsName       = cd.Name;
            string        perfCounterName = GetPerfCounterName(cd);

            try
            {
                if (logger.IsVerbose3)
                {
                    logger.Verbose3(ErrorCode.PerfCounterWriting, "Writing perf counter {0}", perfCounterName);
                }

                switch (mode)
                {
                case UpdateMode.Increment:
                    if (value.HasValue)
                    {
                        cd.PerfCounter.IncrementBy((long)value.Value);
                    }
                    else
                    {
                        cd.PerfCounter.Increment();
                    }
                    break;

                case UpdateMode.Decrement:
                    if (value.HasValue)
                    {
                        cd.PerfCounter.RawValue = cd.PerfCounter.RawValue - (long)value.Value;
                    }
                    else
                    {
                        cd.PerfCounter.Decrement();
                    }
                    break;

                case UpdateMode.Set:
                    cd.PerfCounter.RawValue = (long)value.Value;
                    break;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ErrorCode.PerfCounterUnableToWrite, string.Format("Unable to write to Windows perf counter '{0}'", statsName), ex);
            }
        }
 private static string GetPerfCounterName(PerfCounterConfigData cd)
 {
     return(cd.Name.Name + "." + (cd.UseDeltaValue ? "Delta" : "Current"));
 }
        private void WriteMetric(string name, UpdateMode mode = UpdateMode.Increment, double?value = null)
        {
            if (!this.isInitialized.Value)
            {
                return;
            }

            // Attempt to initialize grain-specific counters if they haven't been initialized yet.
            if (!this.initializedGrainCounters)
            {
                this.Initialize();
            }

            PerfCounterConfigData cd = GetCounter(name);

            if (cd == null || cd.PerfCounter == null)
            {
                return;
            }

            StatisticName statsName       = cd.Name;
            string        perfCounterName = GetPerfCounterName(cd);

            try
            {
                if (this.logger.IsEnabled(LogLevel.Trace))
                {
                    this.logger.Trace(ErrorCode.PerfCounterWriting, "Writing perf counter {0}", perfCounterName);
                }

                switch (mode)
                {
                case UpdateMode.Increment:
                    if (value.HasValue)
                    {
                        cd.PerfCounter.IncrementBy((long)value.Value);
                    }
                    else
                    {
                        cd.PerfCounter.Increment();
                    }
                    break;

                case UpdateMode.Decrement:
                    if (value.HasValue)
                    {
                        cd.PerfCounter.RawValue = cd.PerfCounter.RawValue - (long)value.Value;
                    }
                    else
                    {
                        cd.PerfCounter.Decrement();
                    }
                    break;

                case UpdateMode.Set:
                    cd.PerfCounter.RawValue = (long)value.Value;
                    break;
                }
            }
            catch (Exception ex)
            {
                this.logger.Error(ErrorCode.PerfCounterUnableToWrite, string.Format("Unable to write to Windows perf counter '{0}'", statsName), ex);
            }
        }
 private static string GetPerfCounterName(PerfCounterConfigData cd)
 {
     return cd.Name.Name + "." + (cd.UseDeltaValue ? "Delta" : "Current");
 }