Skip to content

Commit 3fd439b

Browse files
Merged PR 4037: [5.1.2] | Fix activity correlator to continue using same GUID for current thread activity (#1997)
Ports [#1997](#1997)
1 parent 7ad5c58 commit 3fd439b

File tree

1 file changed

+6
-23
lines changed

1 file changed

+6
-23
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/ActivityCorrelator.cs

+6-23
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@ internal sealed class ActivityId
1919
internal readonly Guid Id;
2020
internal readonly uint Sequence;
2121

22-
internal ActivityId(uint sequence)
22+
internal ActivityId(Guid? currentActivityId, uint sequence = 1)
2323
{
24-
this.Id = Guid.NewGuid();
25-
this.Sequence = sequence;
24+
Id = currentActivityId ?? Guid.NewGuid();
25+
Sequence = sequence;
2626
}
2727

2828
public override string ToString()
29-
{
30-
return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.Id, this.Sequence);
31-
}
29+
=> string.Format(CultureInfo.InvariantCulture, "{0}:{1}", Id, Sequence);
3230
}
3331

3432
// Declare the ActivityId which will be stored in TLS. The Id is unique for each thread.
@@ -40,27 +38,12 @@ public override string ToString()
4038
/// <summary>
4139
/// Get the current ActivityId
4240
/// </summary>
43-
internal static ActivityId Current
44-
{
45-
get
46-
{
47-
if (t_tlsActivity == null)
48-
{
49-
t_tlsActivity = new ActivityId(1);
50-
}
51-
return t_tlsActivity;
52-
}
53-
}
41+
internal static ActivityId Current => t_tlsActivity ??= new ActivityId(null);
5442

5543
/// <summary>
5644
/// Increment the sequence number and generate the new ActivityId
5745
/// </summary>
5846
/// <returns>ActivityId</returns>
59-
internal static ActivityId Next()
60-
{
61-
t_tlsActivity = new ActivityId( (t_tlsActivity?.Sequence ?? 0) + 1);
62-
63-
return t_tlsActivity;
64-
}
47+
internal static ActivityId Next() => t_tlsActivity = new ActivityId(t_tlsActivity?.Id, (t_tlsActivity?.Sequence ?? 0) + 1);
6548
}
6649
}

0 commit comments

Comments
 (0)