Mercurial > pub > ImplabNet
annotate Implab/Diagnostics/Trace.cs @ 281:e0916ddc9950 v3 tip
code cleanup and refactoring
author | cin |
---|---|
date | Fri, 01 Jun 2018 21:35:24 +0300 |
parents | f07be402ab02 |
children |
rev | line source |
---|---|
281 | 1 // enable System.Diagnostics trace methods |
2 #define TRACE | |
3 | |
4 using System; | |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
5 using System.Collections.Generic; |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
6 using System.Diagnostics; |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
7 using System.Linq; |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
8 using System.Text; |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
9 using System.Threading; |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
10 using System.Threading.Tasks; |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
11 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
12 namespace Implab.Diagnostics { |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
13 public static class Trace<T> { |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
14 |
281 | 15 static Lazy<TraceSource> _traceSource = new Lazy<TraceSource>(CreateChannel, LazyThreadSafetyMode.ExecutionAndPublication); |
16 | |
17 static int _nextId; | |
18 | |
19 static TraceSource CreateChannel() { | |
20 var id = Interlocked.Increment(ref _nextId); | |
21 return new TraceSource(typeof(T).Name); | |
22 } | |
23 | |
24 public static TraceSource TraceSource { get { return _traceSource.Value; } } | |
25 | |
26 public static IDisposable Subscribe() { | |
27 | |
28 throw new NotImplementedException(); | |
29 } | |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
30 |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
31 #if NETFX_TRACE_BUG |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
32 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
|
33 #endif |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
34 |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
35 /// <summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
36 /// 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
|
37 /// </summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
38 public static void StartLogicalOperation() { |
252 | 39 Trace.CorrelationManager.StartLogicalOperation(); |
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 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
42 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
43 /// <summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
44 /// 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
|
45 /// </summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
46 /// <param name="name">Name.</param> |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
47 #if NETFX_TRACE_BUG |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
48 public static void StartLogicalOperation(object name) { |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
49 m_currentOperation.Value = name; |
253 | 50 Trace.CorrelationManager.StartLogicalOperation(name); |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
51 } |
255
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
52 #else |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
53 public static void StartLogicalOperation(object name) { |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
54 Trace.CorrelationManager.StartLogicalOperation(name); |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
55 } |
b00441e04738
Adde workaround to the behaviour of the logical operations stack in conjuction
cin
parents:
253
diff
changeset
|
56 #endif |
212
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 /// 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
|
60 /// </summary> |
252 | 61 public static void StopLogicalOperation() { |
62 Trace.CorrelationManager.StopLogicalOperation(); | |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
63 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
64 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
65 /// <summary> |
280 | 66 /// Writes a debug message. |
67 /// </summary> | |
68 /// <param name="format">Format.</param> | |
69 /// <param name="arguments">Arguments.</param> | |
70 [Conditional("DEBUG")] | |
71 public static void Debug(string format, params object[] arguments) { | |
72 | |
73 } | |
74 | |
75 /// <summary> | |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
76 /// Writes an informational message. |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
77 /// </summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
78 /// <param name="format">Format.</param> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
79 /// <param name="arguments">Arguments.</param> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
80 [Conditional("TRACE")] |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
81 public static void Log(string format, params object[] arguments) { |
253 | 82 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
|
83 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
84 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
85 /// <summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
86 /// Writes a warning message. |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
87 /// </summary> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
88 /// <param name="format">Format.</param> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
89 /// <param name="arguments">Arguments.</param> |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
90 public static void Warn(string format, params object[] arguments) { |
253 | 91 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
|
92 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
93 |
280 | 94 /// <summary> |
95 /// Writes a error message. | |
96 /// </summary> | |
97 /// <param name="format">Format.</param> | |
98 /// <param name="arguments">Arguments.</param> | |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
99 public static void Error(string format, params object[] arguments) { |
253 | 100 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
|
101 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
102 |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
103 public static void Error(Exception err) { |
253 | 104 TraceSource.TraceData(TraceEventType.Error, 0, err); |
105 } | |
106 | |
107 /// <summary> | |
108 /// This method save the current activity, and transfers to the specified activity, | |
109 /// emits <see cref="TraceEventType.Start"/> and returns a scope of the new | |
110 /// activity. | |
111 /// </summary> | |
112 /// <param name="activityName">The name of the new activity/</param> | |
113 /// <param name="activityId">The identifier of the activity to which | |
114 /// the control will be transferred</param> | |
115 /// <returns>A scope of the new activity, dispose it to transfer | |
116 /// the control back to the original activity.</returns> | |
117 public static ActivityScope TransferActivity(string activityName, Guid activityId) { | |
118 var prev = Trace.CorrelationManager.ActivityId; | |
119 | |
120 TraceSource.TraceTransfer(0, "Transfer", activityId); | |
121 Trace.CorrelationManager.ActivityId = activityId; | |
122 TraceSource.TraceEvent(TraceEventType.Start, 0, activityName); | |
123 | |
124 return new ActivityScope(TraceSource, prev, 0, activityName); | |
125 } | |
126 | |
127 /// <summary> | |
128 /// Emits <see cref="TraceEventType.Start"/> and returns a scope of the | |
129 /// activity. | |
130 /// </summary> | |
131 /// <param name="activityName">The name of the activity to start</param> | |
132 /// <returns>A scope of the new activity, dispose it to emit | |
133 /// <see cref="TraceEventType.Stop"/> for the current activity.</returns> | |
134 public static ActivityScope StartActivity(string activityName) { | |
135 if (Trace.CorrelationManager.ActivityId == Guid.Empty) | |
136 Trace.CorrelationManager.ActivityId = Guid.NewGuid(); | |
137 | |
138 var prev = Trace.CorrelationManager.ActivityId; | |
139 | |
140 TraceSource.TraceEvent(TraceEventType.Start, 0, activityName); | |
141 return new ActivityScope(TraceSource, prev, 0, activityName); | |
142 } | |
143 | |
144 /// <summary> | |
145 /// Creates new <see cref="LogicalOperation(string)"/> and calls | |
146 /// to <see cref="CorrelationManager.StartLogicalOperation(object)"/> | |
147 /// passing the created operation as identity. Calls | |
148 /// <see cref="TraceSource.TraceData(TraceEventType, int, object)"/> | |
149 /// to notify listeners on operation start. | |
150 /// </summary> | |
151 /// <param name="name">The name of the logical operation.</param> | |
152 /// <returns>Logical operation scope, disposing it will stop | |
153 /// logical operation and notify trace listeners.</returns> | |
154 public static LogicalOperationScope LogicalOperation(string name) { | |
155 var operation = new LogicalOperation(name); | |
156 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
|
157 StartLogicalOperation(operation); |
253 | 158 return new LogicalOperationScope(TraceSource, operation); |
212
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
159 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
160 } |
a01d9df88d74
Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff
changeset
|
161 } |