Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data / System / Data / Common / ActivityCorrelator.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="ActivityCorrelator.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">[....]</owner>
6 //------------------------------------------------------------------------------
7
8 namespace System.Data.Common
9 {
10     using System;
11     using System.Data;
12     using System.Threading;
13     using System.Diagnostics;
14     using System.Globalization;
15     
16     /// <summary>
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.
19     /// </summary>
20  
21     internal static class ActivityCorrelator
22     {
23         internal const Bid.ApiGroup CorrelationTracePoints = Bid.ApiGroup.Correlation;
24
25         internal class ActivityId
26         {
27             internal Guid Id { get; private set; }
28             internal UInt32 Sequence { get; private set; }
29
30             internal ActivityId()
31             {
32                 this.Id = Guid.NewGuid();
33                 this.Sequence = 0; // the first event will start 1
34             }
35
36             // copy-constructor
37             internal ActivityId(ActivityId activity)
38             {
39                 this.Id = activity.Id;
40                 this.Sequence = activity.Sequence;
41             }
42
43             internal void Increment()
44             {
45                 unchecked
46                 {
47                     ++this.Sequence;
48                 }
49             }
50
51             public override string ToString()
52             {
53                 return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.Id, this.Sequence);
54             }
55         }
56
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;
62
63         /// <summary>
64         /// Get the current ActivityId
65         /// </summary>
66         internal static ActivityId Current
67         {
68             get
69             {
70                 if (tlsActivity == null)
71                 {
72                     tlsActivity = new ActivityId();
73                 }
74
75                 return new ActivityId(tlsActivity);
76             }
77         }
78
79         /// <summary>
80         /// Increment the sequence number and generate the new ActviityId
81         /// </summary>
82         /// <returns>ActivityId</returns>
83         internal static ActivityId Next()   
84         {
85             if (tlsActivity == null)
86             {
87                 tlsActivity = new ActivityId();
88             }
89
90             tlsActivity.Increment();
91
92             return new ActivityId(tlsActivity);
93         }
94     }
95 }