Mercurial > pub > ImplabNet
annotate Implab/Diagnostics/TraceContext.cs @ 76:c761fc982e1d v2
Refactoring of the IPromise<T> interface
Added tests
| author | cin |
|---|---|
| date | Wed, 10 Sep 2014 17:53:05 +0400 |
| parents | 790e8a997d30 |
| children | 4c0e5ef99986 |
| rev | line source |
|---|---|
| 36 | 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 { | |
| 40 | 9 /// <summary> |
| 10 /// Контекст трассировки, привязывается к потоку и содержит в себе информацию о стеке логических операций. | |
| 11 /// </summary> | |
| 12 /// <remarks> | |
| 13 /// Контекст трассировки передается слушателям событий для определения места, где возникло событие. | |
| 14 /// </remarks> | |
| 36 | 15 public class TraceContext { |
| 16 LogicalOperation m_currentOperation; | |
| 40 | 17 readonly LogicalOperation m_bound; |
| 36 | 18 readonly int m_threadId; |
| 19 | |
| 20 [ThreadStatic] | |
| 21 static TraceContext _current; | |
| 22 | |
| 40 | 23 /// <summary> |
| 24 /// Текущий контекст трассировки для потока, создается астоматически при первом обращении. | |
| 25 /// </summary> | |
| 36 | 26 public static TraceContext Current { |
| 27 get { | |
| 48 | 28 if (_current == null) { |
| 36 | 29 _current = new TraceContext(); |
| 48 | 30 _current.LogEvent(TraceEventType.Created,"[{0}]", _current.ThreadId); |
| 31 } | |
| 36 | 32 return _current; |
| 33 } | |
| 34 } | |
| 35 | |
| 48 | 36 TraceContext(TraceContext context) |
| 37 : this(context, false) { | |
| 38 } | |
| 39 | |
| 40 TraceContext(TraceContext context, bool attach) { | |
| 36 | 41 if (context == null) |
| 42 throw new ArgumentNullException("context"); | |
| 43 | |
| 44 m_currentOperation = context.CurrentOperation; | |
| 48 | 45 m_bound = attach ? context.BoundOperation : context.CurrentOperation; |
| 36 | 46 m_threadId = Thread.CurrentThread.ManagedThreadId; |
| 47 } | |
| 48 | |
| 49 TraceContext() { | |
| 50 m_currentOperation = new LogicalOperation(); | |
| 40 | 51 m_bound = m_currentOperation; |
| 36 | 52 m_threadId = Thread.CurrentThread.ManagedThreadId; |
| 53 } | |
| 54 | |
| 40 | 55 /// <summary> |
| 56 /// При необходимости копирует состояние контекста трассивровки в текущий поток. | |
| 57 /// </summary> | |
| 58 /// <param name="from">Исходный контекст трассировки, который передается.</param> | |
| 59 /// <remarks> | |
| 60 /// <para> | |
| 61 /// Копирование происходит за счет создания нового контекста трассировки и заполнением его | |
| 62 /// состояния из переданного контекста. При этом копируется стек операций, однако в новом | |
| 63 /// контексте ранее начатые логические операции не могут быть завершены. | |
| 64 /// </para> | |
| 65 /// <para> | |
| 48 | 66 /// Если передача состояния состоялась, то вызывается событие трассировки <see cref="TraceEventType.Fork"/>. |
| 40 | 67 /// </para> |
| 68 /// </remarks> | |
| 48 | 69 public static void Fork(TraceContext from) { |
| 40 | 70 if (_current == from) |
| 71 return; | |
| 72 if (from != null) { | |
| 73 var context = new TraceContext(from); | |
| 48 | 74 context.LogEvent(TraceEventType.Fork, "[{0}]-->[{1}]",from.ThreadId, context.ThreadId); |
| 40 | 75 _current = context; |
| 76 } else { | |
| 77 _current = new TraceContext(); | |
| 36 | 78 } |
| 79 } | |
| 80 | |
| 40 | 81 /// <summary> |
| 48 | 82 /// Задает текущему потоку указанный контекст, текущей поток может заканчивать ранее начатые |
| 83 /// логические операции в указанном контексте. | |
| 84 /// </summary> | |
| 85 /// <param name="source"></param> | |
| 86 public static void Attach(TraceContext source) { | |
| 87 if (_current == source) | |
| 88 return; | |
| 89 if (source != null) { | |
| 90 var context = new TraceContext(source, true); | |
| 91 context.LogEvent(TraceEventType.Attach, "[{0}]-->[{1}]", source.ThreadId, context.ThreadId); | |
| 92 _current = context; | |
| 93 } else { | |
| 94 _current = new TraceContext(); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 /// <summary> | |
| 99 /// Отсоединяет текущий контекст трассировки от потока, для дальнейшей его передачи другому потоку | |
| 100 /// <see cref="Attach(TraceContext)"/>. | |
| 101 /// </summary> | |
| 102 /// <returns>Контекст трассировки потока</returns> | |
| 103 /// <remarks> | |
| 104 /// После отсоединения контекста трассировки от потока, при первом обращении к трассировке в этом | |
| 105 /// потоке будет создан новый контекст. | |
| 106 /// </remarks> | |
| 107 public static TraceContext Detach() { | |
| 108 var context = Current; | |
| 109 context.LogEvent(TraceEventType.Detach, null); | |
| 110 _current = null; | |
| 111 return context; | |
| 112 } | |
| 113 | |
| 114 /// <summary> | |
| 115 /// Создает постоянную копию текущего контекста, данную копию можно хранить и использовать для передачи через <see cref="Fork(TraceContext)"/> | |
| 40 | 116 /// </summary> |
| 41 | 117 /// <returns>Копия текущего контекста трассировки.</returns> |
| 40 | 118 public static TraceContext Snapshot() { |
| 52 | 119 return _current == null ? new TraceContext() : new TraceContext(_current,false); |
| 40 | 120 } |
| 121 | |
| 122 /// <summary> | |
| 123 /// Выполняет переданное действие в указанном контексте трассировки, по окончании восстанавливает предыдущий контекст трассировки потока. | |
| 124 /// </summary> | |
| 125 /// <param name="action"></param> | |
| 126 public void Invoke(Action action) { | |
| 127 if (action == null) | |
| 128 throw new ArgumentNullException("action"); | |
| 129 var old = _current; | |
| 48 | 130 Fork(this); |
| 40 | 131 try { |
| 132 action(); | |
| 133 } finally { | |
| 52 | 134 if(_current != null) |
| 135 _current.EndAllOperations(); | |
| 40 | 136 _current = old; |
| 137 } | |
| 138 } | |
| 139 | |
| 140 /// <summary> | |
| 141 /// Текущая логическая операция. | |
| 142 /// </summary> | |
| 36 | 143 public LogicalOperation CurrentOperation { |
| 144 get { | |
| 145 return m_currentOperation; | |
| 146 } | |
| 147 } | |
| 148 | |
| 40 | 149 /// <summary> |
| 150 /// Операция ниже которой нельзя опускаться в стеке логических операций, т.е. она не может быть завершена в текущем контексте. | |
| 151 /// </summary> | |
| 152 public LogicalOperation BoundOperation { | |
| 36 | 153 get { |
| 40 | 154 return m_bound; |
| 36 | 155 } |
| 156 } | |
| 157 | |
| 40 | 158 /// <summary> |
| 159 /// Поток, в котором создан контекст трассировки. | |
| 160 /// </summary> | |
| 36 | 161 public int ThreadId { |
| 162 get { | |
| 163 return m_threadId; | |
| 164 } | |
| 165 } | |
| 166 | |
| 40 | 167 /// <summary> |
| 168 /// Начинает безымянную логическую операцию. | |
| 169 /// </summary> | |
| 36 | 170 public void StartLogicalOperation() { |
| 171 StartLogicalOperation(null); | |
| 172 } | |
| 173 | |
| 40 | 174 /// <summary> |
| 175 /// Начинает логическую операцию с указанным именем. Созданная операция будет добвалена в стек логических операций контекста, затем будет создано соответсвующее событие. | |
| 176 /// </summary> | |
| 177 /// <param name="name">Имя начинаемой операции.</param> | |
| 36 | 178 public void StartLogicalOperation(string name) { |
| 179 m_currentOperation = new LogicalOperation(name, m_currentOperation); | |
| 40 | 180 LogEvent(TraceEventType.OperationStarted, name); |
| 36 | 181 } |
| 182 | |
| 40 | 183 /// <summary> |
| 184 /// Заканчивает логическую операцию начатую в текущем контексте. Операции, начатые в других контекстах не могут быть закончены в текущем контексте. | |
| 185 /// </summary> | |
| 186 /// <remarks> | |
| 187 /// При вызове данного метода создается событие журнала трассировки, либо о завершении операции, либо об ошибки, поскольку данная операция | |
| 188 /// начата в другом контексте. | |
| 189 /// </remarks> | |
| 36 | 190 public void EndLogicalOperation() { |
| 40 | 191 if (m_bound == m_currentOperation) { |
| 36 | 192 LogEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace"); |
| 193 } else { | |
| 194 var op = m_currentOperation; | |
| 40 | 195 LogEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration); |
| 36 | 196 m_currentOperation = m_currentOperation.Parent; |
| 197 } | |
| 198 } | |
| 199 | |
|
43
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
41
diff
changeset
|
200 /// <summary> |
| 52 | 201 /// Создает копию контекста и возвращается на предыдущую операцию в текущем контексте, это позволяет начать операцию в одном потоке, а завершить - в другом. |
| 202 /// </summary> | |
| 203 /// <returns>Контекст трассировки, который можно присоединить к другому потоку.</returns> | |
| 204 public TraceContext DetachLogicalOperation() { | |
| 205 if (m_bound == m_currentOperation) { | |
| 206 return new TraceContext(); | |
| 207 } else { | |
| 208 var detached = new TraceContext(this, true); | |
| 209 m_currentOperation = m_currentOperation.Parent; | |
| 210 return detached; | |
| 211 } | |
| 212 } | |
| 213 | |
| 66 | 214 public void BindLogicalOperationToPromise(IPromise promise) { |
| 52 | 215 Safe.ArgumentNotNull(promise, "promise"); |
| 216 | |
| 217 var ctx = DetachLogicalOperation(); | |
| 76 | 218 promise.Anyway(() => { |
| 52 | 219 var old = _current; |
| 220 TraceContext.Attach(ctx); | |
| 221 TraceContext.Current.EndLogicalOperation(); | |
| 222 _current = old; | |
| 223 }); | |
| 224 } | |
| 225 | |
| 226 /// <summary> | |
|
43
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
41
diff
changeset
|
227 /// Заврешает все начатые в этом контексте операции |
|
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
41
diff
changeset
|
228 /// </summary> |
|
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
41
diff
changeset
|
229 public void EndAllOperations() { |
|
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
41
diff
changeset
|
230 while (m_bound != m_currentOperation) |
|
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
41
diff
changeset
|
231 EndLogicalOperation(); |
|
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
41
diff
changeset
|
232 } |
|
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
41
diff
changeset
|
233 |
| 36 | 234 void LogEvent(TraceEventType type, string format, params object[] args) { |
| 37 | 235 LogChannel<TraceEvent>.Default.LogEvent(this, TraceEvent.Create(type, format, args)); |
| 36 | 236 } |
| 237 } | |
| 238 } |
