##// END OF EJS Templates
working version of the project
cin -
r200:71e543dbe65a v2
parent child
Show More
@@ -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(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
@@ -1,70 +1,70
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 /// <summary>
9 /// <summary>
10 /// This class is used to trace a logical flow of the application, it publishes events to the default <see cref="TraceEvent"/> channel.
10 /// This class is used to trace a logical flow of the application, it publishes events to the default <see cref="TraceEvent"/> channel.
11 /// </summary>
11 /// </summary>
12 public static class TraceLog {
12 public static class TraceLog {
13 /// <summary>
13 /// <summary>
14 /// Starts the logical operation nested to the current operation nested to the current one.
14 /// Starts the logical operation nested to the current operation nested to the current one.
15 /// </summary>
15 /// </summary>
16 [Conditional("TRACE")]
16 [Conditional("TRACE")]
17 public static void StartLogicalOperation() {
17 public static void StartLogicalOperation() {
18 TraceContext.Instance.StartLogicalOperation();
18 TraceContext.Instance.StartLogicalOperation();
19
19
20 }
20 }
21
21
22 /// <summary>
22 /// <summary>
23 /// Starts the logical operation with the specified name, this name is usefull in logs.
23 /// Starts the logical operation with the specified name, this name is usefull in logs.
24 /// </summary>
24 /// </summary>
25 /// <param name="name">Name.</param>
25 /// <param name="name">Name.</param>
26 [Conditional("TRACE")]
26 [Conditional("TRACE")]
27 public static void StartLogicalOperation(string name) {
27 public static void StartLogicalOperation(string name) {
28 TraceContext.Instance.StartLogicalOperation(name);
28 TraceContext.Instance.StartLogicalOperation(name);
29 }
29 }
30
30
31 /// <summary>
31 /// <summary>
32 /// Ends the logical operation and restores the previous one.
32 /// Ends the logical operation and restores the previous one.
33 /// </summary>
33 /// </summary>
34 [Conditional("TRACE")]
34 [Conditional("TRACE")]
35 public static void EndLogicalOperation() {
35 public static void EndLogicalOperation() {
36 var op = TraceContext.Instance.EndLogicalOperation();
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 /// <summary>
40 /// <summary>
41 /// Writes an informational message.
41 /// Writes an informational message.
42 /// </summary>
42 /// </summary>
43 /// <param name="format">Format.</param>
43 /// <param name="format">Format.</param>
44 /// <param name="arguments">Arguments.</param>
44 /// <param name="arguments">Arguments.</param>
45 [Conditional("TRACE")]
45 [Conditional("TRACE")]
46 public static void TraceInformation(string format, params object[] arguments) {
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 /// <summary>
50 /// <summary>
51 /// Writes a warning message.
51 /// Writes a warning message.
52 /// </summary>
52 /// </summary>
53 /// <param name="format">Format.</param>
53 /// <param name="format">Format.</param>
54 /// <param name="arguments">Arguments.</param>
54 /// <param name="arguments">Arguments.</param>
55 [Conditional("TRACE")]
55 [Conditional("TRACE")]
56 public static void TraceWarning(string format, params object[] arguments) {
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 [Conditional("TRACE")]
60 [Conditional("TRACE")]
61 public static void TraceError(string format, params object[] arguments) {
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 [Conditional("TRACE")]
65 [Conditional("TRACE")]
66 public static void TraceError(Exception err) {
66 public static void TraceError(Exception err) {
67 TraceError("{0}", err);
67 TraceError("{0}", err);
68 }
68 }
69 }
69 }
70 }
70 }
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now