1 //------------------------------------------------------------------------------
2 // <copyright file="ActivityCorrelator.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">[....]</owner>
6 //------------------------------------------------------------------------------
8 namespace System.Data.Common
12 using System.Threading;
13 using System.Diagnostics;
14 using System.Globalization;
17 /// This class defines the data strucutre for ActvitiyId used for correlated tracing between client (bid trace event) and server (XEvent).
18 /// It also includes all the APIs used to access the ActivityId. Note: ActivityId is thread based which is stored in TLS.
21 internal static class ActivityCorrelator
23 internal const Bid.ApiGroup CorrelationTracePoints = Bid.ApiGroup.Correlation;
25 internal class ActivityId
27 internal Guid Id { get; private set; }
28 internal UInt32 Sequence { get; private set; }
32 this.Id = Guid.NewGuid();
33 this.Sequence = 0; // the first event will start 1
37 internal ActivityId(ActivityId activity)
39 this.Id = activity.Id;
40 this.Sequence = activity.Sequence;
43 internal void Increment()
51 public override string ToString()
53 return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.Id, this.Sequence);
57 // Declare the ActivityId which will be stored in TLS. The Id is unique for each thread.
58 // The Sequence number will be incremented when each event happens.
59 // Correlation along threads is consistent with the current XEvent mechanisam at server.
60 [ThreadStaticAttribute]
61 static ActivityId tlsActivity;
64 /// Get the current ActivityId
66 internal static ActivityId Current
70 if (tlsActivity == null)
72 tlsActivity = new ActivityId();
75 return new ActivityId(tlsActivity);
80 /// Increment the sequence number and generate the new ActviityId
82 /// <returns>ActivityId</returns>
83 internal static ActivityId Next()
85 if (tlsActivity == null)
87 tlsActivity = new ActivityId();
90 tlsActivity.Increment();
92 return new ActivityId(tlsActivity);