private static Metric GetOrCreateMetric( TelemetryClient telemetryClient, MetricAggregationScope aggregationScope, string metricId, string dimension1Name, string dimension2Name, IMetricConfiguration metricConfiguration) { Util.ValidateNotNull(telemetryClient, nameof(telemetryClient)); MetricManager metricManager = telemetryClient.Metrics(aggregationScope); MetricsCache cache = metricManager.GetOrCreateExtensionState(MetricsCache.CreateNewInstance); if (cache == null) { throw new InvalidOperationException($"telemetryConfiguration.Metrics().GetOrCreateExtensionState(..) unexpectedly returned null." + $" This indicates that multiple extensions attempt to use" + $" the \"Cache\" extension point of the {nameof(MetricManager)} in a conflicting manner."); } Metric metric = cache.GetOrCreateMetric(metricId, dimension1Name, dimension2Name, metricConfiguration); return(metric); }