annotate Implab/Diagnostics/TextListenerBase.cs @ 111:0fdaf280c797 v1

minor fix
author cin
date Wed, 19 Nov 2014 03:18:42 +0400
parents d9d794b61bb9
children 4c0e5ef99986
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) {
43
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
16 m_boundOperation = TraceContext.Current.CurrentOperation;
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
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
41 EventHandler<ValueEventArgs<TEvent>> handler = (sender, args) => {
43
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
42 TraceContext context = (TraceContext)sender;
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
43 var text = formatter.Format(context, args.Value);
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
44 text.indent -= m_baseIndent;
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
45
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
46 if (IsRelated(context.CurrentOperation))
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
47 WriteEntry(context, text, channelName);
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
48 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
49
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
50 if (m_subscriptions.ContainsKey(channel))
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
51 return;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
52
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
53 channel.Events += handler;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
54
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
55 Action unsubscribe = () => {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
56 channel.Events -= handler;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
57 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
58
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
59 m_subscriptions.Add(channel, unsubscribe);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
60 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
61 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
62
43
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
63 public bool IsRelated(LogicalOperation op) {
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
64 if (m_boundOperation == null)
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
65 return true;
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
66
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
67 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
68 op = op.Parent;
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
69 return op == m_boundOperation;
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
70 }
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
71
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
72 public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
73 if (channel == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
74 throw new ArgumentNullException("channel");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
75
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
76 lock (m_subscriptions) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
77 Action subscription;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
78 if (m_subscriptions.TryGetValue(channel, out subscription)) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
79 subscription();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
80 m_subscriptions.Remove(channel);
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
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
85 public void UnsubscribeAll() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
86 lock (m_subscriptions) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
87 foreach (var subscription in m_subscriptions.Values)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
88 subscription();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
89 m_subscriptions.Clear();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
90 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
91 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
92
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
93 /// <summary>
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
94 /// Вызывается для записи текста сообщения, в журнал.
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
95 /// </summary>
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
96 /// <remarks>
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
97 /// Данный метод может вызваться из разных потоков одновременно. Возможна ситуация, когда
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
98 /// данный метод вызывается уже после освобождения ообъекта методом <see cref="Dispose()"/>.
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
99 /// </remarks>
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
100 /// <param name="context">Контекст трассировки.</param>
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
101 /// <param name="text">Текст сообщения.</param>
48
d9d794b61bb9 Interactive tracing
cin
parents: 47
diff changeset
102 protected abstract void WriteEntry(TraceContext context, EventText text, string channel);
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
103
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
104 public EventText Format(TraceContext context, object data) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
105 return new EventText {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
106 indent = context.CurrentOperation.Level,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
107 content = data.ToString()
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
108 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
109 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
110
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
111 public EventText Format(TraceContext context, TraceEvent data) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
112 var level = context.CurrentOperation.Level;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
113 if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
114 level--;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
115
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
116 return new EventText {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
117 indent = level,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
118 content = data.ToString()
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
119 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
120 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
121
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
122 protected override void Dispose(bool disposing) {
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 43
diff changeset
123 base.Dispose(disposing);
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
124 if (disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
125 UnsubscribeAll();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
126 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
127 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
128 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
129 }