示例#1
0
        /// <summary>
        /// Computes the duration and tracks the respective telemetry item on dispose.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !this.isDisposed)
            {
                // We need to compare the operation id and name of telemetry with operation id and name of current call context before tracking it
                // to make sure that the customer is tracking the right telemetry.
                lock (this)
                {
                    if (!this.isDisposed)
                    {
                        var operationTelemetry = this.Telemetry;
                        operationTelemetry.Stop();

                        bool isActivityAvailable = false;
                        isActivityAvailable = ActivityExtensions.TryRun(() =>
                        {
                            var currentActivity = Activity.Current;
                            if (currentActivity == null || operationTelemetry.Id != currentActivity.Id)
                            {
                                // this is for internal error reporting
                                CoreEventSource.Log.InvalidOperationToStopError();

                                // this are details with unique ids for debugging
                                CoreEventSource.Log.InvalidOperationToStopDetails(
                                    string.Format(
                                        CultureInfo.InvariantCulture,
                                        "Telemetry Id '{0}' does not match current Activity '{1}'",
                                        operationTelemetry.Id,
                                        currentActivity?.Id));

                                return;
                            }

                            this.telemetryClient.Track(operationTelemetry);

                            currentActivity.Stop();
                        });

                        if (!isActivityAvailable)
                        {
                            var currentOperationContext = CallContextHelpers.GetCurrentOperationContext();
                            if (currentOperationContext == null || operationTelemetry.Id != currentOperationContext.ParentOperationId)
                            {
                                // this is for internal error reporting
                                CoreEventSource.Log.InvalidOperationToStopError();

                                // this are details with unique ids for debugging
                                CoreEventSource.Log.InvalidOperationToStopDetails(
                                    string.Format(
                                        CultureInfo.InvariantCulture,
                                        "Telemetry Id '{0}' does not match current Activity '{1}'",
                                        operationTelemetry.Id,
                                        currentOperationContext?.ParentOperationId));
                                return;
                            }

                            this.telemetryClient.Track(operationTelemetry);

                            CallContextHelpers.RestoreOperationContext(this.ParentContext);
                        }
                    }

                    this.isDisposed = true;
                }
            }
        }
        /// <summary>
        /// Computes the duration and tracks the respective telemetry item on dispose.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !this.isDisposed)
            {
                // We need to compare the operation id and name of telemetry with operation id and name of current call context before tracking it
                // to make sure that the customer is tracking the right telemetry.
                lock (this)
                {
                    if (!this.isDisposed)
                    {
                        var operationTelemetry = this.Telemetry;
                        operationTelemetry.Stop();

                        bool isActivityAvailable = false;
                        isActivityAvailable = ActivityExtensions.TryRun(() =>
                        {
                            var currentActivity = Activity.Current;
                            if (currentActivity == null ||
                                (Activity.DefaultIdFormat != ActivityIdFormat.W3C && operationTelemetry.Id != currentActivity.Id) ||
                                (Activity.DefaultIdFormat == ActivityIdFormat.W3C && operationTelemetry.Id != W3CUtilities.FormatTelemetryId(currentActivity.TraceId.ToHexString(), currentActivity.SpanId.ToHexString())))
                            {
                                // W3COperationCorrelationTelemetryInitializer changes Id
                                // but keeps an original one in 'ai_legacyRequestId' property

                                if (!operationTelemetry.Properties.TryGetValue("ai_legacyRequestId", out var legacyId) ||
                                    legacyId != currentActivity?.Id)
                                {
                                    // this is for internal error reporting
                                    CoreEventSource.Log.InvalidOperationToStopError();

                                    // this are details with unique ids for debugging
                                    CoreEventSource.Log.InvalidOperationToStopDetails(
                                        string.Format(
                                            CultureInfo.InvariantCulture,
                                            "Telemetry Id '{0}' does not match current Activity '{1}'",
                                            operationTelemetry.Id,
                                            currentActivity?.Id));

                                    return;
                                }
                            }

                            this.telemetryClient.Track(operationTelemetry);

                            currentActivity?.Stop();

                            if (this.originalActivity != null &&
                                Activity.Current != this.originalActivity &&
                                this.originalActivity is Activity activity)
                            {
                                Activity.Current = activity;
                            }
                        });

                        if (!isActivityAvailable)
                        {
                            var currentOperationContext = CallContextHelpers.GetCurrentOperationContext();
                            if (currentOperationContext == null || operationTelemetry.Id != currentOperationContext.ParentOperationId)
                            {
                                // this is for internal error reporting
                                CoreEventSource.Log.InvalidOperationToStopError();

                                // this are details with unique ids for debugging
                                CoreEventSource.Log.InvalidOperationToStopDetails(
                                    string.Format(
                                        CultureInfo.InvariantCulture,
                                        "Telemetry Id '{0}' does not match current Activity '{1}'",
                                        operationTelemetry.Id,
                                        currentOperationContext?.ParentOperationId));
                                return;
                            }

                            this.telemetryClient.Track(operationTelemetry);

                            CallContextHelpers.RestoreOperationContext(this.ParentContext);
                        }
                    }

                    this.isDisposed = true;
                }
            }
        }