From 34401b388ec4930e1462a4078ab11a1a27ab5aaf Mon Sep 17 00:00:00 2001 From: Davoud Eshtehari Date: Sat, 14 Oct 2023 04:13:09 +0000 Subject: [PATCH] Merged PR 4061: [4.0.4] | Fix activity correlator to continue using same GUID for current thread activity (#1997) Ports [#1997](https://github.com/dotnet/SqlClient/pull/1997) --- .../Data/Common/ActivityCorrelator.cs | 29 ++++--------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/ActivityCorrelator.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/ActivityCorrelator.cs index ef6b9b6cd2..5823921abc 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/ActivityCorrelator.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/ActivityCorrelator.cs @@ -19,16 +19,14 @@ internal sealed class ActivityId internal readonly Guid Id; internal readonly uint Sequence; - internal ActivityId(uint sequence) + internal ActivityId(Guid? currentActivityId, uint sequence = 1) { - this.Id = Guid.NewGuid(); - this.Sequence = sequence; + Id = currentActivityId ?? Guid.NewGuid(); + Sequence = sequence; } public override string ToString() - { - return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.Id, this.Sequence); - } + => string.Format(CultureInfo.InvariantCulture, "{0}:{1}", Id, Sequence); } // Declare the ActivityId which will be stored in TLS. The Id is unique for each thread. @@ -40,27 +38,12 @@ public override string ToString() /// /// Get the current ActivityId /// - internal static ActivityId Current - { - get - { - if (t_tlsActivity == null) - { - t_tlsActivity = new ActivityId(1); - } - return t_tlsActivity; - } - } + internal static ActivityId Current => t_tlsActivity ??= new ActivityId(null); /// /// Increment the sequence number and generate the new ActivityId /// /// ActivityId - internal static ActivityId Next() - { - t_tlsActivity = new ActivityId( (t_tlsActivity?.Sequence ?? 0) + 1); - - return t_tlsActivity; - } + internal static ActivityId Next() => t_tlsActivity = new ActivityId(t_tlsActivity?.Id, (t_tlsActivity?.Sequence ?? 0) + 1); } }