/// <summary>
        ///     Writes a metric message to Application Insights.
        /// </summary>
        /// <param name="name">The metric name.</param>
        /// <param name="value">The metric value.</param>
        /// <param name="count">The count of metrics being logged (default = 1).</param>
        /// <param name="min">The minimum value of metrics being logged (default = value).</param>
        /// <param name="max">The maximum value of metrics being logged (default = value).</param>
        /// <param name="stdDev">The standard deviation of metrics being logged (default = 0).</param>
        /// <param name="timestamp">The UTC timestamp of the event (default = DateTime.UtcNow).</param>
        /// <returns><c>true</c> if successfully logged, <c>false</c> otherwise.</returns>
        public bool WriteMetric(
            string name,
            double value,
            double?count       = null,
            double?min         = null,
            double?max         = null,
            double?stdDev      = null,
            DateTime?timestamp = null)
        {
            if (!this.Log("Metric", this._disableMetricTracking, this._percentLoggedMetric))
            {
                return(true);
            }

            timestamp = timestamp ?? DateTime.UtcNow;

            var metric = new AiMetric(name, value, count, min, max, stdDev);

            var json = this.GetMetricJsonString(timestamp.Value, metric);

            if (this._enableDebug)
            {
                this._tracingService.Trace($"DEBUG: Application Insights JSON: {CreateJsonDataLog(json)}");
            }

            return(this.SendToAi(json));
        }
        private string GetMetricJsonString(DateTime timestamp, AiMetric aiMetric)
        {
            var logRequest = new AiLogRequest
            {
                Name = $"Microsoft.ApplicationInsights.{this._instrumentationKey}.Metric",
                Time = timestamp.ToString("O"),
                InstrumentationKey = this._instrumentationKey,
                Tags = new AiTags
                {
                    RoleInstance        = null,
                    OperationName       = "xrmEvent",
                    AuthenticatedUserId = this._authenticatedUserId
                },
                Data = new AiData
                {
                    BaseType = "MetricData",
                    BaseData = new AiBaseData
                    {
                        Metrics = new List <AiMetric> {
                            aiMetric
                        },
                        Properties = this.EventProperties
                    }
                }
            };

            logRequest.Tags.OperationId = this.EventProperties.OperationId;

            var json = SerializationHelper.SerializeObject <AiLogRequest>(logRequest);

            return(json);
        }
Example #3
0
        /// <summary>
        /// Writes a metric message to Application Insights.
        /// </summary>
        /// <param name="name">The metric name.</param>
        /// <param name="value">The metric value.</param>
        /// <param name="count">The count of metrics being logged (default = 1).</param>
        /// <param name="min">The minimum value of metrics being logged (default = value).</param>
        /// <param name="max">The maximum value of metrics being logged (default = value).</param>
        /// <param name="stdDev">The standard deviation of metrics being logged (default = 0).</param>
        /// <param name="timestamp">The UTC timestamp of the event (default = DateTime.UtcNow).</param>
        /// <returns><c>true</c> if successfully logged, <c>false</c> otherwise.</returns>
        public bool WriteMetric(string name, int value, int?count = null, int?min = null,
                                int?max = null, int?stdDev = null, DateTime?timestamp = null)
        {
            if (!Log("Metric", _disableMetricTracking, _percentLoggedMetric))
            {
                return(true);
            }

            timestamp = timestamp ?? DateTime.UtcNow;

            AiMetric metric = new AiMetric(name, value, count, min, max, stdDev);

            string json = GetMetricJsonString(timestamp.Value, metric);

            if (_enableDebug)
            {
                _tracingService.Trace($"DEBUG: Application Insights JSON: {CreateJsonDataLog(json)}");
            }

            return(SendToAi(json));
        }