Auto status change to "Under Review"
@@ -1,82 +1,82 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Threading; |
|
4 | 4 | |
|
5 | 5 | namespace Implab.Diagnostics { |
|
6 | 6 | /// <summary> |
|
7 | 7 | /// Trace context is bound to the specific thread, each thread has it's own ThreadContext. |
|
8 | 8 | /// </summary> |
|
9 | 9 | /// <remarks> |
|
10 | 10 | /// ThreadContext manages relations between logical operations and threads. |
|
11 | 11 | /// </remarks> |
|
12 | 12 | public class TraceContext { |
|
13 | 13 | |
|
14 | 14 | [ThreadStatic] |
|
15 | 15 | static TraceContext _instance; |
|
16 | 16 | |
|
17 | 17 | OperationContext m_current = OperationContext.EMPTY; |
|
18 | 18 | readonly Stack<OperationContext> m_stack = new Stack<OperationContext>(); |
|
19 | 19 | readonly int m_threadId; |
|
20 | 20 | |
|
21 | 21 | public static TraceContext Instance { |
|
22 | 22 | get { |
|
23 | 23 | if (_instance == null) |
|
24 | 24 | _instance = new TraceContext(); |
|
25 | 25 | return _instance; |
|
26 | 26 | } |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | public TraceContext() { |
|
30 | 30 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | public int ThreadId { |
|
34 | 34 | get { return m_threadId; } |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | public LogicalOperation CurrentOperation { |
|
38 | 38 | get { |
|
39 | 39 | return m_current.CurrentOperation; |
|
40 | 40 | } |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) { |
|
44 | 44 | //var prev = CurrentOperation; |
|
45 | 45 | //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(takeOwnership ? TraceEventType.Attach : TraceEventType.Enter, String.Format("{0} -> {1}",prev.Name, operation.Name))); |
|
46 | 46 | m_stack.Push(m_current); |
|
47 | 47 | m_current = new OperationContext(operation, takeOwnership); |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | public void StartLogicalOperation(string name) { |
|
51 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationStarted, name)); | |
|
51 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceContext.Instance.CurrentOperation, TraceEventType.OperationStarted, name)); | |
|
52 | 52 | m_current.BeginLogicalOperation(name); |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | public void StartLogicalOperation() { |
|
56 | 56 | StartLogicalOperation(String.Empty); |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | 59 | public LogicalOperation EndLogicalOperation() { |
|
60 | 60 | return m_current.EndLogicalOperation(); |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | public LogicalOperation DetachLogicalOperation() { |
|
64 | 64 | var prev = m_current.DetachLogicalOperation(); |
|
65 | 65 | //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("{0} -> {1}",prev.Name, CurrentOperation.Name))); |
|
66 | 66 | return prev; |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | public void Leave() { |
|
70 | 70 | if (m_stack.Count > 0) { |
|
71 | 71 | m_current.Leave(); |
|
72 | 72 | //var prev = CurrentOperation; |
|
73 | 73 | m_current = m_stack.Pop(); |
|
74 | 74 | //LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Leave, String.Format("{0} -> {1}", prev.Name, CurrentOperation.Name))); |
|
75 | 75 | } else { |
|
76 | 76 | TraceLog.TraceWarning("Attempt to leave the last operation context"); |
|
77 | 77 | m_current = OperationContext.EMPTY; |
|
78 | 78 | } |
|
79 | 79 | } |
|
80 | 80 | } |
|
81 | 81 | } |
|
82 | 82 |
@@ -1,70 +1,70 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Diagnostics; |
|
4 | 4 | using System.Linq; |
|
5 | 5 | using System.Text; |
|
6 | 6 | using System.Threading.Tasks; |
|
7 | 7 | |
|
8 | 8 | namespace Implab.Diagnostics { |
|
9 | 9 | /// <summary> |
|
10 | 10 | /// This class is used to trace a logical flow of the application, it publishes events to the default <see cref="TraceEvent"/> channel. |
|
11 | 11 | /// </summary> |
|
12 | 12 | public static class TraceLog { |
|
13 | 13 | /// <summary> |
|
14 | 14 | /// Starts the logical operation nested to the current operation nested to the current one. |
|
15 | 15 | /// </summary> |
|
16 | 16 | [Conditional("TRACE")] |
|
17 | 17 | public static void StartLogicalOperation() { |
|
18 | 18 | TraceContext.Instance.StartLogicalOperation(); |
|
19 | 19 | |
|
20 | 20 | } |
|
21 | 21 | |
|
22 | 22 | /// <summary> |
|
23 | 23 | /// Starts the logical operation with the specified name, this name is usefull in logs. |
|
24 | 24 | /// </summary> |
|
25 | 25 | /// <param name="name">Name.</param> |
|
26 | 26 | [Conditional("TRACE")] |
|
27 | 27 | public static void StartLogicalOperation(string name) { |
|
28 | 28 | TraceContext.Instance.StartLogicalOperation(name); |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | /// <summary> |
|
32 | 32 | /// Ends the logical operation and restores the previous one. |
|
33 | 33 | /// </summary> |
|
34 | 34 | [Conditional("TRACE")] |
|
35 | 35 | public static void EndLogicalOperation() { |
|
36 | 36 | var op = TraceContext.Instance.EndLogicalOperation(); |
|
37 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",op.Name, op.Duration))); | |
|
37 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(op, TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",op.Name, op.Duration))); | |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | /// <summary> |
|
41 | 41 | /// Writes an informational message. |
|
42 | 42 | /// </summary> |
|
43 | 43 | /// <param name="format">Format.</param> |
|
44 | 44 | /// <param name="arguments">Arguments.</param> |
|
45 | 45 | [Conditional("TRACE")] |
|
46 | 46 | public static void TraceInformation(string format, params object[] arguments) { |
|
47 | LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceEventType.Information, format, arguments)); | |
|
47 | LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Information, format, arguments)); | |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | /// <summary> |
|
51 | 51 | /// Writes a warning message. |
|
52 | 52 | /// </summary> |
|
53 | 53 | /// <param name="format">Format.</param> |
|
54 | 54 | /// <param name="arguments">Arguments.</param> |
|
55 | 55 | [Conditional("TRACE")] |
|
56 | 56 | public static void TraceWarning(string format, params object[] arguments) { |
|
57 | LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceEventType.Warning, format, arguments)); | |
|
57 | LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Warning, format, arguments)); | |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | [Conditional("TRACE")] |
|
61 | 61 | public static void TraceError(string format, params object[] arguments) { |
|
62 | LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceEventType.Error, format, arguments)); | |
|
62 | LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Error, format, arguments)); | |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | [Conditional("TRACE")] |
|
66 | 66 | public static void TraceError(Exception err) { |
|
67 | 67 | TraceError("{0}", err); |
|
68 | 68 | } |
|
69 | 69 | } |
|
70 | 70 | } |
General Comments 3
ok, latest stable version should be in default
You need to be logged in to leave comments.
Login now