Skip to content

Instantly share code, notes, and snippets.

@i3arnon
Last active October 6, 2015 21:28
Show Gist options
  • Save i3arnon/fd0ed2d637c57c289511 to your computer and use it in GitHub Desktop.
Save i3arnon/fd0ed2d637c57c289511 to your computer and use it in GitHub Desktop.
Fixed LogicalFlow using an ImmutableStack
public static class LogicalFlow
{
private static ImmutableStack<Guid> LogicalStack
{
get
{
return CallContext.LogicalGetData("LogicalFlow") as ImmutableStack<Guid> ?? ImmutableStack.Create<Guid>();
}
set
{
CallContext.LogicalSetData("LogicalFlow", value);
}
}
public static Guid CurrentId
{
get
{
var logicalStack = LogicalStack;
return logicalStack.IsEmpty ? Guid.Empty : logicalStack.Peek();
}
}
public static IDisposable StartScope()
{
LogicalStack = LogicalStack.Push(Guid.NewGuid()); // Here's where the CallContext is copied using copy-on-write
return new Stopper();
}
private static void StopScope()
{
LogicalStack = LogicalStack.Pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment