diff Implab/Diagnostics/Trace.cs @ 253:34df34841225 v3 v3.0.1-beta

Implab.Diagnostics drafts
author cin
date Mon, 12 Feb 2018 17:03:37 +0300
parents 6f4630d0bcd9
children b00441e04738
line wrap: on
line diff
--- a/Implab/Diagnostics/Trace.cs	Mon Feb 12 07:24:31 2018 +0300
+++ b/Implab/Diagnostics/Trace.cs	Mon Feb 12 17:03:37 2018 +0300
@@ -29,7 +29,7 @@
         /// <param name="name">Name.</param>
         [Conditional("TRACE")]
         public static void StartLogicalOperation(string name) {
-            Trace.CorrelationManager.StartLogicalOperation();
+            Trace.CorrelationManager.StartLogicalOperation(name);
         }
 
         /// <summary>
@@ -47,7 +47,7 @@
         /// <param name="arguments">Arguments.</param>
         [Conditional("TRACE")]
         public static void Log(string format, params object[] arguments) {
-            TraceSource.TraceEvent(TraceEventType.Information, 1, format, arguments);
+            TraceSource.TraceEvent(TraceEventType.Information, 0, format, arguments);
         }
 
         /// <summary>
@@ -57,17 +57,71 @@
         /// <param name="arguments">Arguments.</param>
         [Conditional("TRACE")]
         public static void Warn(string format, params object[] arguments) {
-            TraceSource.TraceEvent(TraceEventType.Warning, 1, format, arguments);
+            TraceSource.TraceEvent(TraceEventType.Warning, 0, format, arguments);
         }
 
         [Conditional("TRACE")]
         public static void Error(string format, params object[] arguments) {
-            TraceSource.TraceEvent(TraceEventType.Error, 1, format, arguments);
+            TraceSource.TraceEvent(TraceEventType.Error, 0, format, arguments);
         }
 
         [Conditional("TRACE")]
         public static void Error(Exception err) {
-            TraceSource.TraceData(TraceEventType.Error, 1, err);
+            TraceSource.TraceData(TraceEventType.Error, 0, err);
+        }
+
+        /// <summary>
+        /// This method save the current activity, and transfers to the specified activity,
+        /// emits <see cref="TraceEventType.Start"/> and returns a scope of the new
+        /// activity.
+        /// </summary>
+        /// <param name="activityName">The name of the new activity/</param>
+        /// <param name="activityId">The identifier of the activity to which 
+        /// the control will be transferred</param>
+        /// <returns>A scope of the new activity, dispose it to transfer
+        /// the control back to the original activity.</returns>
+        public static ActivityScope TransferActivity(string activityName, Guid activityId) {
+            var prev = Trace.CorrelationManager.ActivityId;
+
+            TraceSource.TraceTransfer(0, "Transfer", activityId);
+            Trace.CorrelationManager.ActivityId = activityId;
+            TraceSource.TraceEvent(TraceEventType.Start, 0, activityName);
+
+            return new ActivityScope(TraceSource, prev, 0, activityName);
+        }
+
+        /// <summary>
+        /// Emits <see cref="TraceEventType.Start"/> and returns a scope of the
+        /// activity.
+        /// </summary>
+        /// <param name="activityName">The name of the activity to start</param>
+        /// <returns>A scope of the new activity, dispose it to emit
+        /// <see cref="TraceEventType.Stop"/> for the current activity.</returns>
+        public static ActivityScope StartActivity(string activityName) {
+            if (Trace.CorrelationManager.ActivityId == Guid.Empty)
+                Trace.CorrelationManager.ActivityId = Guid.NewGuid();
+
+            var prev = Trace.CorrelationManager.ActivityId;
+            
+            TraceSource.TraceEvent(TraceEventType.Start, 0, activityName);
+            return new ActivityScope(TraceSource, prev, 0, activityName);
+        }
+
+        /// <summary>
+        /// Creates new <see cref="LogicalOperation(string)"/> and calls
+        /// to <see cref="CorrelationManager.StartLogicalOperation(object)"/>
+        /// passing the created operation as identity. Calls
+        /// <see cref="TraceSource.TraceData(TraceEventType, int, object)"/>
+        /// to notify listeners on operation start.
+        /// </summary>
+        /// <param name="name">The name of the logical operation.</param>
+        /// <returns>Logical operation scope, disposing it will stop
+        /// logical operation and notify trace listeners.</returns>
+        public static LogicalOperationScope LogicalOperation(string name) {
+            var operation = new LogicalOperation(name);
+            TraceSource.TraceData(TraceEventType.Information, TraceEventCodes.StartLogicalOperation, operation);
+            Trace.CorrelationManager.StartLogicalOperation(operation);
+            return new LogicalOperationScope(TraceSource, operation);
         }
     }
 }