public TraceIdentifiersContext CloneForThread()
        {
            Stack <KeyValuePair <string, bool> > stack = new Stack <KeyValuePair <string, bool> >(this.local.Reverse());
            LinkedList <KeyValuePair <IEnumerable <string>, bool> > list = new LinkedList <KeyValuePair <IEnumerable <string>, bool> >(this.remote.Select(enm => enm).ToArray());

            TraceIdentifiersContext result = new TraceIdentifiersContext(stack, list);

            result.localBookmark     = this.localBookmark;
            result.OnClonedForThread = this.OnClonedForThread;

            if (this.remoteBookmark != null)
            {
                LinkedListNode <KeyValuePair <IEnumerable <string>, bool> > nthis   = this.remote.First;
                LinkedListNode <KeyValuePair <IEnumerable <string>, bool> > nresult = result.remote.First;

                while (this.remoteBookmark != nthis)
                {
                    nthis   = nthis.Next;
                    nresult = nresult.Next;
                }

                result.remoteBookmark = nresult;
            }

            this.OnClonedForThread?.Invoke(result, EventArgs.Empty);

            return(result);
        }
 public static bool TryAddLocalSharedAndRemoteShared(
     this HttpRequestMessage message,
     TraceIdentifiersContext context,
     SendIdentifiersOptions options)
 {
     return(message.Headers.TryAddWithoutValidation(options.HeaderName,
                                                    Normalize(context.LocalShared.Reverse().Concat(context.RemoteShared), options)));
 }
        public TraceIdentifiersContext CreateChildWithLocal(bool shared = true, string local = null)
        {
            if (!this.local.Any() || this.localBookmark == this.local.Peek().Key)
            {
                TraceIdentifiersContext result = new TraceIdentifiersContext(this.local, this.remote, local, shared);

                result.OnChildCreated    = this.OnChildCreated;
                result.OnClonedForThread = this.OnClonedForThread;
                this.OnChildCreated?.Invoke(result, EventArgs.Empty);
                return(result);
            }

            throw new InvalidOperationException("Unable to create child context, because previous context with same nested level not disposed");
        }
        public TraceIdentifiersContext CreateChildWithRemote(IEnumerable <string> values, bool shared = true)
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }
            TraceIdentifiersContext result = new TraceIdentifiersContext(this.local, this.remote);

            result.remoteBookmark    = result.remote.AddLast(new KeyValuePair <IEnumerable <string>, bool>(values, shared));
            result.localBookmark     = this.localBookmark;
            result.OnChildCreated    = this.OnChildCreated;
            result.OnClonedForThread = this.OnClonedForThread;
            this.OnChildCreated?.Invoke(result, EventArgs.Empty);
            return(result);
        }
 public static TraceIdentifiersContext CreateChildWithRemote(this TraceIdentifiersContext context, string value, bool shared = true)
 {
     return(context.CreateChildWithRemote(Enumerable.Repeat(value, 1), shared));
 }