annotate Implab/Diagnostics/Trace.cs @ 254:12c00235b105 v3

Добавлена метка v3.0.1-beta для набора изменений 34df34841225
author cin
date Mon, 12 Feb 2018 17:03:49 +0300
parents 34df34841225
children b00441e04738
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
6 using System.Threading.Tasks;
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
7
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
8 namespace Implab.Diagnostics {
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
9 public static class Trace<T> {
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
10
252
6f4630d0bcd9 removed absolete Diagnostics classes
cin
parents: 218
diff changeset
11 readonly static TraceSource _traceSource = new TraceSource(typeof(T).Name);
6f4630d0bcd9 removed absolete Diagnostics classes
cin
parents: 218
diff changeset
12
6f4630d0bcd9 removed absolete Diagnostics classes
cin
parents: 218
diff changeset
13 public static TraceSource TraceSource {
6f4630d0bcd9 removed absolete Diagnostics classes
cin
parents: 218
diff changeset
14 get { return _traceSource; }
212
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
15 }
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
16
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
17 /// <summary>
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
18 /// 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
19 /// </summary>
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
20 [Conditional("TRACE")]
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
21 public static void StartLogicalOperation() {
252
6f4630d0bcd9 removed absolete Diagnostics classes
cin
parents: 218
diff changeset
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>
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
30 [Conditional("TRACE")]
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
31 public static void StartLogicalOperation(string name) {
253
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
32 Trace.CorrelationManager.StartLogicalOperation(name);
212
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
33 }
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
34
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 /// 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
37 /// </summary>
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
38 [Conditional("TRACE")]
252
6f4630d0bcd9 removed absolete Diagnostics classes
cin
parents: 218
diff changeset
39 public static void StopLogicalOperation() {
6f4630d0bcd9 removed absolete Diagnostics classes
cin
parents: 218
diff changeset
40 Trace.CorrelationManager.StopLogicalOperation();
212
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 /// Writes an informational message.
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="format">Format.</param>
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
47 /// <param name="arguments">Arguments.</param>
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
48 [Conditional("TRACE")]
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
49 public static void Log(string format, params object[] arguments) {
253
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
50 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
51 }
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
52
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
53 /// <summary>
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
54 /// Writes a warning message.
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
55 /// </summary>
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
56 /// <param name="format">Format.</param>
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
57 /// <param name="arguments">Arguments.</param>
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
58 [Conditional("TRACE")]
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
59 public static void Warn(string format, params object[] arguments) {
253
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
60 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
61 }
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
62
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 Error(string format, params object[] arguments) {
253
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
65 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
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(Exception err) {
253
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
70 TraceSource.TraceData(TraceEventType.Error, 0, err);
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
71 }
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
72
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
73 /// <summary>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
74 /// This method save the current activity, and transfers to the specified activity,
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
75 /// emits <see cref="TraceEventType.Start"/> and returns a scope of the new
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
76 /// activity.
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
77 /// </summary>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
78 /// <param name="activityName">The name of the new activity/</param>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
79 /// <param name="activityId">The identifier of the activity to which
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
80 /// the control will be transferred</param>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
81 /// <returns>A scope of the new activity, dispose it to transfer
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
82 /// the control back to the original activity.</returns>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
83 public static ActivityScope TransferActivity(string activityName, Guid activityId) {
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
84 var prev = Trace.CorrelationManager.ActivityId;
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
85
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
86 TraceSource.TraceTransfer(0, "Transfer", activityId);
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
87 Trace.CorrelationManager.ActivityId = activityId;
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
88 TraceSource.TraceEvent(TraceEventType.Start, 0, activityName);
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
89
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
90 return new ActivityScope(TraceSource, prev, 0, activityName);
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
91 }
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
92
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
93 /// <summary>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
94 /// Emits <see cref="TraceEventType.Start"/> and returns a scope of the
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
95 /// activity.
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
96 /// </summary>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
97 /// <param name="activityName">The name of the activity to start</param>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
98 /// <returns>A scope of the new activity, dispose it to emit
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
99 /// <see cref="TraceEventType.Stop"/> for the current activity.</returns>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
100 public static ActivityScope StartActivity(string activityName) {
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
101 if (Trace.CorrelationManager.ActivityId == Guid.Empty)
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
102 Trace.CorrelationManager.ActivityId = Guid.NewGuid();
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
103
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
104 var prev = Trace.CorrelationManager.ActivityId;
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
105
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
106 TraceSource.TraceEvent(TraceEventType.Start, 0, activityName);
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
107 return new ActivityScope(TraceSource, prev, 0, activityName);
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
108 }
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
109
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
110 /// <summary>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
111 /// Creates new <see cref="LogicalOperation(string)"/> and calls
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
112 /// to <see cref="CorrelationManager.StartLogicalOperation(object)"/>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
113 /// passing the created operation as identity. Calls
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
114 /// <see cref="TraceSource.TraceData(TraceEventType, int, object)"/>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
115 /// to notify listeners on operation start.
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
116 /// </summary>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
117 /// <param name="name">The name of the logical operation.</param>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
118 /// <returns>Logical operation scope, disposing it will stop
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
119 /// logical operation and notify trace listeners.</returns>
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
120 public static LogicalOperationScope LogicalOperation(string name) {
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
121 var operation = new LogicalOperation(name);
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
122 TraceSource.TraceData(TraceEventType.Information, TraceEventCodes.StartLogicalOperation, operation);
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
123 Trace.CorrelationManager.StartLogicalOperation(operation);
34df34841225 Implab.Diagnostics drafts
cin
parents: 252
diff changeset
124 return new LogicalOperationScope(TraceSource, operation);
212
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
125 }
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
126 }
a01d9df88d74 Added class Trace<T> to manage channels for individual classes, if SomeClass
cin
parents:
diff changeset
127 }