/
CorrelationManager.cs
139 lines (114 loc) · 4.14 KB
/
CorrelationManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
namespace System.Diagnostics
{
public class CorrelationManager
{
private readonly AsyncLocal<Guid> _activityId = new AsyncLocal<Guid>();
private readonly AsyncLocal<StackNode?> _stack = new AsyncLocal<StackNode?>();
private readonly AsyncLocalStackWrapper _stackWrapper;
internal CorrelationManager()
{
_stackWrapper = new AsyncLocalStackWrapper(_stack);
}
public Stack LogicalOperationStack => _stackWrapper;
public void StartLogicalOperation() => StartLogicalOperation(Guid.NewGuid());
public void StopLogicalOperation() => _stackWrapper.Pop();
public Guid ActivityId { get { return _activityId.Value; } set { _activityId.Value = value; } }
public void StartLogicalOperation(object operationId)
{
ArgumentNullException.ThrowIfNull(operationId);
_stackWrapper.Push(operationId);
}
private sealed class StackNode
{
internal StackNode(object? value, StackNode? prev = null)
{
Value = value;
Prev = prev;
Count = prev != null ? prev.Count + 1 : 1;
}
internal int Count { get; }
internal object? Value { get; }
internal StackNode? Prev { get; }
}
private sealed class AsyncLocalStackWrapper : Stack
{
private readonly AsyncLocal<StackNode?> _stack;
internal AsyncLocalStackWrapper(AsyncLocal<StackNode?> stack)
{
Debug.Assert(stack != null);
_stack = stack;
}
public override void Clear() => _stack.Value = null;
public override object Clone() => new AsyncLocalStackWrapper(_stack);
public override int Count => _stack.Value?.Count ?? 0;
public override IEnumerator GetEnumerator() => GetEnumerator(_stack.Value);
public override object? Peek() => _stack.Value?.Value;
public override bool Contains(object? obj)
{
for (StackNode? n = _stack.Value; n != null; n = n.Prev)
{
if (obj == null)
{
if (n.Value == null) return true;
}
else if (obj.Equals(n.Value))
{
return true;
}
}
return false;
}
public override void CopyTo(Array array, int index)
{
for (StackNode? n = _stack.Value; n != null; n = n.Prev)
{
array.SetValue(n.Value, index++);
}
}
private static IEnumerator GetEnumerator(StackNode? n)
{
while (n != null)
{
yield return n.Value;
n = n.Prev;
}
}
public override object? Pop()
{
StackNode? n = _stack.Value;
if (n == null)
{
base.Pop(); // used to throw proper exception
}
_stack.Value = n!.Prev;
return n.Value;
}
public override void Push(object? obj)
{
_stack.Value = new StackNode(obj, _stack.Value);
}
public override object?[] ToArray()
{
StackNode? n = _stack.Value;
if (n == null)
{
return Array.Empty<object>();
}
var results = new List<object?>();
do
{
results.Add(n.Value);
n = n.Prev;
}
while (n != null);
return results.ToArray();
}
}
}
}