public async Task InvokeAsync(HttpContext context) { TraceIdentifiersContext feature = TraceIdentifiersContext.Startup .CloneForThread(); string local = this.Options.LocalValueFactory(context); this.TryToWriteLocal(context, local); feature = feature.CreateChildWithLocal(this.Options.LocalIsShared(context), local); context.Features.Set(feature); ICollection <string> all = this.TryToReadRemote(context).ToArray(); if (all.Any()) { feature.CreateChildWithRemote(all); } try { using (feature) { await _next(context); } } finally { this.TryToWriteLocal(context, local); } }
public TraceIdentifiersContext CloneForThread() { Stack <KeyValuePair <string, bool> > stack = new Stack <KeyValuePair <string, bool> >(this.local.Reverse()); LinkedList <IEnumerable <string> > list = new LinkedList <IEnumerable <string> >(this.remoteShared.Select(enm => enm.ToArray()).ToArray()); TraceIdentifiersContext result = new TraceIdentifiersContext(stack, list, this.Remote); result.OnChildCreated = this.OnChildCreated; result.localBookmark = this.localBookmark; if (this.remoteBookmark != null) { LinkedListNode <IEnumerable <string> > nthis = this.remoteShared.First; LinkedListNode <IEnumerable <string> > nresult = result.remoteShared.First; while (this.remoteBookmark != nthis) { nthis = nthis.Next; nresult = nresult.Next; } result.remoteBookmark = nresult; } return(result); }
public async Task InvokeAsync(HttpContext context) { this.TryToWriteLocal(context); ICollection <string> all = this.TryToReadRemoteShared(context).ToArray(); TraceIdentifiersContext feature = TraceIdentifiersContext.StartupEmpty .CloneForThread(); feature.Remote = this.TryToReadRemoteSingle(context); feature = feature.CreateChildWithLocal(this.Options.ShareLocal, context.TraceIdentifier); using (feature) { if (all.Any()) { using (var withRemote = feature.CreateChildWithRemote(all)) { context.Features.Set(withRemote); await _next(context); } } else { context.Features.Set(feature); await _next(context); } } this.TryToWriteLocal(context); }
public TraceIdentifiersContext CreateChildWithLocal(bool shared = true, string local = null) { if (this.localBookmark == this.local.Peek().Key) { TraceIdentifiersContext result = new TraceIdentifiersContext(this.local, this.remoteShared, this.Remote, local, shared); result.OnChildCreated = this.OnChildCreated; 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) { if (values == null) { throw new ArgumentNullException(nameof(values)); } TraceIdentifiersContext result = new TraceIdentifiersContext(this.local, this.remoteShared, this.Remote); result.remoteBookmark = result.remoteShared.AddLast(values); result.localBookmark = this.localBookmark; result.OnChildCreated = this.OnChildCreated; this.OnChildCreated?.Invoke(result, EventArgs.Empty); return(result); }