annotate Implab/Diagnostics/TraceContext.cs @ 93:dc4942d09e74 v2

improved tracing added the application components container MTComponentContainer.AppContainer
author cin
date Thu, 23 Oct 2014 01:13:57 +0400
parents 4c0e5ef99986
children a43745f81f10
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
1 using System;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
2 using System.Collections.Generic;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
3 using System.Threading;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
4
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
5 namespace Implab.Diagnostics {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
6 /// <summary>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
7 /// Trace context is bound to the specific thread, each thread has it's own ThreadContext.
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
8 /// </summary>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
9 /// <remarks>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
10 /// ThreadContext manages relations between logical operations and threads.
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
11 /// </remarks>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
12 public class TraceContext {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
13
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
14 [ThreadStatic]
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
15 static TraceContext _instance;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
16
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
17 OperationContext m_current = OperationContext.EMPTY;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
18 readonly Stack<OperationContext> m_stack = new Stack<OperationContext>();
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
19 readonly int m_threadId;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
20
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
21 public static TraceContext Instance {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
22 get {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
23 if (_instance == null)
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
24 _instance = new TraceContext();
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
25 return _instance;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
26 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
27 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
28
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
29 public TraceContext() {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
30 m_threadId = Thread.CurrentThread.ManagedThreadId;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
31 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
32
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
33 public int ThreadId {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
34 get { return m_threadId; }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
35 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
36
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
37 public LogicalOperation CurrentOperation {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
38 get {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
39 return m_current.CurrentOperation;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
40 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
41 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
42
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
43 public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) {
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
44 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Attach, String.Format("{0} -> [{1}]", operation.Name, m_threadId)));
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
45 m_stack.Push(m_current);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
46 m_current = new OperationContext(operation, takeOwnership);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
47 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
48
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
49 public void StartLogicalOperation(string name) {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
50 m_current.BeginLogicalOperation(name);
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
51 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationStarted, String.Format("+{0}",CurrentOperation.Name)));
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
52 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
53
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
54 public void StartLogicalOperation() {
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
55 StartLogicalOperation(String.Empty);
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
56 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
57
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
58 public void EndLogicalOperation() {
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
59 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",CurrentOperation.Name, CurrentOperation.Duration)));
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
60 m_current.EndLogicalOperation();
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
61 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
62
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
63 public LogicalOperation DetachLogicalOperation() {
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
64 var op = m_current.DetachLogicalOperation();
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
65 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("[{0}] -> {1}", m_threadId, op.Name)));
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
66 return op;
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
67 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
68
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
69 public void Leave() {
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
70 if (m_stack.Count > 0) {
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
71 m_current.Leave();
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
72 m_current = m_stack.Pop();
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
73 } else {
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
74 TraceLog.TraceWarning("Attemtp to leave the last operation context");
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
75 m_current = OperationContext.EMPTY;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
76 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
77 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
78 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
79 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
80