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