comparison Implab/Diagnostics/LogContext.cs @ 35:2880242f987a diagnostics

initial log capabilities
author cin
date Mon, 14 Apr 2014 18:25:26 +0400
parents
children
comparison
equal deleted inserted replaced
34:dabf79fde388 35:2880242f987a
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading;
6 using System.Threading.Tasks;
7
8 namespace Implab.Diagnostics {
9 public class LogContext {
10 LogicalOperation m_currentOperation;
11 readonly LogicalOperation m_traceBound;
12 readonly int m_threadId;
13 readonly LogContext m_parent;
14
15 readonly static object _consoleLock = new object();
16
17 [ThreadStatic]
18 static LogContext _current;
19
20 public static LogContext Current {
21 get {
22 if (_current == null)
23 _current = new LogContext();
24 return _current;
25 }
26 }
27
28 LogContext(LogContext context) {
29 if (context == null)
30 throw new ArgumentNullException("context");
31
32 m_parent = context;
33 m_currentOperation = context.CurrentOperation;
34 m_traceBound = context.CurrentOperation;
35 m_threadId = Thread.CurrentThread.ManagedThreadId;
36
37 TraceEvent(TraceEventType.Transfer, String.Empty);
38 }
39
40 LogContext() {
41 m_currentOperation = new LogicalOperation();
42 m_traceBound = m_currentOperation;
43 m_threadId = Thread.CurrentThread.ManagedThreadId;
44 }
45
46 public static void Transfer(LogContext from) {
47 _current = from == null ? new LogContext() : new LogContext(from);
48 }
49
50 public LogContext ParentContext {
51 get {
52 return m_parent;
53 }
54 }
55
56 public LogicalOperation CurrentOperation {
57 get {
58 return m_currentOperation;
59 }
60 }
61
62 public LogicalOperation TraceBound {
63 get {
64 return m_traceBound;
65 }
66 }
67
68 public int ThreadId {
69 get {
70 return m_threadId;
71 }
72 }
73
74 public void StartLogicalOperation() {
75 StartLogicalOperation(null);
76 }
77
78 public void StartLogicalOperation(string name) {
79 TraceEvent(TraceEventType.OperationStarted, "{0}", name);
80 m_currentOperation = new LogicalOperation(name, m_currentOperation);
81 }
82
83 public void EndLogicalOperation() {
84 if (m_traceBound == m_currentOperation) {
85 TraceEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace");
86 } else {
87 var op = m_currentOperation;
88 m_currentOperation = m_currentOperation.Parent;
89 TraceEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration);
90 }
91 }
92
93 public void TraceEvent(TraceEventType type, string format, params object[] args) {
94 /*var msg = new StringBuilder();
95 for (int i = 0; i < CurrentOperation.Level; i++)
96 msg.Append(" ");
97 msg.Append(type);
98 msg.AppendFormat("[{0}]: ",m_threadId);
99 msg.AppendFormat(format, args);
100
101 lock (_consoleLock) {
102 Console.ForegroundColor = (ConsoleColor)(m_threadId % 15 + 1);
103 Console.WriteLine(msg.ToString());
104 }*/
105 }
106 }
107 }