@@ -1,24 +1,30 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | |
|
6 | 6 | namespace Implab.Diagnostics { |
|
7 | 7 | public class LogChannel<TEvent> { |
|
8 | 8 | static LogChannel<TEvent> _default = new LogChannel<TEvent>(); |
|
9 | 9 | |
|
10 | 10 | public static LogChannel<TEvent> Default { |
|
11 | 11 | get { |
|
12 | 12 | return _default; |
|
13 | 13 | } |
|
14 | 14 | } |
|
15 | 15 | |
|
16 | 16 | public event EventHandler<ValueEventArgs<TEvent>> Events; |
|
17 | 17 | |
|
18 | 18 | public void LogEvent(TEvent data) { |
|
19 | 19 | var t = Events; |
|
20 | 20 | if (t!= null) |
|
21 | 21 | t(TraceContext.Current,new ValueEventArgs<TEvent>(data)); |
|
22 | 22 | } |
|
23 | ||
|
24 | public void LogEvent(TraceContext context,TEvent data) { | |
|
25 | var t = Events; | |
|
26 | if (t != null) | |
|
27 | t(context, new ValueEventArgs<TEvent>(data)); | |
|
23 | 28 | } |
|
24 | 29 | } |
|
30 | } |
@@ -1,97 +1,97 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading; |
|
6 | 6 | using System.Threading.Tasks; |
|
7 | 7 | |
|
8 | 8 | namespace Implab.Diagnostics { |
|
9 | 9 | public class TraceContext { |
|
10 | 10 | LogicalOperation m_currentOperation; |
|
11 | 11 | readonly LogicalOperation m_traceBound; |
|
12 | 12 | readonly int m_threadId; |
|
13 | 13 | readonly TraceContext m_parent; |
|
14 | 14 | |
|
15 | 15 | readonly static object _consoleLock = new object(); |
|
16 | 16 | |
|
17 | 17 | [ThreadStatic] |
|
18 | 18 | static TraceContext _current; |
|
19 | 19 | |
|
20 | 20 | public static TraceContext Current { |
|
21 | 21 | get { |
|
22 | 22 | if (_current == null) |
|
23 | 23 | _current = new TraceContext(); |
|
24 | 24 | return _current; |
|
25 | 25 | } |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | TraceContext(TraceContext context) { |
|
29 | 29 | if (context == null) |
|
30 | 30 | throw new ArgumentNullException("context"); |
|
31 | 31 | |
|
32 | 32 | m_parent = context; |
|
33 | 33 | m_currentOperation = context.CurrentOperation; |
|
34 | 34 | m_traceBound = context.CurrentOperation; |
|
35 | 35 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
36 | 36 | |
|
37 |
LogEvent(TraceEventType.Transfer, |
|
|
37 | LogEvent(TraceEventType.Transfer, "FORK {0}", context.ThreadId); | |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | TraceContext() { |
|
41 | 41 | m_currentOperation = new LogicalOperation(); |
|
42 | 42 | m_traceBound = m_currentOperation; |
|
43 | 43 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | public static void Transfer(TraceContext from) { |
|
47 | 47 | _current = from == null ? new TraceContext() : new TraceContext(from); |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | public TraceContext ParentContext { |
|
51 | 51 | get { |
|
52 | 52 | return m_parent; |
|
53 | 53 | } |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | public LogicalOperation CurrentOperation { |
|
57 | 57 | get { |
|
58 | 58 | return m_currentOperation; |
|
59 | 59 | } |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | public LogicalOperation TraceBound { |
|
63 | 63 | get { |
|
64 | 64 | return m_traceBound; |
|
65 | 65 | } |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | public int ThreadId { |
|
69 | 69 | get { |
|
70 | 70 | return m_threadId; |
|
71 | 71 | } |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | 74 | public void StartLogicalOperation() { |
|
75 | 75 | StartLogicalOperation(null); |
|
76 | 76 | } |
|
77 | 77 | |
|
78 | 78 | public void StartLogicalOperation(string name) { |
|
79 | 79 | LogEvent(TraceEventType.OperationStarted, "{0}", name); |
|
80 | 80 | m_currentOperation = new LogicalOperation(name, m_currentOperation); |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | public void EndLogicalOperation() { |
|
84 | 84 | if (m_traceBound == m_currentOperation) { |
|
85 | 85 | LogEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace"); |
|
86 | 86 | } else { |
|
87 | 87 | var op = m_currentOperation; |
|
88 | 88 | m_currentOperation = m_currentOperation.Parent; |
|
89 | 89 | LogEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration); |
|
90 | 90 | } |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | void LogEvent(TraceEventType type, string format, params object[] args) { |
|
94 | LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(type, format, args)); | |
|
94 | LogChannel<TraceEvent>.Default.LogEvent(this, TraceEvent.Create(type, format, args)); | |
|
95 | 95 | } |
|
96 | 96 | } |
|
97 | 97 | } |
General Comments 0
You need to be logged in to leave comments.
Login now