annotate Implab/Diagnostics/TextListenerBase.cs @ 92:4c0e5ef99986 v2

rewritten tracing
author cin
date Wed, 22 Oct 2014 18:37:56 +0400
parents d9d794b61bb9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
1 using System;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
2 using System.Collections.Generic;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
3 using System.Linq;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
4 using System.Text;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
5
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
6 namespace Implab.Diagnostics {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
7 public abstract class TextListenerBase : ServiceLocator, IEventTextFormatter<object>, IEventTextFormatter<TraceEvent> {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
8
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
9 readonly Dictionary<object, Action> m_subscriptions = new Dictionary<object, Action>();
43
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
10 readonly LogicalOperation m_boundOperation;
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
11 readonly int m_baseIndent;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
12
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
13 protected TextListenerBase(bool global) {
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
14 Register(this);
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
15 if (!global) {
92
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
16 m_boundOperation = TraceContext.Instance.CurrentOperation;
43
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
17 m_baseIndent = Math.Max(0, m_boundOperation.Level - 1);
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
18 }
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
19 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
20
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
21 public void Subscribe(Type eventType) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
22 if (eventType == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
23 throw new ArgumentNullException("eventType");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
24 GetType().GetMethod("Subscribe", new Type[0]).MakeGenericMethod(eventType).Invoke(this, null);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
25 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
26
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
27 public void Subscribe<TEvent>() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
28 Subscribe<TEvent>(LogChannel<TEvent>.Default);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
29 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
30
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
31 public void Subscribe<TEvent>(LogChannel<TEvent> channel) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
32 if (channel == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
33 throw new ArgumentNullException("channel");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
34
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
35 lock (m_subscriptions) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
36 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
37
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
38 var formatter = GetService<IEventTextFormatter<TEvent>>();
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
39 var channelName = channel.Name;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
40
92
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
41 EventHandler<LogEventArgs<TEvent>> handler = (sender, args) => {
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
42 var text = formatter.Format(args, args.Value);
43
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
43 text.indent -= m_baseIndent;
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
44
92
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
45 if (IsRelated(args.Operation))
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
46 WriteEntry(args, text, channelName);
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
47 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
48
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
49 if (m_subscriptions.ContainsKey(channel))
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
50 return;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
51
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
52 channel.Events += handler;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
53
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
54 Action unsubscribe = () => {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
55 channel.Events -= handler;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
56 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
57
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
58 m_subscriptions.Add(channel, unsubscribe);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
59 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
60 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
61
43
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
62 public bool IsRelated(LogicalOperation op) {
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
63 if (m_boundOperation == null)
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
64 return true;
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
65
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
66 while (op != m_boundOperation && op.Level > m_boundOperation.Level)
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
67 op = op.Parent;
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
68 return op == m_boundOperation;
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
69 }
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
70
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
71 public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
72 if (channel == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
73 throw new ArgumentNullException("channel");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
74
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
75 lock (m_subscriptions) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
76 Action subscription;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
77 if (m_subscriptions.TryGetValue(channel, out subscription)) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
78 subscription();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
79 m_subscriptions.Remove(channel);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
80 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
81 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
82 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
83
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
84 public void UnsubscribeAll() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
85 lock (m_subscriptions) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
86 foreach (var subscription in m_subscriptions.Values)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
87 subscription();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
88 m_subscriptions.Clear();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
89 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
90 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
91
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
92 /// <summary>
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
93 /// Вызывается для записи текста сообщения, в журнал.
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
94 /// </summary>
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
95 /// <remarks>
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
96 /// Данный метод может вызваться из разных потоков одновременно. Возможна ситуация, когда
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
97 /// данный метод вызывается уже после освобождения ообъекта методом <see cref="Dispose()"/>.
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
98 /// </remarks>
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
99 /// <param name="text">Текст сообщения.</param>
92
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
100 /// <param name = "channel"></param>
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
101 protected abstract void WriteEntry(LogEventArgs args, EventText text, string channel);
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
102
92
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
103 public EventText Format(LogEventArgs args, object data) {
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
104 return new EventText {
92
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
105 indent = args.Operation.Level,
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
106 content = data.ToString()
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
107 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
108 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
109
92
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
110 public EventText Format(LogEventArgs args, TraceEvent data) {
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
111 var level = args.Operation.Level;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
112 if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
113 level--;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
114
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
115 return new EventText {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
116 indent = level,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
117 content = data.ToString()
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
118 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
119 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
120
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
121 protected override void Dispose(bool disposing) {
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
122 base.Dispose(disposing);
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
123 if (disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
124 UnsubscribeAll();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
125 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
126 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
127 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
128 }