private void LogFunctionResultAggregate()

in src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ApplicationInsightsLogger.cs [326:377]


        private void LogFunctionResultAggregate(IEnumerable<KeyValuePair<string, object>> values, LogLevel logLevel, EventId eventId)
        {
            // Metric names will be created like "{FunctionName} {MetricName}"
            IDictionary<string, double> metrics = new Dictionary<string, double>(7);
            string functionName = LoggingConstants.Unknown;

            // build up the collection of metrics to send
            foreach (KeyValuePair<string, object> value in values)
            {
                switch (value.Key)
                {
                    case LogConstants.NameKey when value.Value is string name:
                        functionName = name;
                        break;
                    case LogConstants.TimestampKey:
                    case LogConstants.OriginalFormatKey:
                        // Timestamp is created automatically
                        // We won't use the format string here
                        break;
                    default:
                        if (value.Value is int intValue)
                        {
                            metrics.Add(value.Key, Convert.ToDouble(intValue));
                        }
                        else if (value.Value is double doubleValue)
                        {
                            metrics.Add(value.Key, doubleValue);
                        }
                        else if (value.Value is TimeSpan timeSpan)
                        {
                            // if it's a TimeSpan, log the milliseconds
                            metrics.Add(value.Key, timeSpan.TotalMilliseconds);
                        }
                        // do nothing otherwise
                        break;
                }
            }

            IDictionary<string, string> properties = null;

            // Do not apply state properties if optimization is enabled
            if (!_loggerOptions.EnableMetricsCustomDimensionOptimization)
            {
                properties = new Dictionary<string, string>(2);
                ApplyKnownProperties(properties, logLevel, eventId);
            }

            foreach (KeyValuePair<string, double> metric in metrics)
            {
                _telemetryClient.TrackMetric($"{functionName} {metric.Key}", metric.Value, properties);
            }
        }