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