@@ -0,0 +1,15 | |||||
|
1 | namespace Implab.Diagnostics { | |||
|
2 | public static class Extensions { | |||
|
3 | public static IPromise<T> EndLogicalOperation<T>(this IPromise<T> promise) { | |||
|
4 | Safe.ArgumentNotNull(promise, "promise"); | |||
|
5 | var op = TraceContext.Instance.DetachLogicalOperation(); | |||
|
6 | ||||
|
7 | return promise.Anyway(() => { | |||
|
8 | TraceContext.Instance.EnterLogicalOperation(op,true); | |||
|
9 | TraceLog.EndLogicalOperation(); | |||
|
10 | TraceContext.Instance.Leave(); | |||
|
11 | }); | |||
|
12 | } | |||
|
13 | } | |||
|
14 | } | |||
|
15 |
@@ -0,0 +1,24 | |||||
|
1 | using System; | |||
|
2 | ||||
|
3 | namespace Implab.Diagnostics { | |||
|
4 | public class LogEventArgs : EventArgs { | |||
|
5 | public int ThreadId { | |||
|
6 | get; | |||
|
7 | private set; | |||
|
8 | } | |||
|
9 | public LogicalOperation Operation { | |||
|
10 | get; | |||
|
11 | private set; | |||
|
12 | } | |||
|
13 | public int OperationTimeOffset { | |||
|
14 | get; | |||
|
15 | private set; | |||
|
16 | } | |||
|
17 | public LogEventArgs(int threadId, LogicalOperation operation, int timeOffset) { | |||
|
18 | ThreadId = threadId; | |||
|
19 | Operation = operation; | |||
|
20 | OperationTimeOffset = timeOffset; | |||
|
21 | } | |||
|
22 | } | |||
|
23 | } | |||
|
24 |
@@ -0,0 +1,13 | |||||
|
1 | namespace Implab.Diagnostics { | |||
|
2 | public class LogEventArgs<TEvent> : LogEventArgs { | |||
|
3 | public TEvent Value { | |||
|
4 | get; | |||
|
5 | private set; | |||
|
6 | } | |||
|
7 | ||||
|
8 | public LogEventArgs(TEvent value, int threadId, LogicalOperation operation, int timeOffset) : base(threadId, operation, timeOffset) { | |||
|
9 | Value = value; | |||
|
10 | } | |||
|
11 | } | |||
|
12 | } | |||
|
13 |
@@ -0,0 +1,49 | |||||
|
1 | namespace Implab.Diagnostics { | |||
|
2 | struct OperationContext { | |||
|
3 | readonly LogicalOperation m_initial; | |||
|
4 | public readonly static OperationContext EMPTY = new OperationContext(LogicalOperation.EMPTY, false); | |||
|
5 | LogicalOperation m_current; | |||
|
6 | readonly bool m_ownership; | |||
|
7 | ||||
|
8 | public OperationContext(LogicalOperation operation, bool ownership) { | |||
|
9 | Safe.ArgumentNotNull(operation, "operation"); | |||
|
10 | ||||
|
11 | m_initial = operation; | |||
|
12 | m_current = operation; | |||
|
13 | m_ownership = ownership; | |||
|
14 | } | |||
|
15 | ||||
|
16 | public LogicalOperation CurrentOperation { | |||
|
17 | get { return m_current; } | |||
|
18 | } | |||
|
19 | ||||
|
20 | public void BeginLogicalOperation(string name) { | |||
|
21 | m_current = new LogicalOperation(name, m_current); | |||
|
22 | } | |||
|
23 | ||||
|
24 | public LogicalOperation DetachLogicalOperation() { | |||
|
25 | var detached = m_current; | |||
|
26 | if (m_current != LogicalOperation.EMPTY) { | |||
|
27 | if (m_current != m_initial) | |||
|
28 | m_current = m_current.Parent; | |||
|
29 | else if (m_ownership) | |||
|
30 | m_current = LogicalOperation.EMPTY; | |||
|
31 | else | |||
|
32 | detached = LogicalOperation.EMPTY; | |||
|
33 | } | |||
|
34 | TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context"); | |||
|
35 | return detached; | |||
|
36 | } | |||
|
37 | ||||
|
38 | public void EndLogicalOperation() { | |||
|
39 | if (m_current != m_initial) { | |||
|
40 | m_current = m_current.Parent; | |||
|
41 | } else if (m_current != null && m_ownership) { | |||
|
42 | m_current = null; | |||
|
43 | } else { | |||
|
44 | TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context"); | |||
|
45 | } | |||
|
46 | } | |||
|
47 | } | |||
|
48 | } | |||
|
49 |
@@ -107,11 +107,11 namespace Implab.Diagnostics.Interactive | |||||
107 | base.Dispose(disposing); |
|
107 | base.Dispose(disposing); | |
108 | } |
|
108 | } | |
109 |
|
109 | |||
110 |
protected override void WriteEntry( |
|
110 | protected override void WriteEntry(LogEventArgs args, EventText text, string channel) { | |
111 | var item = new TraceViewItem { |
|
111 | var item = new TraceViewItem { | |
112 | Indent = text.indent, |
|
112 | Indent = text.indent, | |
113 | Message = text.content, |
|
113 | Message = text.content, | |
114 |
Thread = |
|
114 | Thread = args.ThreadId, | |
115 | Channel = channel, |
|
115 | Channel = channel, | |
116 | Timestamp = Environment.TickCount |
|
116 | Timestamp = Environment.TickCount | |
117 | }; |
|
117 | }; |
@@ -18,16 +18,16 namespace Implab.Diagnostics { | |||||
18 |
|
18 | |||
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 |
protected override void WriteEntry( |
|
21 | protected override void WriteEntry(LogEventArgs args, EventText text, string channel) { | |
22 | var msg = new StringBuilder(); |
|
22 | var msg = new StringBuilder(); | |
23 |
|
23 | |||
24 | for (int i = 0; i < text.indent; i++) |
|
24 | for (int i = 0; i < text.indent; i++) | |
25 | msg.Append(" "); |
|
25 | msg.Append(" "); | |
26 |
msg.AppendFormat("[{0}]:{1}: {2}", |
|
26 | msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, channel, text.content); | |
27 |
|
27 | |||
28 | lock (_consoleLock) { |
|
28 | lock (_consoleLock) { | |
29 |
Console.ForegroundColor = (ConsoleColor)( |
|
29 | Console.ForegroundColor = (ConsoleColor)(args.ThreadId % 15 + 1); | |
30 |
Console.WriteLine(msg |
|
30 | Console.WriteLine(msg); | |
31 | } |
|
31 | } | |
32 | } |
|
32 | } | |
33 | } |
|
33 | } |
@@ -1,10 +1,5 | |||||
1 | using System; |
|
1 | namespace Implab.Diagnostics { | |
2 | using System.Collections.Generic; |
|
|||
3 | using System.Linq; |
|
|||
4 | using System.Text; |
|
|||
5 |
|
||||
6 | namespace Implab.Diagnostics { |
|
|||
7 | public interface IEventTextFormatter<in TEvent> { |
|
2 | public interface IEventTextFormatter<in TEvent> { | |
8 |
EventText Format( |
|
3 | EventText Format(LogEventArgs args, TEvent data); | |
9 | } |
|
4 | } | |
10 | } |
|
5 | } |
@@ -27,8 +27,8 namespace Implab.Diagnostics { | |||||
27 | /// <summary> |
|
27 | /// <summary> | |
28 | /// Π‘ΠΎΠ±ΡΡΠΈΠ΅ ΠΏΠΎΡΠ²Π»Π΅Π½ΠΈΠ΅ Π½ΠΎΠ²ΠΎΠΉ Π·Π°ΠΏΠΈΡΠΈ Π² ΠΆΡΡΠ½Π°Π»Π΅, Π½Π° ΡΡΠΎ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΡΠ²Π°ΡΡΡΡ ΡΠ»ΡΡΠ°ΡΠ΅Π»ΠΈ. |
|
28 | /// Π‘ΠΎΠ±ΡΡΠΈΠ΅ ΠΏΠΎΡΠ²Π»Π΅Π½ΠΈΠ΅ Π½ΠΎΠ²ΠΎΠΉ Π·Π°ΠΏΠΈΡΠΈ Π² ΠΆΡΡΠ½Π°Π»Π΅, Π½Π° ΡΡΠΎ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΡΠ²Π°ΡΡΡΡ ΡΠ»ΡΡΠ°ΡΠ΅Π»ΠΈ. | |
29 | /// </summary> |
|
29 | /// </summary> | |
30 |
public event EventHandler< |
|
30 | public event EventHandler<LogEventArgs<TEvent>> Events; | |
31 |
|
31 | |||
32 | /// <summary> |
|
32 | /// <summary> | |
33 | /// ΠΠΌΡ ΠΊΠ°Π½Π°Π»Π°, ΠΏΠΎΠ»Π΅Π·Π½ΠΎ Π΄Π»Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π² ΠΆΡΡΠ½Π°Π»Π΅ |
|
33 | /// ΠΠΌΡ ΠΊΠ°Π½Π°Π»Π°, ΠΏΠΎΠ»Π΅Π·Π½ΠΎ Π΄Π»Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π² ΠΆΡΡΠ½Π°Π»Π΅ | |
34 | /// </summary> |
|
34 | /// </summary> | |
@@ -63,19 +63,18 namespace Implab.Diagnostics { | |||||
63 | /// </remarks> |
|
63 | /// </remarks> | |
64 | public void LogEvent(TEvent data) { |
|
64 | public void LogEvent(TEvent data) { | |
65 | var t = Events; |
|
65 | var t = Events; | |
66 | if (t!= null) |
|
66 | if (t != null) { | |
67 | t(TraceContext.Current,new ValueEventArgs<TEvent>(data)); |
|
67 | var traceContext = TraceContext.Instance; | |
68 | } |
|
68 | t( | |
69 |
|
69 | this, | ||
70 | /// <summary> |
|
70 | new LogEventArgs<TEvent>( | |
71 | /// ΠΡΠΏΡΠ°Π²Π»ΡΠ΅Ρ Π·Π°ΠΏΠΈΡΡ ΠΆΡΡΠ½Π°Π»Π° ΡΠ΅ΡΠ΅Π· ΠΊΠ°Π½Π°Π» ΠΏΠΎΠ΄ΠΏΠΈΡΡΠΈΠΊΠ°ΠΌ. |
|
71 | data, | |
72 | /// </summary> |
|
72 | traceContext.ThreadId, | |
73 | /// <param name="data">ΠΠ°ΠΏΠΈΡΡ ΠΆΡΡΠ½Π°Π»Π°.</param> |
|
73 | traceContext.CurrentOperation, | |
74 | /// <param name="context">ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΎΡ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ°ΡΡΡΠ»Π°Π΅ΡΡΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅/</param> |
|
74 | traceContext.CurrentOperation.Duration | |
75 | public void LogEvent(TraceContext context,TEvent data) { |
|
75 | ) | |
76 |
|
|
76 | ); | |
77 |
|
|
77 | } | |
78 | t(context, new ValueEventArgs<TEvent>(data)); |
|
|||
79 | } |
|
78 | } | |
80 | } |
|
79 | } | |
81 | } |
|
80 | } |
@@ -1,11 +1,9 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
|||
3 | using System.Linq; |
|
|||
4 | using System.Text; |
|
|||
5 | using System.Threading.Tasks; |
|
|||
6 |
|
2 | |||
7 | namespace Implab.Diagnostics { |
|
3 | namespace Implab.Diagnostics { | |
8 | public class LogicalOperation { |
|
4 | public class LogicalOperation { | |
|
5 | public static readonly LogicalOperation EMPTY = new LogicalOperation("__EMPTY__", null); | |||
|
6 | ||||
9 | readonly LogicalOperation m_parent; |
|
7 | readonly LogicalOperation m_parent; | |
10 | readonly string m_name; |
|
8 | readonly string m_name; | |
11 | readonly int m_level; |
|
9 | readonly int m_level; |
@@ -1,7 +1,5 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
|||
3 | using System.IO; |
|
2 | using System.IO; | |
4 | using System.Linq; |
|
|||
5 | using System.Text; |
|
3 | using System.Text; | |
6 |
|
4 | |||
7 | namespace Implab.Diagnostics { |
|
5 | namespace Implab.Diagnostics { | |
@@ -16,16 +14,16 namespace Implab.Diagnostics { | |||||
16 | Register(this); |
|
14 | Register(this); | |
17 | } |
|
15 | } | |
18 |
|
16 | |||
19 |
protected override void WriteEntry( |
|
17 | protected override void WriteEntry(LogEventArgs args, EventText text, string channel) { | |
20 | var msg = new StringBuilder(); |
|
18 | var msg = new StringBuilder(); | |
21 | for (int i = 0; i < text.indent; i++) |
|
19 | for (int i = 0; i < text.indent; i++) | |
22 | msg.Append(" "); |
|
20 | msg.Append(" "); | |
23 |
msg.AppendFormat("[{0}]:{1}: {2}", |
|
21 | msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, channel, text.content); | |
24 |
|
22 | |||
25 | lock (m_textWriter) { |
|
23 | lock (m_textWriter) { | |
26 | if (!IsDisposed) { |
|
24 | if (!IsDisposed) { | |
27 | // ΡΡΡ Π³Π°ΡΠ°Π½ΡΠΈΡΠΎΠ²Π°Π½ΠΎ Π΅ΡΠ΅ Π½Π΅ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ m_textWriter |
|
25 | // ΡΡΡ Π³Π°ΡΠ°Π½ΡΠΈΡΠΎΠ²Π°Π½ΠΎ Π΅ΡΠ΅ Π½Π΅ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ m_textWriter | |
28 |
m_textWriter.WriteLine(msg |
|
26 | m_textWriter.WriteLine(msg); | |
29 | m_textWriter.Flush(); |
|
27 | m_textWriter.Flush(); | |
30 | } |
|
28 | } | |
31 | } |
|
29 | } |
@@ -13,7 +13,7 namespace Implab.Diagnostics { | |||||
13 | protected TextListenerBase(bool global) { |
|
13 | protected TextListenerBase(bool global) { | |
14 | Register(this); |
|
14 | Register(this); | |
15 | if (!global) { |
|
15 | if (!global) { | |
16 |
m_boundOperation = TraceContext. |
|
16 | m_boundOperation = TraceContext.Instance.CurrentOperation; | |
17 | m_baseIndent = Math.Max(0, m_boundOperation.Level - 1); |
|
17 | m_baseIndent = Math.Max(0, m_boundOperation.Level - 1); | |
18 | } |
|
18 | } | |
19 | } |
|
19 | } | |
@@ -38,13 +38,12 namespace Implab.Diagnostics { | |||||
38 | var formatter = GetService<IEventTextFormatter<TEvent>>(); |
|
38 | var formatter = GetService<IEventTextFormatter<TEvent>>(); | |
39 | var channelName = channel.Name; |
|
39 | var channelName = channel.Name; | |
40 |
|
40 | |||
41 |
EventHandler< |
|
41 | EventHandler<LogEventArgs<TEvent>> handler = (sender, args) => { | |
42 | TraceContext context = (TraceContext)sender; |
|
42 | var text = formatter.Format(args, args.Value); | |
43 | var text = formatter.Format(context, args.Value); |
|
|||
44 | text.indent -= m_baseIndent; |
|
43 | text.indent -= m_baseIndent; | |
45 |
|
44 | |||
46 |
if (IsRelated( |
|
45 | if (IsRelated(args.Operation)) | |
47 |
WriteEntry( |
|
46 | WriteEntry(args, text, channelName); | |
48 | }; |
|
47 | }; | |
49 |
|
48 | |||
50 | if (m_subscriptions.ContainsKey(channel)) |
|
49 | if (m_subscriptions.ContainsKey(channel)) | |
@@ -97,19 +96,19 namespace Implab.Diagnostics { | |||||
97 | /// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠ·Π²Π°ΡΡΡΡ ΠΈΠ· ΡΠ°Π·Π½ΡΡ ΠΏΠΎΡΠΎΠΊΠΎΠ² ΠΎΠ΄Π½ΠΎΠ²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎ. ΠΠΎΠ·ΠΌΠΎΠΆΠ½Π° ΡΠΈΡΡΠ°ΡΠΈΡ, ΠΊΠΎΠ³Π΄Π° |
|
96 | /// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠ·Π²Π°ΡΡΡΡ ΠΈΠ· ΡΠ°Π·Π½ΡΡ ΠΏΠΎΡΠΎΠΊΠΎΠ² ΠΎΠ΄Π½ΠΎΠ²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎ. ΠΠΎΠ·ΠΌΠΎΠΆΠ½Π° ΡΠΈΡΡΠ°ΡΠΈΡ, ΠΊΠΎΠ³Π΄Π° | |
98 | /// Π΄Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΡΠΆΠ΅ ΠΏΠΎΡΠ»Π΅ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ ΠΎΠΎΠ±ΡΠ΅ΠΊΡΠ° ΠΌΠ΅ΡΠΎΠ΄ΠΎΠΌ <see cref="Dispose()"/>. |
|
97 | /// Π΄Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΡΠΆΠ΅ ΠΏΠΎΡΠ»Π΅ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ ΠΎΠΎΠ±ΡΠ΅ΠΊΡΠ° ΠΌΠ΅ΡΠΎΠ΄ΠΎΠΌ <see cref="Dispose()"/>. | |
99 | /// </remarks> |
|
98 | /// </remarks> | |
100 | /// <param name="context">ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ.</param> |
|
|||
101 | /// <param name="text">Π’Π΅ΠΊΡΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ.</param> |
|
99 | /// <param name="text">Π’Π΅ΠΊΡΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ.</param> | |
102 | protected abstract void WriteEntry(TraceContext context, EventText text, string channel); |
|
100 | /// <param name = "channel"></param> | |
|
101 | protected abstract void WriteEntry(LogEventArgs args, EventText text, string channel); | |||
103 |
|
102 | |||
104 |
public EventText Format( |
|
103 | public EventText Format(LogEventArgs args, object data) { | |
105 | return new EventText { |
|
104 | return new EventText { | |
106 |
indent = |
|
105 | indent = args.Operation.Level, | |
107 | content = data.ToString() |
|
106 | content = data.ToString() | |
108 | }; |
|
107 | }; | |
109 | } |
|
108 | } | |
110 |
|
109 | |||
111 |
public EventText Format( |
|
110 | public EventText Format(LogEventArgs args, TraceEvent data) { | |
112 |
var level = |
|
111 | var level = args.Operation.Level; | |
113 | if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted) |
|
112 | if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted) | |
114 | level--; |
|
113 | level--; | |
115 |
|
114 |
@@ -1,238 +1,79 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
3 |
using System. |
|
3 | using System.Threading; | |
4 | using System.Text; |
|
4 | ||
5 | using System.Threading; |
|
5 | namespace Implab.Diagnostics { | |
6 | using System.Threading.Tasks; |
|
6 | /// <summary> | |
7 |
|
7 | /// Trace context is bound to the specific thread, each thread has it's own ThreadContext. | ||
8 | namespace Implab.Diagnostics { |
|
8 | /// </summary> | |
9 |
/// < |
|
9 | /// <remarks> | |
10 | /// ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΏΡΠΈΠ²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΠΊ ΠΏΠΎΡΠΎΠΊΡ ΠΈ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ Π² ΡΠ΅Π±Π΅ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΡΡΠ΅ΠΊΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. |
|
10 | /// ThreadContext manages relations between logical operations and threads. | |
11 |
/// </ |
|
11 | /// </remarks> | |
12 | /// <remarks> |
|
12 | public class TraceContext { | |
13 | /// ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ ΡΠ»ΡΡΠ°ΡΠ΅Π»ΡΠΌ ΡΠΎΠ±ΡΡΠΈΠΉ Π΄Π»Ρ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ ΠΌΠ΅ΡΡΠ°, Π³Π΄Π΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ»ΠΎ ΡΠΎΠ±ΡΡΠΈΠ΅. |
|
13 | ||
14 | /// </remarks> |
|
14 | [ThreadStatic] | |
15 | public class TraceContext { |
|
15 | static TraceContext _instance; | |
16 | LogicalOperation m_currentOperation; |
|
16 | ||
17 | readonly LogicalOperation m_bound; |
|
17 | OperationContext m_current = OperationContext.EMPTY; | |
18 | readonly int m_threadId; |
|
18 | readonly Stack<OperationContext> m_stack = new Stack<OperationContext>(); | |
19 |
|
19 | readonly int m_threadId; | ||
20 | [ThreadStatic] |
|
20 | ||
21 |
static TraceContext |
|
21 | public static TraceContext Instance { | |
22 |
|
22 | get { | ||
23 | /// <summary> |
|
23 | if (_instance == null) | |
24 | /// Π’Π΅ΠΊΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ Π΄Π»Ρ ΠΏΠΎΡΠΎΠΊΠ°, ΡΠΎΠ·Π΄Π°Π΅ΡΡΡ Π°ΡΡΠΎΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΈ ΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ. |
|
24 | _instance = new TraceContext(); | |
25 | /// </summary> |
|
25 | return _instance; | |
26 | public static TraceContext Current { |
|
26 | } | |
27 |
|
|
27 | } | |
28 | if (_current == null) { |
|
28 | ||
29 | _current = new TraceContext(); |
|
29 | public TraceContext() { | |
30 | _current.LogEvent(TraceEventType.Created,"[{0}]", _current.ThreadId); |
|
30 | m_threadId = Thread.CurrentThread.ManagedThreadId; | |
31 |
|
|
31 | } | |
32 | return _current; |
|
32 | ||
33 | } |
|
33 | public int ThreadId { | |
34 | } |
|
34 | get { return m_threadId; } | |
35 |
|
35 | } | ||
36 | TraceContext(TraceContext context) |
|
36 | ||
37 | : this(context, false) { |
|
37 | public LogicalOperation CurrentOperation { | |
38 |
|
|
38 | get { | |
39 |
|
39 | return m_current.CurrentOperation; | ||
40 | TraceContext(TraceContext context, bool attach) { |
|
40 | } | |
41 | if (context == null) |
|
41 | } | |
42 | throw new ArgumentNullException("context"); |
|
42 | ||
43 |
|
43 | public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) { | ||
44 | m_currentOperation = context.CurrentOperation; |
|
44 | // TODO Emit event | |
45 | m_bound = attach ? context.BoundOperation : context.CurrentOperation; |
|
45 | m_stack.Push(m_current); | |
46 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
46 | m_current = new OperationContext(operation, takeOwnership); | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | TraceContext() { |
|
49 | public void StartLogicalOperation(string name) { | |
50 |
m_current |
|
50 | m_current.BeginLogicalOperation(name); | |
51 | m_bound = m_currentOperation; |
|
51 | } | |
52 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
52 | ||
53 | } |
|
53 | public void StartLogicalOperation() { | |
54 |
|
54 | // TODO Emit Event | ||
55 | /// <summary> |
|
55 | m_current.BeginLogicalOperation(String.Empty); | |
56 | /// ΠΡΠΈ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎΡΡΠΈ ΠΊΠΎΠΏΠΈΡΡΠ΅Ρ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΠ²ΡΠΎΠ²ΠΊΠΈ Π² ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΏΠΎΡΠΎΠΊ. |
|
56 | } | |
57 | /// </summary> |
|
57 | ||
58 | /// <param name="from">ΠΡΡ ΠΎΠ΄Π½ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ.</param> |
|
58 | public void EndLogicalOperation() { | |
59 | /// <remarks> |
|
59 | // TODO Emit event | |
60 | /// <para> |
|
60 | m_current.EndLogicalOperation(); | |
61 | /// ΠΠΎΠΏΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΡΠΎΠΈΡΡ ΠΎΠ΄ΠΈΡ Π·Π° ΡΡΠ΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΈ Π·Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ΠΌ Π΅Π³ΠΎ |
|
61 | } | |
62 | /// ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°. ΠΡΠΈ ΡΡΠΎΠΌ ΠΊΠΎΠΏΠΈΡΡΠ΅ΡΡΡ ΡΡΠ΅ΠΊ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π² Π½ΠΎΠ²ΠΎΠΌ |
|
62 | ||
63 | /// ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΡΠ°Π½Π΅Π΅ Π½Π°ΡΠ°ΡΡΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½Ρ. |
|
63 | public LogicalOperation DetachLogicalOperation() { | |
64 | /// </para> |
|
64 | // TODO Emit event | |
65 | /// <para> |
|
65 | return m_current.DetachLogicalOperation(); | |
66 | /// ΠΡΠ»ΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠ° ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΡΠΎΡΡΠΎΡΠ»Π°ΡΡ, ΡΠΎ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΡΠΎΠ±ΡΡΠΈΠ΅ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ <see cref="TraceEventType.Fork"/>. |
|
66 | } | |
67 | /// </para> |
|
67 | ||
68 | /// </remarks> |
|
68 | public void Leave() { | |
69 | public static void Fork(TraceContext from) { |
|
69 | // TODO Emit event | |
70 |
if (_cu |
|
70 | if (m_stack.Count > 0) | |
71 |
|
|
71 | m_current = m_stack.Pop(); | |
72 |
|
|
72 | else { | |
73 | var context = new TraceContext(from); |
|
73 | TraceLog.TraceWarning("Attemtp to leave the last operation context"); | |
74 | context.LogEvent(TraceEventType.Fork, "[{0}]-->[{1}]",from.ThreadId, context.ThreadId); |
|
74 | m_current = OperationContext.EMPTY; | |
75 | _current = context; |
|
75 | } | |
76 |
|
|
76 | } | |
77 | _current = new TraceContext(); |
|
77 | } | |
78 | } |
|
78 | } | |
79 | } |
|
79 | ||
80 |
|
||||
81 | /// <summary> |
|
|||
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)"/> |
|
|||
116 | /// </summary> |
|
|||
117 | /// <returns>ΠΠΎΠΏΠΈΡ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ.</returns> |
|
|||
118 | public static TraceContext Snapshot() { |
|
|||
119 | return _current == null ? new TraceContext() : new TraceContext(_current,false); |
|
|||
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; |
|
|||
130 | Fork(this); |
|
|||
131 | try { |
|
|||
132 | action(); |
|
|||
133 | } finally { |
|
|||
134 | if(_current != null) |
|
|||
135 | _current.EndAllOperations(); |
|
|||
136 | _current = old; |
|
|||
137 | } |
|
|||
138 | } |
|
|||
139 |
|
||||
140 | /// <summary> |
|
|||
141 | /// Π’Π΅ΠΊΡΡΠ°Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠ°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ. |
|
|||
142 | /// </summary> |
|
|||
143 | public LogicalOperation CurrentOperation { |
|
|||
144 | get { |
|
|||
145 | return m_currentOperation; |
|
|||
146 | } |
|
|||
147 | } |
|
|||
148 |
|
||||
149 | /// <summary> |
|
|||
150 | /// ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π½ΠΈΠΆΠ΅ ΠΊΠΎΡΠΎΡΠΎΠΉ Π½Π΅Π»ΡΠ·Ρ ΠΎΠΏΡΡΠΊΠ°ΡΡΡΡ Π² ΡΡΠ΅ΠΊΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, Ρ.Π΅. ΠΎΠ½Π° Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½Π° Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. |
|
|||
151 | /// </summary> |
|
|||
152 | public LogicalOperation BoundOperation { |
|
|||
153 | get { |
|
|||
154 | return m_bound; |
|
|||
155 | } |
|
|||
156 | } |
|
|||
157 |
|
||||
158 | /// <summary> |
|
|||
159 | /// ΠΠΎΡΠΎΠΊ, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΡΠΎΠ·Π΄Π°Π½ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ. |
|
|||
160 | /// </summary> |
|
|||
161 | public int ThreadId { |
|
|||
162 | get { |
|
|||
163 | return m_threadId; |
|
|||
164 | } |
|
|||
165 | } |
|
|||
166 |
|
||||
167 | /// <summary> |
|
|||
168 | /// ΠΠ°ΡΠΈΠ½Π°Π΅Ρ Π±Π΅Π·ΡΠΌΡΠ½Π½ΡΡ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ. |
|
|||
169 | /// </summary> |
|
|||
170 | public void StartLogicalOperation() { |
|
|||
171 | StartLogicalOperation(null); |
|
|||
172 | } |
|
|||
173 |
|
||||
174 | /// <summary> |
|
|||
175 | /// ΠΠ°ΡΠΈΠ½Π°Π΅Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Ρ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ. Π‘ΠΎΠ·Π΄Π°Π½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π΄ΠΎΠ±Π²Π°Π»Π΅Π½Π° Π² ΡΡΠ΅ΠΊ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°, Π·Π°ΡΠ΅ΠΌ Π±ΡΠ΄Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΎ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠ΅Π΅ ΡΠΎΠ±ΡΡΠΈΠ΅. |
|
|||
176 | /// </summary> |
|
|||
177 | /// <param name="name">ΠΠΌΡ Π½Π°ΡΠΈΠ½Π°Π΅ΠΌΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
|||
178 | public void StartLogicalOperation(string name) { |
|
|||
179 | m_currentOperation = new LogicalOperation(name, m_currentOperation); |
|
|||
180 | LogEvent(TraceEventType.OperationStarted, name); |
|
|||
181 | } |
|
|||
182 |
|
||||
183 | /// <summary> |
|
|||
184 | /// ΠΠ°ΠΊΠ°Π½ΡΠΈΠ²Π°Π΅Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π½Π°ΡΠ°ΡΡΡ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. ΠΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π½Π°ΡΠ°ΡΡΠ΅ Π² Π΄ΡΡΠ³ΠΈΡ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°Ρ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π·Π°ΠΊΠΎΠ½ΡΠ΅Π½Ρ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. |
|
|||
185 | /// </summary> |
|
|||
186 | /// <remarks> |
|
|||
187 | /// ΠΡΠΈ Π²ΡΠ·ΠΎΠ²Π΅ Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Π° ΡΠΎΠ·Π΄Π°Π΅ΡΡΡ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΆΡΡΠ½Π°Π»Π° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, Π»ΠΈΠ±ΠΎ ΠΎ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π»ΠΈΠ±ΠΎ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠΈ, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ Π΄Π°Π½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ |
|
|||
188 | /// Π½Π°ΡΠ°ΡΠ° Π² Π΄ΡΡΠ³ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. |
|
|||
189 | /// </remarks> |
|
|||
190 | public void EndLogicalOperation() { |
|
|||
191 | if (m_bound == m_currentOperation) { |
|
|||
192 | LogEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace"); |
|
|||
193 | } else { |
|
|||
194 | var op = m_currentOperation; |
|
|||
195 | LogEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration); |
|
|||
196 | m_currentOperation = m_currentOperation.Parent; |
|
|||
197 | } |
|
|||
198 | } |
|
|||
199 |
|
||||
200 | /// <summary> |
|
|||
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 |
|
||||
214 | public void BindLogicalOperationToPromise(IPromise promise) { |
|
|||
215 | Safe.ArgumentNotNull(promise, "promise"); |
|
|||
216 |
|
||||
217 | var ctx = DetachLogicalOperation(); |
|
|||
218 | promise.Anyway(() => { |
|
|||
219 | var old = _current; |
|
|||
220 | TraceContext.Attach(ctx); |
|
|||
221 | TraceContext.Current.EndLogicalOperation(); |
|
|||
222 | _current = old; |
|
|||
223 | }); |
|
|||
224 | } |
|
|||
225 |
|
||||
226 | /// <summary> |
|
|||
227 | /// ΠΠ°Π²ΡΠ΅ΡΠ°Π΅Ρ Π²ΡΠ΅ Π½Π°ΡΠ°ΡΡΠ΅ Π² ΡΡΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ |
|
|||
228 | /// </summary> |
|
|||
229 | public void EndAllOperations() { |
|
|||
230 | while (m_bound != m_currentOperation) |
|
|||
231 | EndLogicalOperation(); |
|
|||
232 | } |
|
|||
233 |
|
||||
234 | void LogEvent(TraceEventType type, string format, params object[] args) { |
|
|||
235 | LogChannel<TraceEvent>.Default.LogEvent(this, TraceEvent.Create(type, format, args)); |
|
|||
236 | } |
|
|||
237 | } |
|
|||
238 | } |
|
@@ -14,22 +14,17 namespace Implab.Diagnostics { | |||||
14 | public static class TraceLog { |
|
14 | public static class TraceLog { | |
15 | [Conditional("TRACE")] |
|
15 | [Conditional("TRACE")] | |
16 | public static void StartLogicalOperation() { |
|
16 | public static void StartLogicalOperation() { | |
17 |
TraceContext. |
|
17 | TraceContext.Instance.StartLogicalOperation(); | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | [Conditional("TRACE")] |
|
20 | [Conditional("TRACE")] | |
21 | public static void StartLogicalOperation(string name) { |
|
21 | public static void StartLogicalOperation(string name) { | |
22 |
TraceContext. |
|
22 | TraceContext.Instance.StartLogicalOperation(name); | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | [Conditional("TRACE")] |
|
25 | [Conditional("TRACE")] | |
26 | public static void EndLogicalOperation() { |
|
26 | public static void EndLogicalOperation() { | |
27 |
TraceContext. |
|
27 | TraceContext.Instance.EndLogicalOperation(); | |
28 | } |
|
|||
29 |
|
||||
30 | [Conditional("TRACE")] |
|
|||
31 | public static void BindLogicalOperationToPromise(IPromise promise) { |
|
|||
32 | TraceContext.Current.BindLogicalOperationToPromise(promise); |
|
|||
33 | } |
|
28 | } | |
34 |
|
29 | |||
35 | [Conditional("TRACE")] |
|
30 | [Conditional("TRACE")] |
@@ -80,7 +80,6 | |||||
80 | <Compile Include="Diagnostics\TextFileListener.cs" /> |
|
80 | <Compile Include="Diagnostics\TextFileListener.cs" /> | |
81 | <Compile Include="Diagnostics\TextListenerBase.cs" /> |
|
81 | <Compile Include="Diagnostics\TextListenerBase.cs" /> | |
82 | <Compile Include="Diagnostics\TraceLog.cs" /> |
|
82 | <Compile Include="Diagnostics\TraceLog.cs" /> | |
83 | <Compile Include="Diagnostics\TraceContext.cs" /> |
|
|||
84 | <Compile Include="Diagnostics\TraceEvent.cs" /> |
|
83 | <Compile Include="Diagnostics\TraceEvent.cs" /> | |
85 | <Compile Include="Diagnostics\TraceEventType.cs" /> |
|
84 | <Compile Include="Diagnostics\TraceEventType.cs" /> | |
86 | <Compile Include="Disposable.cs" /> |
|
85 | <Compile Include="Disposable.cs" /> | |
@@ -141,6 +140,11 | |||||
141 | <Compile Include="TransientPromiseException.cs" /> |
|
140 | <Compile Include="TransientPromiseException.cs" /> | |
142 | <Compile Include="SyncContextPromise.cs" /> |
|
141 | <Compile Include="SyncContextPromise.cs" /> | |
143 | <Compile Include="ObjectPool.cs" /> |
|
142 | <Compile Include="ObjectPool.cs" /> | |
|
143 | <Compile Include="Diagnostics\OperationContext.cs" /> | |||
|
144 | <Compile Include="Diagnostics\TraceContext.cs" /> | |||
|
145 | <Compile Include="Diagnostics\LogEventArgs.cs" /> | |||
|
146 | <Compile Include="Diagnostics\LogEventArgsT.cs" /> | |||
|
147 | <Compile Include="Diagnostics\Extensions.cs" /> | |||
144 | </ItemGroup> |
|
148 | </ItemGroup> | |
145 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
149 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | |
146 | <ItemGroup /> |
|
150 | <ItemGroup /> |
@@ -12,7 +12,7 namespace Implab.Parallels { | |||||
12 | readonly Action<TSrc> m_action; |
|
12 | readonly Action<TSrc> m_action; | |
13 | readonly TSrc[] m_source; |
|
13 | readonly TSrc[] m_source; | |
14 | readonly Promise<int> m_promise = new Promise<int>(); |
|
14 | readonly Promise<int> m_promise = new Promise<int>(); | |
15 |
readonly |
|
15 | readonly LogicalOperation m_logicalOperation; | |
16 |
|
16 | |||
17 | int m_pending; |
|
17 | int m_pending; | |
18 | int m_next; |
|
18 | int m_next; | |
@@ -23,7 +23,7 namespace Implab.Parallels { | |||||
23 | Debug.Assert(source != null); |
|
23 | Debug.Assert(source != null); | |
24 | Debug.Assert(action != null); |
|
24 | Debug.Assert(action != null); | |
25 |
|
25 | |||
26 |
m_ |
|
26 | m_logicalOperation = TraceContext.Instance.CurrentOperation; | |
27 | m_next = 0; |
|
27 | m_next = 0; | |
28 | m_source = source; |
|
28 | m_source = source; | |
29 | m_pending = source.Length; |
|
29 | m_pending = source.Length; | |
@@ -41,8 +41,12 namespace Implab.Parallels { | |||||
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | protected override void Worker() { |
|
43 | protected override void Worker() { | |
44 | TraceContext.Fork(m_traceContext); |
|
44 | TraceContext.Instance.EnterLogicalOperation(m_logicalOperation, false); | |
45 |
|
|
45 | try { | |
|
46 | base.Worker(); | |||
|
47 | } finally { | |||
|
48 | TraceContext.Instance.Leave(); | |||
|
49 | } | |||
46 | } |
|
50 | } | |
47 |
|
51 | |||
48 | protected override bool TryDequeue(out int unit) { |
|
52 | protected override bool TryDequeue(out int unit) { | |
@@ -67,7 +71,7 namespace Implab.Parallels { | |||||
67 | readonly TSrc[] m_source; |
|
71 | readonly TSrc[] m_source; | |
68 | readonly TDst[] m_dest; |
|
72 | readonly TDst[] m_dest; | |
69 | readonly Promise<TDst[]> m_promise = new Promise<TDst[]>(); |
|
73 | readonly Promise<TDst[]> m_promise = new Promise<TDst[]>(); | |
70 |
readonly |
|
74 | readonly LogicalOperation m_logicalOperation; | |
71 |
|
75 | |||
72 | int m_pending; |
|
76 | int m_pending; | |
73 | int m_next; |
|
77 | int m_next; | |
@@ -83,7 +87,7 namespace Implab.Parallels { | |||||
83 | m_dest = new TDst[source.Length]; |
|
87 | m_dest = new TDst[source.Length]; | |
84 | m_pending = source.Length; |
|
88 | m_pending = source.Length; | |
85 | m_transform = transform; |
|
89 | m_transform = transform; | |
86 |
m_ |
|
90 | m_logicalOperation = TraceContext.Instance.CurrentOperation; | |
87 |
|
91 | |||
88 | m_promise.Anyway(Dispose); |
|
92 | m_promise.Anyway(Dispose); | |
89 |
|
93 | |||
@@ -97,13 +101,17 namespace Implab.Parallels { | |||||
97 | } |
|
101 | } | |
98 |
|
102 | |||
99 | protected override void Worker() { |
|
103 | protected override void Worker() { | |
100 |
TraceContext. |
|
104 | TraceContext.Instance.EnterLogicalOperation(m_logicalOperation,false); | |
101 |
|
|
105 | try { | |
|
106 | base.Worker(); | |||
|
107 | } finally { | |||
|
108 | TraceContext.Instance.Leave(); | |||
|
109 | } | |||
102 | } |
|
110 | } | |
103 |
|
111 | |||
104 | protected override bool TryDequeue(out int unit) { |
|
112 | protected override bool TryDequeue(out int unit) { | |
105 | unit = Interlocked.Increment(ref m_next) - 1; |
|
113 | unit = Interlocked.Increment(ref m_next) - 1; | |
106 |
return unit |
|
114 | return unit < m_source.Length; | |
107 | } |
|
115 | } | |
108 |
|
116 | |||
109 | protected override void InvokeUnit(int unit) { |
|
117 | protected override void InvokeUnit(int unit) { |
@@ -14,15 +14,17 namespace Implab.Parallels { | |||||
14 |
|
14 | |||
15 | public static IPromise<T> Invoke<T>(Func<T> func) { |
|
15 | public static IPromise<T> Invoke<T>(Func<T> func) { | |
16 | var p = new Promise<T>(); |
|
16 | var p = new Promise<T>(); | |
17 |
var caller = TraceContext. |
|
17 | var caller = TraceContext.Instance.CurrentOperation; | |
18 |
|
18 | |||
19 | ThreadPool.QueueUserWorkItem(param => { |
|
19 | ThreadPool.QueueUserWorkItem(param => { | |
20 |
TraceContext. |
|
20 | TraceContext.Instance.EnterLogicalOperation(caller,false); | |
21 | try { |
|
21 | try { | |
22 | p.Resolve(func()); |
|
22 | p.Resolve(func()); | |
23 | } catch(Exception e) { |
|
23 | } catch(Exception e) { | |
24 | p.Reject(e); |
|
24 | p.Reject(e); | |
25 | } |
|
25 | } finally { | |
|
26 | TraceContext.Instance.Leave(); | |||
|
27 | } | |||
26 | }); |
|
28 | }); | |
27 |
|
29 | |||
28 | return p; |
|
30 | return p; | |
@@ -31,14 +33,16 namespace Implab.Parallels { | |||||
31 | public static IPromise<T> InvokeNewThread<T>(Func<T> func) { |
|
33 | public static IPromise<T> InvokeNewThread<T>(Func<T> func) { | |
32 | var p = new Promise<T>(); |
|
34 | var p = new Promise<T>(); | |
33 |
|
35 | |||
34 |
var caller = TraceContext. |
|
36 | var caller = TraceContext.Instance.CurrentOperation; | |
35 |
|
37 | |||
36 | var worker = new Thread(() => { |
|
38 | var worker = new Thread(() => { | |
37 |
TraceContext. |
|
39 | TraceContext.Instance.EnterLogicalOperation(caller,false); | |
38 | try { |
|
40 | try { | |
39 | p.Resolve(func()); |
|
41 | p.Resolve(func()); | |
40 | } catch (Exception e) { |
|
42 | } catch (Exception e) { | |
41 | p.Reject(e); |
|
43 | p.Reject(e); | |
|
44 | } finally { | |||
|
45 | TraceContext.Instance.Leave(); | |||
42 | } |
|
46 | } | |
43 | }); |
|
47 | }); | |
44 | worker.IsBackground = true; |
|
48 | worker.IsBackground = true; | |
@@ -51,15 +55,17 namespace Implab.Parallels { | |||||
51 | public static IPromise InvokeNewThread(Action func) { |
|
55 | public static IPromise InvokeNewThread(Action func) { | |
52 | var p = new Promise<object>(); |
|
56 | var p = new Promise<object>(); | |
53 |
|
57 | |||
54 |
var caller = TraceContext. |
|
58 | var caller = TraceContext.Instance.CurrentOperation; | |
55 |
|
59 | |||
56 | var worker = new Thread(() => { |
|
60 | var worker = new Thread(() => { | |
57 |
TraceContext. |
|
61 | TraceContext.Instance.EnterLogicalOperation(caller,false); | |
58 | try { |
|
62 | try { | |
59 | func(); |
|
63 | func(); | |
60 | p.Resolve(); |
|
64 | p.Resolve(); | |
61 | } catch (Exception e) { |
|
65 | } catch (Exception e) { | |
62 | p.Reject(e); |
|
66 | p.Reject(e); | |
|
67 | } finally { | |||
|
68 | TraceContext.Instance.Leave(); | |||
63 | } |
|
69 | } | |
64 | }); |
|
70 | }); | |
65 | worker.IsBackground = true; |
|
71 | worker.IsBackground = true; |
@@ -1,9 +1,5 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
|||
3 | using System.Linq; |
|
|||
4 | using System.Text; |
|
|||
5 | using System.Threading; |
|
2 | using System.Threading; | |
6 | using System.Diagnostics; |
|
|||
7 |
|
3 | |||
8 | namespace Implab.Parallels { |
|
4 | namespace Implab.Parallels { | |
9 | public abstract class DispatchPool<TUnit> : IDisposable { |
|
5 | public abstract class DispatchPool<TUnit> : IDisposable { | |
@@ -150,14 +146,13 namespace Implab.Parallels { | |||||
150 | protected bool StartWorker() { |
|
146 | protected bool StartWorker() { | |
151 | if (AllocateThreadSlot()) { |
|
147 | if (AllocateThreadSlot()) { | |
152 | // slot successfully allocated |
|
148 | // slot successfully allocated | |
153 |
var worker = new Thread( |
|
149 | var worker = new Thread(Worker); | |
154 | worker.IsBackground = true; |
|
150 | worker.IsBackground = true; | |
155 | worker.Start(); |
|
151 | worker.Start(); | |
156 |
|
152 | |||
157 | return true; |
|
153 | return true; | |
158 | } else { |
|
|||
159 | return false; |
|
|||
160 | } |
|
154 | } | |
|
155 | return false; | |||
161 | } |
|
156 | } | |
162 |
|
157 | |||
163 | protected abstract void InvokeUnit(TUnit unit); |
|
158 | protected abstract void InvokeUnit(TUnit unit); |
@@ -1,7 +1,4 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
|||
3 | using System.Linq; |
|
|||
4 | using System.Text; |
|
|||
5 | using System.Threading; |
|
2 | using System.Threading; | |
6 | using System.Diagnostics; |
|
3 | using System.Diagnostics; | |
7 | using Implab.Diagnostics; |
|
4 | using Implab.Diagnostics; | |
@@ -12,29 +9,24 namespace Implab.Parallels { | |||||
12 | MTQueue<Action> m_queue = new MTQueue<Action>(); |
|
9 | MTQueue<Action> m_queue = new MTQueue<Action>(); | |
13 | int m_queueLength = 0; |
|
10 | int m_queueLength = 0; | |
14 | readonly int m_threshold = 1; |
|
11 | readonly int m_threshold = 1; | |
15 | int m_workers = 0; |
|
|||
16 |
|
12 | |||
17 | public WorkerPool(int minThreads, int maxThreads, int threshold) |
|
13 | public WorkerPool(int minThreads, int maxThreads, int threshold) | |
18 | : base(minThreads, maxThreads) { |
|
14 | : base(minThreads, maxThreads) { | |
19 | m_threshold = threshold; |
|
15 | m_threshold = threshold; | |
20 | m_workers = minThreads; |
|
|||
21 | InitPool(); |
|
16 | InitPool(); | |
22 | } |
|
17 | } | |
23 |
|
18 | |||
24 | public WorkerPool(int minThreads, int maxThreads) : |
|
19 | public WorkerPool(int minThreads, int maxThreads) : | |
25 | base(minThreads, maxThreads) { |
|
20 | base(minThreads, maxThreads) { | |
26 | m_workers = minThreads; |
|
|||
27 | InitPool(); |
|
21 | InitPool(); | |
28 | } |
|
22 | } | |
29 |
|
23 | |||
30 | public WorkerPool(int threads) |
|
24 | public WorkerPool(int threads) | |
31 | : base(threads) { |
|
25 | : base(threads) { | |
32 | m_workers = threads; |
|
|||
33 | InitPool(); |
|
26 | InitPool(); | |
34 | } |
|
27 | } | |
35 |
|
28 | |||
36 | public WorkerPool() |
|
29 | public WorkerPool() { | |
37 | : base() { |
|
|||
38 | InitPool(); |
|
30 | InitPool(); | |
39 | } |
|
31 | } | |
40 |
|
32 | |||
@@ -46,16 +38,17 namespace Implab.Parallels { | |||||
46 |
|
38 | |||
47 | var promise = new Promise<T>(); |
|
39 | var promise = new Promise<T>(); | |
48 |
|
40 | |||
49 |
var |
|
41 | var lop = TraceContext.Instance.CurrentOperation; | |
50 |
|
42 | |||
51 | EnqueueTask(delegate() { |
|
43 | EnqueueTask(delegate() { | |
52 | caller.Invoke(delegate() { |
|
44 | TraceContext.Instance.EnterLogicalOperation(lop, false); | |
53 |
|
|
45 | try { | |
54 |
|
|
46 | promise.Resolve(task()); | |
55 |
|
|
47 | } catch (Exception e) { | |
56 |
|
|
48 | promise.Reject(e); | |
57 |
|
|
49 | } finally { | |
58 | }); |
|
50 | TraceContext.Instance.Leave(); | |
|
51 | } | |||
59 | }); |
|
52 | }); | |
60 |
|
53 | |||
61 | return promise; |
|
54 | return promise; |
General Comments 0
You need to be logged in to leave comments.
Login now