Skip to content

Commit

Permalink
Fix activity correlator to continue using same GUID for current threa…
Browse files Browse the repository at this point in the history
…d activity (dotnet#1997)
  • Loading branch information
cheenamalhotra authored and kant2002 committed Jun 29, 2023
1 parent 6449dd3 commit 92089bf
Showing 1 changed file with 6 additions and 23 deletions.
Expand Up @@ -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.
Expand All @@ -40,27 +38,12 @@ public override string ToString()
/// <summary>
/// Get the current ActivityId
/// </summary>
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);

/// <summary>
/// Increment the sequence number and generate the new ActivityId
/// </summary>
/// <returns>ActivityId</returns>
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);
}
}

0 comments on commit 92089bf

Please sign in to comment.