/// <summary>
        ///     Create a new activity scope.
        /// </summary>
        /// <param name="activityId">
        ///     The current activity Id (if any).
        /// </param>
        /// <param name="previousActivityId">
        ///     The previous activity Id (if any).
        /// </param>
        internal ActivityScope(Guid?activityId = null, Guid?previousActivityId = null)
        {
            _activityId         = activityId;
            _previousActivityId = previousActivityId;

            if (_activityId.HasValue)
            {
                ActivityCorrelationManager.SetCurrentActivityId(_activityId.Value);
            }
            else
            {
                ActivityCorrelationManager.ClearCurrentActivityId();
            }

            ActivityCorrelationManager.SynchronizeEventSourceActivityIds();
        }
        /// <summary>
        ///     Dispose of resources being used by the object.
        /// </summary>
        protected override void Disposing()
        {
            // If the correlation manager does not have the expected activity Id, it's safer to not clean up.
            if (ActivityCorrelationManager.CurrentActivityId == _activityId)
            {
                // Restore previous activity Id (if any).
                if (_previousActivityId.HasValue)
                {
                    ActivityCorrelationManager.SetCurrentActivityId(_previousActivityId.Value);
                }
                else
                {
                    ActivityCorrelationManager.ClearCurrentActivityId();
                }

                ActivityCorrelationManager.SynchronizeEventSourceActivityIds();
            }
        }