comparison Implab/Diagnostics/Trace.cs @ 255:b00441e04738 v3

Adde workaround to the behaviour of the logical operations stack in conjuction with async/await methods
author cin
date Wed, 04 Apr 2018 15:38:48 +0300
parents 34df34841225
children f07be402ab02
comparison
equal deleted inserted replaced
254:12c00235b105 255:b00441e04738
1 using System; 1 using System;
2 using System.Collections.Generic; 2 using System.Collections.Generic;
3 using System.Diagnostics; 3 using System.Diagnostics;
4 using System.Linq; 4 using System.Linq;
5 using System.Text; 5 using System.Text;
6 using System.Threading;
6 using System.Threading.Tasks; 7 using System.Threading.Tasks;
7 8
8 namespace Implab.Diagnostics { 9 namespace Implab.Diagnostics {
9 public static class Trace<T> { 10 public static class Trace<T> {
10 11
11 readonly static TraceSource _traceSource = new TraceSource(typeof(T).Name); 12 public static TraceSource TraceSource { get; } = new TraceSource(typeof(T).Name);
12 13
13 public static TraceSource TraceSource { 14 #if NETFX_TRACE_BUG
14 get { return _traceSource; } 15 readonly static AsyncLocal<object> m_currentOperation = new AsyncLocal<object>();
15 } 16 #endif
16 17
17 /// <summary> 18 /// <summary>
18 /// Starts the logical operation nested to the current operation nested to the current one. 19 /// Starts the logical operation nested to the current operation nested to the current one.
19 /// </summary> 20 /// </summary>
20 [Conditional("TRACE")]
21 public static void StartLogicalOperation() { 21 public static void StartLogicalOperation() {
22 Trace.CorrelationManager.StartLogicalOperation(); 22 Trace.CorrelationManager.StartLogicalOperation();
23 23
24 } 24 }
25 25
26 /// <summary> 26 /// <summary>
27 /// Starts the logical operation with the specified name, this name is usefull in logs. 27 /// Starts the logical operation with the specified name, this name is usefull in logs.
28 /// </summary> 28 /// </summary>
29 /// <param name="name">Name.</param> 29 /// <param name="name">Name.</param>
30 [Conditional("TRACE")] 30 #if NETFX_TRACE_BUG
31 public static void StartLogicalOperation(string name) { 31 public static void StartLogicalOperation(object name) {
32 m_currentOperation.Value = name;
32 Trace.CorrelationManager.StartLogicalOperation(name); 33 Trace.CorrelationManager.StartLogicalOperation(name);
33 } 34 }
35 #else
36 public static void StartLogicalOperation(object name) {
37 Trace.CorrelationManager.StartLogicalOperation(name);
38 }
39 #endif
34 40
35 /// <summary> 41 /// <summary>
36 /// Ends the logical operation and restores the previous one. 42 /// Ends the logical operation and restores the previous one.
37 /// </summary> 43 /// </summary>
38 [Conditional("TRACE")]
39 public static void StopLogicalOperation() { 44 public static void StopLogicalOperation() {
40 Trace.CorrelationManager.StopLogicalOperation(); 45 Trace.CorrelationManager.StopLogicalOperation();
41 } 46 }
42 47
43 /// <summary> 48 /// <summary>
118 /// <returns>Logical operation scope, disposing it will stop 123 /// <returns>Logical operation scope, disposing it will stop
119 /// logical operation and notify trace listeners.</returns> 124 /// logical operation and notify trace listeners.</returns>
120 public static LogicalOperationScope LogicalOperation(string name) { 125 public static LogicalOperationScope LogicalOperation(string name) {
121 var operation = new LogicalOperation(name); 126 var operation = new LogicalOperation(name);
122 TraceSource.TraceData(TraceEventType.Information, TraceEventCodes.StartLogicalOperation, operation); 127 TraceSource.TraceData(TraceEventType.Information, TraceEventCodes.StartLogicalOperation, operation);
123 Trace.CorrelationManager.StartLogicalOperation(operation); 128 StartLogicalOperation(operation);
124 return new LogicalOperationScope(TraceSource, operation); 129 return new LogicalOperationScope(TraceSource, operation);
125 } 130 }
126 } 131 }
127 } 132 }