Mercurial > pub > ImplabNet
annotate Implab/Diagnostics/TextListenerBase.cs @ 44:e5ec543feee3
Рефакторинг, журналирование
author | cin |
---|---|
date | Wed, 16 Apr 2014 19:02:58 +0400 |
parents | 7c2369f580b8 |
children | b181f7bcb259 |
rev | line source |
---|---|
40 | 1 using System; |
2 using System.Collections.Generic; | |
3 using System.Linq; | |
4 using System.Text; | |
5 | |
6 namespace Implab.Diagnostics { | |
7 public abstract class TextListenerBase : ServiceLocator, IEventTextFormatter<object>, IEventTextFormatter<TraceEvent> { | |
8 | |
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 | 12 |
43
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
13 protected TextListenerBase(bool local) { |
40 | 14 Register(this); |
43
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
15 if (local) { |
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 | 19 } |
20 | |
21 public void Subscribe(Type eventType) { | |
22 if (eventType == null) | |
23 throw new ArgumentNullException("eventType"); | |
24 GetType().GetMethod("Subscribe", new Type[0]).MakeGenericMethod(eventType).Invoke(this, null); | |
25 } | |
26 | |
27 public void Subscribe<TEvent>() { | |
28 Subscribe<TEvent>(LogChannel<TEvent>.Default); | |
29 } | |
30 | |
31 public void Subscribe<TEvent>(LogChannel<TEvent> channel) { | |
32 if (channel == null) | |
33 throw new ArgumentNullException("channel"); | |
34 | |
35 lock (m_subscriptions) { | |
36 AssertNotDisposed(); | |
37 | |
38 var formatter = GetService<IEventTextFormatter<TEvent>>(); | |
39 | |
40 EventHandler<ValueEventArgs<TEvent>> handler = (sender, args) => { | |
43
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
41 TraceContext context = (TraceContext)sender; |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
42 var text = formatter.Format(context, args.Value); |
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 |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
45 if (IsRelated(context.CurrentOperation)) |
7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents:
40
diff
changeset
|
46 WriteEntry(context, text); |
40 | 47 }; |
48 | |
49 if (m_subscriptions.ContainsKey(channel)) | |
50 return; | |
51 | |
52 channel.Events += handler; | |
53 | |
54 Action unsubscribe = () => { | |
55 channel.Events -= handler; | |
56 }; | |
57 | |
58 m_subscriptions.Add(channel, unsubscribe); | |
59 } | |
60 } | |
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 | 71 public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) { |
72 if (channel == null) | |
73 throw new ArgumentNullException("channel"); | |
74 | |
75 lock (m_subscriptions) { | |
76 Action subscription; | |
77 if (m_subscriptions.TryGetValue(channel, out subscription)) { | |
78 subscription(); | |
79 m_subscriptions.Remove(channel); | |
80 } | |
81 } | |
82 } | |
83 | |
84 public void UnsubscribeAll() { | |
85 lock (m_subscriptions) { | |
86 foreach (var subscription in m_subscriptions.Values) | |
87 subscription(); | |
88 m_subscriptions.Clear(); | |
89 } | |
90 } | |
91 | |
92 protected abstract void WriteEntry(TraceContext context, EventText text); | |
93 | |
94 public EventText Format(TraceContext context, object data) { | |
95 return new EventText { | |
96 indent = context.CurrentOperation.Level, | |
97 content = data.ToString() | |
98 }; | |
99 } | |
100 | |
101 public EventText Format(TraceContext context, TraceEvent data) { | |
102 var level = context.CurrentOperation.Level; | |
103 if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted) | |
104 level--; | |
105 | |
106 return new EventText { | |
107 indent = level, | |
108 content = data.ToString() | |
109 }; | |
110 } | |
111 | |
112 protected override void Dispose(bool disposing) { | |
113 if (disposing) { | |
114 UnsubscribeAll(); | |
115 } | |
116 base.Dispose(disposing); | |
117 } | |
118 } | |
119 } |