Initial commit
[mono.git] / mcs / class / referencesource / System / compmod / system / diagnostics / CorrelationManager.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="CorrelationManager.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 using System;
8 using System.Collections;
9 using System.Collections.Specialized;
10 using System.Threading;
11 using System.Runtime.Remoting.Messaging;
12
13
14 namespace System.Diagnostics {
15     public class CorrelationManager {
16         private const string transactionSlotName = "System.Diagnostics.Trace.CorrelationManagerSlot";
17         private const string activityIdSlotName = "E2ETrace.ActivityID";
18         
19         internal CorrelationManager() { }
20
21         public Guid ActivityId {
22             get {
23                 Object id = CallContext.LogicalGetData(activityIdSlotName);
24                 if (id != null)
25                     return (Guid) id;
26                 else
27                     return Guid.Empty;
28             }
29             set {
30                 CallContext.LogicalSetData(activityIdSlotName, value);
31             }
32         }
33
34         public Stack LogicalOperationStack {
35             get {
36                 return GetLogicalOperationStack();
37             }
38         }
39         
40         public void StartLogicalOperation(object operationId) {
41             if (operationId == null)
42                 throw new ArgumentNullException("operationId");
43
44             Stack idStack = GetLogicalOperationStack();
45             idStack.Push(operationId);
46         }
47
48         public void StartLogicalOperation() {
49             StartLogicalOperation(Guid.NewGuid());
50         }
51
52         public void StopLogicalOperation() {
53             Stack idStack = GetLogicalOperationStack();
54             idStack.Pop();
55         }
56
57         private Stack GetLogicalOperationStack() {
58             Stack idStack = CallContext.LogicalGetData(transactionSlotName) as Stack;
59             if (idStack == null) {
60                 idStack = new Stack();
61                 CallContext.LogicalSetData(transactionSlotName, idStack);
62             }
63
64             return idStack;
65         }
66
67     }
68 }