public Activity StartNewActivity(string activityName, ActivityLogLevel logLevel)
        {
            activityName = ValidateActivityName(activityName);

            string parentOperationId, globalOperationId;

            TryGetDistributedTracingContext(out parentOperationId, out globalOperationId);

            LogicalExecutionStack currentLogicalStack = GetOrCreateCurrentLogicalStack();

            lock (currentLogicalStack)
            {
                Activity parent = currentLogicalStack.Peek(); // may be null

                var activity = new Activity(
                    activityName,
                    logLevel,
                    Util.CreateRandomId(),
                    parent?.RootActivity,
                    parent,
                    parentOperationId,
                    globalOperationId,
                    currentLogicalStack);

                currentLogicalStack.Push(activity);

                return(activity);
            }
        }
        private Activity PeekCurrentActivityToUseProperties()
        {
            LogicalExecutionStack logicalStack = _logicalExecutionThread.Value;

            if (logicalStack == null)
            {
                throw new InvalidOperationException(ExceptionMsg_CannotAccessActivityToUseProperties);
            }

            lock (logicalStack)
            {
                Activity activity = logicalStack.Peek();
                return(activity);
            }
        }
        public Activity StartNewLogicalActivityThread(string activityName, ActivityLogLevel logLevel)
        {
            activityName = ValidateActivityName(activityName);

            string parentOperationId, globalOperationId;

            TryGetDistributedTracingContext(out parentOperationId, out globalOperationId);

            LogicalExecutionStack currentLogicalStack = _logicalExecutionThread.Value;

            object lockScope = currentLogicalStack;

            lockScope = lockScope ?? _logicalExecutionThread;

            lock (lockScope)
            {
                Activity parent = currentLogicalStack?.Peek();  // may be null

                var newLogicalStack = new LogicalExecutionStack(currentLogicalStack);

                var activity = new Activity(
                    activityName,
                    logLevel,
                    Util.CreateRandomId(),
                    parent?.RootActivity,
                    parent,
                    parentOperationId,
                    globalOperationId,
                    newLogicalStack);

                newLogicalStack.Push(activity);

                _logicalExecutionThread.Value = newLogicalStack;

                return(activity);
            }
        }