Mercurial > pub > ImplabNet
annotate Implab/Diagnostics/Trace.cs @ 260:547a2fc0d93e v3 v3.0.6
minor fixes
author | cin |
---|---|
date | Fri, 13 Apr 2018 19:14:59 +0300 |
parents | b00441e04738 |
children | f07be402ab02 |
rev | line source |
---|---|
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
1 using System; |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
2 using System.Collections.Generic; |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
3 using System.Diagnostics; |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
4 using System.Linq; |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
5 using System.Text; |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
6 using System.Threading; |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
7 using System.Threading.Tasks; |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
8 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
9 namespace Implab.Diagnostics { |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
10 public static class Trace<T> { |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
11 |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
12 public static TraceSource TraceSource { get; } = new TraceSource(typeof(T).Name); |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
13 |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
14 #if NETFX_TRACE_BUG |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
15 readonly static AsyncLocal<object> m_currentOperation = new AsyncLocal<object>(); |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
16 #endif |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
17 |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
18 /// <summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
19 /// Starts the logical operation nested to the current operation nested to the current one. |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
20 /// </summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
21 public static void StartLogicalOperation() { |
252 | 22 Trace.CorrelationManager.StartLogicalOperation(); |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
23 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
24 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
25 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
26 /// <summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
27 /// Starts the logical operation with the specified name, this name is usefull in logs. |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
28 /// </summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
29 /// <param name="name">Name.</param> |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
30 #if NETFX_TRACE_BUG |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
31 public static void StartLogicalOperation(object name) { |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
32 m_currentOperation.Value = name; |
253 | 33 Trace.CorrelationManager.StartLogicalOperation(name); |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
34 } |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
35 #else |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
36 public static void StartLogicalOperation(object name) { |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
37 Trace.CorrelationManager.StartLogicalOperation(name); |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
38 } |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
39 #endif |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
40 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
41 /// <summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
42 /// Ends the logical operation and restores the previous one. |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
43 /// </summary> |
252 | 44 public static void StopLogicalOperation() { |
45 Trace.CorrelationManager.StopLogicalOperation(); | |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
46 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
47 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
48 /// <summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
49 /// Writes an informational message. |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
50 /// </summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
51 /// <param name="format">Format.</param> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
52 /// <param name="arguments">Arguments.</param> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
53 [Conditional("TRACE")] |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
54 public static void Log(string format, params object[] arguments) { |
253 | 55 TraceSource.TraceEvent(TraceEventType.Information, 0, format, arguments); |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
56 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
57 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
58 /// <summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
59 /// Writes a warning message. |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
60 /// </summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
61 /// <param name="format">Format.</param> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
62 /// <param name="arguments">Arguments.</param> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
63 [Conditional("TRACE")] |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
64 public static void Warn(string format, params object[] arguments) { |
253 | 65 TraceSource.TraceEvent(TraceEventType.Warning, 0, format, arguments); |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
66 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
67 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
68 [Conditional("TRACE")] |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
69 public static void Error(string format, params object[] arguments) { |
253 | 70 TraceSource.TraceEvent(TraceEventType.Error, 0, format, arguments); |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
71 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
72 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
73 [Conditional("TRACE")] |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
74 public static void Error(Exception err) { |
253 | 75 TraceSource.TraceData(TraceEventType.Error, 0, err); |
76 } | |
77 | |
78 /// <summary> | |
79 /// This method save the current activity, and transfers to the specified activity, | |
80 /// emits <see cref="TraceEventType.Start"/> and returns a scope of the new | |
81 /// activity. | |
82 /// </summary> | |
83 /// <param name="activityName">The name of the new activity/</param> | |
84 /// <param name="activityId">The identifier of the activity to which | |
85 /// the control will be transferred</param> | |
86 /// <returns>A scope of the new activity, dispose it to transfer | |
87 /// the control back to the original activity.</returns> | |
88 public static ActivityScope TransferActivity(string activityName, Guid activityId) { | |
89 var prev = Trace.CorrelationManager.ActivityId; | |
90 | |
91 TraceSource.TraceTransfer(0, "Transfer", activityId); | |
92 Trace.CorrelationManager.ActivityId = activityId; | |
93 TraceSource.TraceEvent(TraceEventType.Start, 0, activityName); | |
94 | |
95 return new ActivityScope(TraceSource, prev, 0, activityName); | |
96 } | |
97 | |
98 /// <summary> | |
99 /// Emits <see cref="TraceEventType.Start"/> and returns a scope of the | |
100 /// activity. | |
101 /// </summary> | |
102 /// <param name="activityName">The name of the activity to start</param> | |
103 /// <returns>A scope of the new activity, dispose it to emit | |
104 /// <see cref="TraceEventType.Stop"/> for the current activity.</returns> | |
105 public static ActivityScope StartActivity(string activityName) { | |
106 if (Trace.CorrelationManager.ActivityId == Guid.Empty) | |
107 Trace.CorrelationManager.ActivityId = Guid.NewGuid(); | |
108 | |
109 var prev = Trace.CorrelationManager.ActivityId; | |
110 | |
111 TraceSource.TraceEvent(TraceEventType.Start, 0, activityName); | |
112 return new ActivityScope(TraceSource, prev, 0, activityName); | |
113 } | |
114 | |
115 /// <summary> | |
116 /// Creates new <see cref="LogicalOperation(string)"/> and calls | |
117 /// to <see cref="CorrelationManager.StartLogicalOperation(object)"/> | |
118 /// passing the created operation as identity. Calls | |
119 /// <see cref="TraceSource.TraceData(TraceEventType, int, object)"/> | |
120 /// to notify listeners on operation start. | |
121 /// </summary> | |
122 /// <param name="name">The name of the logical operation.</param> | |
123 /// <returns>Logical operation scope, disposing it will stop | |
124 /// logical operation and notify trace listeners.</returns> | |
125 public static LogicalOperationScope LogicalOperation(string name) { | |
126 var operation = new LogicalOperation(name); | |
127 TraceSource.TraceData(TraceEventType.Information, TraceEventCodes.StartLogicalOperation, operation); | |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
128 StartLogicalOperation(operation); |
253 | 129 return new LogicalOperationScope(TraceSource, operation); |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
130 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
131 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
132 } |