annotate Implab/Diagnostics/TraceContext.cs @ 212:a01d9df88d74 v2

Added class Trace<T> to manage channels for individual classes, if SomeClass uses Trace<SomeClass> it sould be marked with TraceSourceAttribute
author cin
date Tue, 04 Apr 2017 12:04:05 +0300
parents 71e543dbe65a
children babe55c34931
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) {
133
cin
parents: 119
diff changeset
44 //var prev = CurrentOperation;
134
04d4c92d0f28 Improved logging
cin
parents: 133
diff changeset
45 //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(takeOwnership ? TraceEventType.Attach : TraceEventType.Enter, String.Format("{0} -> {1}",prev.Name, operation.Name)));
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
46 m_stack.Push(m_current);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
47 m_current = new OperationContext(operation, takeOwnership);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
48 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
49
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
50 public void StartLogicalOperation(string name) {
200
71e543dbe65a working version of the project
cin
parents: 195
diff changeset
51 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceContext.Instance.CurrentOperation, TraceEventType.OperationStarted, name));
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
52 m_current.BeginLogicalOperation(name);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
53 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
54
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
55 public void StartLogicalOperation() {
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
56 StartLogicalOperation(String.Empty);
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
57 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
58
195
ea485487a424 minor changes
cin
parents: 134
diff changeset
59 public LogicalOperation EndLogicalOperation() {
ea485487a424 minor changes
cin
parents: 134
diff changeset
60 return m_current.EndLogicalOperation();
92
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() {
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
64 var prev = m_current.DetachLogicalOperation();
133
cin
parents: 119
diff changeset
65 //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("{0} -> {1}",prev.Name, CurrentOperation.Name)));
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
66 return prev;
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();
133
cin
parents: 119
diff changeset
72 //var prev = CurrentOperation;
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
73 m_current = m_stack.Pop();
133
cin
parents: 119
diff changeset
74 //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Leave, String.Format("{0} -> {1}", prev.Name, CurrentOperation.Name)));
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
75 } else {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 94
diff changeset
76 TraceLog.TraceWarning("Attempt to leave the last operation context");
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
77 m_current = OperationContext.EMPTY;
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 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
81 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
82