annotate Implab/Diagnostics/TraceContext.cs @ 36:313f708a50e9 diagnostics

improved log concept
author cin
date Tue, 15 Apr 2014 02:00:09 +0400
parents
children c2c043520724
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
1 using System;
313f708a50e9 improved log concept
cin
parents:
diff changeset
2 using System.Collections.Generic;
313f708a50e9 improved log concept
cin
parents:
diff changeset
3 using System.Linq;
313f708a50e9 improved log concept
cin
parents:
diff changeset
4 using System.Text;
313f708a50e9 improved log concept
cin
parents:
diff changeset
5 using System.Threading;
313f708a50e9 improved log concept
cin
parents:
diff changeset
6 using System.Threading.Tasks;
313f708a50e9 improved log concept
cin
parents:
diff changeset
7
313f708a50e9 improved log concept
cin
parents:
diff changeset
8 namespace Implab.Diagnostics {
313f708a50e9 improved log concept
cin
parents:
diff changeset
9 public class TraceContext {
313f708a50e9 improved log concept
cin
parents:
diff changeset
10 LogicalOperation m_currentOperation;
313f708a50e9 improved log concept
cin
parents:
diff changeset
11 readonly LogicalOperation m_traceBound;
313f708a50e9 improved log concept
cin
parents:
diff changeset
12 readonly int m_threadId;
313f708a50e9 improved log concept
cin
parents:
diff changeset
13 readonly TraceContext m_parent;
313f708a50e9 improved log concept
cin
parents:
diff changeset
14
313f708a50e9 improved log concept
cin
parents:
diff changeset
15 readonly static object _consoleLock = new object();
313f708a50e9 improved log concept
cin
parents:
diff changeset
16
313f708a50e9 improved log concept
cin
parents:
diff changeset
17 [ThreadStatic]
313f708a50e9 improved log concept
cin
parents:
diff changeset
18 static TraceContext _current;
313f708a50e9 improved log concept
cin
parents:
diff changeset
19
313f708a50e9 improved log concept
cin
parents:
diff changeset
20 public static TraceContext Current {
313f708a50e9 improved log concept
cin
parents:
diff changeset
21 get {
313f708a50e9 improved log concept
cin
parents:
diff changeset
22 if (_current == null)
313f708a50e9 improved log concept
cin
parents:
diff changeset
23 _current = new TraceContext();
313f708a50e9 improved log concept
cin
parents:
diff changeset
24 return _current;
313f708a50e9 improved log concept
cin
parents:
diff changeset
25 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
26 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
27
313f708a50e9 improved log concept
cin
parents:
diff changeset
28 TraceContext(TraceContext context) {
313f708a50e9 improved log concept
cin
parents:
diff changeset
29 if (context == null)
313f708a50e9 improved log concept
cin
parents:
diff changeset
30 throw new ArgumentNullException("context");
313f708a50e9 improved log concept
cin
parents:
diff changeset
31
313f708a50e9 improved log concept
cin
parents:
diff changeset
32 m_parent = context;
313f708a50e9 improved log concept
cin
parents:
diff changeset
33 m_currentOperation = context.CurrentOperation;
313f708a50e9 improved log concept
cin
parents:
diff changeset
34 m_traceBound = context.CurrentOperation;
313f708a50e9 improved log concept
cin
parents:
diff changeset
35 m_threadId = Thread.CurrentThread.ManagedThreadId;
313f708a50e9 improved log concept
cin
parents:
diff changeset
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
37 LogEvent(TraceEventType.Transfer, String.Empty);
313f708a50e9 improved log concept
cin
parents:
diff changeset
38 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
39
313f708a50e9 improved log concept
cin
parents:
diff changeset
40 TraceContext() {
313f708a50e9 improved log concept
cin
parents:
diff changeset
41 m_currentOperation = new LogicalOperation();
313f708a50e9 improved log concept
cin
parents:
diff changeset
42 m_traceBound = m_currentOperation;
313f708a50e9 improved log concept
cin
parents:
diff changeset
43 m_threadId = Thread.CurrentThread.ManagedThreadId;
313f708a50e9 improved log concept
cin
parents:
diff changeset
44 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
45
313f708a50e9 improved log concept
cin
parents:
diff changeset
46 public static void Transfer(TraceContext from) {
313f708a50e9 improved log concept
cin
parents:
diff changeset
47 _current = from == null ? new TraceContext() : new TraceContext(from);
313f708a50e9 improved log concept
cin
parents:
diff changeset
48 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
49
313f708a50e9 improved log concept
cin
parents:
diff changeset
50 public TraceContext ParentContext {
313f708a50e9 improved log concept
cin
parents:
diff changeset
51 get {
313f708a50e9 improved log concept
cin
parents:
diff changeset
52 return m_parent;
313f708a50e9 improved log concept
cin
parents:
diff changeset
53 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
54 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
55
313f708a50e9 improved log concept
cin
parents:
diff changeset
56 public LogicalOperation CurrentOperation {
313f708a50e9 improved log concept
cin
parents:
diff changeset
57 get {
313f708a50e9 improved log concept
cin
parents:
diff changeset
58 return m_currentOperation;
313f708a50e9 improved log concept
cin
parents:
diff changeset
59 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
60 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
61
313f708a50e9 improved log concept
cin
parents:
diff changeset
62 public LogicalOperation TraceBound {
313f708a50e9 improved log concept
cin
parents:
diff changeset
63 get {
313f708a50e9 improved log concept
cin
parents:
diff changeset
64 return m_traceBound;
313f708a50e9 improved log concept
cin
parents:
diff changeset
65 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
66 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
67
313f708a50e9 improved log concept
cin
parents:
diff changeset
68 public int ThreadId {
313f708a50e9 improved log concept
cin
parents:
diff changeset
69 get {
313f708a50e9 improved log concept
cin
parents:
diff changeset
70 return m_threadId;
313f708a50e9 improved log concept
cin
parents:
diff changeset
71 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
72 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
73
313f708a50e9 improved log concept
cin
parents:
diff changeset
74 public void StartLogicalOperation() {
313f708a50e9 improved log concept
cin
parents:
diff changeset
75 StartLogicalOperation(null);
313f708a50e9 improved log concept
cin
parents:
diff changeset
76 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
77
313f708a50e9 improved log concept
cin
parents:
diff changeset
78 public void StartLogicalOperation(string name) {
313f708a50e9 improved log concept
cin
parents:
diff changeset
79 LogEvent(TraceEventType.OperationStarted, "{0}", name);
313f708a50e9 improved log concept
cin
parents:
diff changeset
80 m_currentOperation = new LogicalOperation(name, m_currentOperation);
313f708a50e9 improved log concept
cin
parents:
diff changeset
81 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
82
313f708a50e9 improved log concept
cin
parents:
diff changeset
83 public void EndLogicalOperation() {
313f708a50e9 improved log concept
cin
parents:
diff changeset
84 if (m_traceBound == m_currentOperation) {
313f708a50e9 improved log concept
cin
parents:
diff changeset
85 LogEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace");
313f708a50e9 improved log concept
cin
parents:
diff changeset
86 } else {
313f708a50e9 improved log concept
cin
parents:
diff changeset
87 var op = m_currentOperation;
313f708a50e9 improved log concept
cin
parents:
diff changeset
88 m_currentOperation = m_currentOperation.Parent;
313f708a50e9 improved log concept
cin
parents:
diff changeset
89 LogEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration);
313f708a50e9 improved log concept
cin
parents:
diff changeset
90 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
91 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
92
313f708a50e9 improved log concept
cin
parents:
diff changeset
93 void LogEvent(TraceEventType type, string format, params object[] args) {
313f708a50e9 improved log concept
cin
parents:
diff changeset
94 LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(type, format, args));
313f708a50e9 improved log concept
cin
parents:
diff changeset
95 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
96 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
97 }