Auto status change to "Under Review"
@@ -1,74 +1,75 | |||
|
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 | public static class Trace<T> { |
|
10 | 10 | |
|
11 | 11 | readonly static LogChannel<TraceEvent> _channel = new LogChannel<TraceEvent>(typeof(T).Name); |
|
12 | 12 | |
|
13 | 13 | public static LogChannel<TraceEvent> Channel { |
|
14 | 14 | get { return _channel; } |
|
15 | 15 | } |
|
16 | 16 | |
|
17 | 17 | /// <summary> |
|
18 | 18 | /// Starts the logical operation nested to the current operation nested to the current one. |
|
19 | 19 | /// </summary> |
|
20 | 20 | [Conditional("TRACE")] |
|
21 | 21 | public static void StartLogicalOperation() { |
|
22 | 22 | TraceContext.Instance.StartLogicalOperation(); |
|
23 | 23 | |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | /// <summary> |
|
27 | 27 | /// Starts the logical operation with the specified name, this name is usefull in logs. |
|
28 | 28 | /// </summary> |
|
29 | 29 | /// <param name="name">Name.</param> |
|
30 | 30 | [Conditional("TRACE")] |
|
31 | 31 | public static void StartLogicalOperation(string name) { |
|
32 | Channel.LogEvent(new TraceEvent(TraceContext.Instance.CurrentOperation, TraceEventType.OperationStarted, name)); | |
|
32 | 33 | TraceContext.Instance.StartLogicalOperation(name); |
|
33 | 34 | } |
|
34 | 35 | |
|
35 | 36 | /// <summary> |
|
36 | 37 | /// Ends the logical operation and restores the previous one. |
|
37 | 38 | /// </summary> |
|
38 | 39 | [Conditional("TRACE")] |
|
39 | 40 | public static void EndLogicalOperation() { |
|
40 | 41 | var op = TraceContext.Instance.EndLogicalOperation(); |
|
41 |
|
|
|
42 | Channel.LogEvent(new TraceEvent(op, TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms", op.Name, op.Duration))); | |
|
42 | 43 | } |
|
43 | 44 | |
|
44 | 45 | /// <summary> |
|
45 | 46 | /// Writes an informational message. |
|
46 | 47 | /// </summary> |
|
47 | 48 | /// <param name="format">Format.</param> |
|
48 | 49 | /// <param name="arguments">Arguments.</param> |
|
49 | 50 | [Conditional("TRACE")] |
|
50 | 51 | public static void Log(string format, params object[] arguments) { |
|
51 | 52 | Channel.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Information, format, arguments)); |
|
52 | 53 | } |
|
53 | 54 | |
|
54 | 55 | /// <summary> |
|
55 | 56 | /// Writes a warning message. |
|
56 | 57 | /// </summary> |
|
57 | 58 | /// <param name="format">Format.</param> |
|
58 | 59 | /// <param name="arguments">Arguments.</param> |
|
59 | 60 | [Conditional("TRACE")] |
|
60 | 61 | public static void Warn(string format, params object[] arguments) { |
|
61 | 62 | Channel.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Warning, format, arguments)); |
|
62 | 63 | } |
|
63 | 64 | |
|
64 | 65 | [Conditional("TRACE")] |
|
65 | 66 | public static void Error(string format, params object[] arguments) { |
|
66 | 67 | Channel.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Error, format, arguments)); |
|
67 | 68 | } |
|
68 | 69 | |
|
69 | 70 | [Conditional("TRACE")] |
|
70 | 71 | public static void Error(Exception err) { |
|
71 | 72 | Error("{0}", err); |
|
72 | 73 | } |
|
73 | 74 | } |
|
74 | 75 | } |
@@ -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(TraceContext.Instance.CurrentOperation, 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 |
General Comments 3
ok, latest stable version should be in default
You need to be logged in to leave comments.
Login now