annotate Implab/Diagnostics/TextListenerBase.cs @ 44:e5ec543feee3

Рефакторинг, журналирование
author cin
date Wed, 16 Apr 2014 19:02:58 +0400
parents 7c2369f580b8
children b181f7bcb259
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
43
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
13 protected TextListenerBase(bool local) {
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
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
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>>();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
39
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
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
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
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
92 protected abstract void WriteEntry(TraceContext context, EventText text);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
93
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
94 public EventText Format(TraceContext context, object data) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
95 return new EventText {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
96 indent = context.CurrentOperation.Level,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
97 content = data.ToString()
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
98 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
99 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
100
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
101 public EventText Format(TraceContext context, TraceEvent data) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
102 var level = context.CurrentOperation.Level;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
103 if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
104 level--;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
105
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
106 return new EventText {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
107 indent = level,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
108 content = data.ToString()
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
109 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
110 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
111
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
112 protected override void Dispose(bool disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
113 if (disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
114 UnsubscribeAll();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
115 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
116 base.Dispose(disposing);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
117 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
118 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
119 }