The Labs \ Source Viewer \ SSCLI \ System.Diagnostics \ CorrelationManager

  1. //------------------------------------------------------------------------------
  2. // <copyright file="CorrelationManager.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Specialized;
  18. using System.Threading;
  19. using System.Runtime.Remoting.Messaging;
  20. namespace System.Diagnostics
  21. {
  22.     public class CorrelationManager
  23.     {
  24.         private const string transactionSlotName = "System.Diagnostics.Trace.CorrelationManagerSlot";
  25.         private const string activityIdSlotName = "E2ETrace.ActivityID";
  26.        
  27.         internal CorrelationManager()
  28.         {
  29.         }
  30.        
  31.         public Guid ActivityId {
  32.             get {
  33.                 object id = CallContext.LogicalGetData(activityIdSlotName);
  34.                 if (id != null)
  35.                     return (Guid)id;
  36.                 else
  37.                     return Guid.Empty;
  38.             }
  39.             set { CallContext.LogicalSetData(activityIdSlotName, value); }
  40.         }
  41.        
  42.         public Stack LogicalOperationStack {
  43.             get { return GetLogicalOperationStack(); }
  44.         }
  45.        
  46.         public void StartLogicalOperation(object operationId)
  47.         {
  48.             if (operationId == null)
  49.                 throw new ArgumentNullException("operationId");
  50.            
  51.             Stack idStack = GetLogicalOperationStack();
  52.             idStack.Push(operationId);
  53.         }
  54.        
  55.         public void StartLogicalOperation()
  56.         {
  57.             StartLogicalOperation(Guid.NewGuid());
  58.         }
  59.        
  60.         public void StopLogicalOperation()
  61.         {
  62.             Stack idStack = GetLogicalOperationStack();
  63.             idStack.Pop();
  64.         }
  65.        
  66.         private Stack GetLogicalOperationStack()
  67.         {
  68.             Stack idStack = CallContext.LogicalGetData(transactionSlotName) as Stack;
  69.             if (idStack == null) {
  70.                 idStack = new Stack();
  71.                 CallContext.LogicalSetData(transactionSlotName, idStack);
  72.             }
  73.            
  74.             return idStack;
  75.         }
  76.        
  77.     }
  78. }

Developer Fusion