annotate Implab/Diagnostics/ListenerBase.cs @ 197:86187b01c4e0

fixed: the error handler should not handle handlers errors
author cin
date Thu, 01 Sep 2016 10:43:31 +0300
parents 240aa6994018
children d45bdf510514
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
134
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
1 using System;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
2 using System.Collections.Generic;
152
240aa6994018 component model refactoring
cin
parents: 134
diff changeset
3 using Implab.Components;
134
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
4
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
5 namespace Implab.Diagnostics {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
6 public abstract class ListenerBase : ServiceLocator, ILogWriter<object>, ILogWriter<TraceEvent> {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
7
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
8 readonly Dictionary<object, Action> m_subscriptions = new Dictionary<object, Action>();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
9
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
10 protected ListenerBase() {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
11 Register(this);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
12 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
13
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
14 public void Subscribe(Type eventType) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
15 if (eventType == null)
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
16 throw new ArgumentNullException("eventType");
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
17 GetType().GetMethod("Subscribe", new Type[0]).MakeGenericMethod(eventType).Invoke(this, null);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
18 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
19
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
20 public void Subscribe<TEvent>() {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
21 Subscribe<TEvent>(LogChannel<TEvent>.Default);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
22 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
23
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
24 public void Subscribe<TEvent>(LogChannel<TEvent> channel) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
25 if (channel == null)
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
26 throw new ArgumentNullException("channel");
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
27
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
28 lock (m_subscriptions) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
29 AssertNotDisposed();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
30 if (m_subscriptions.ContainsKey(channel))
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
31 return;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
32
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
33 var writer = GetService<ILogWriter<TEvent>>();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
34
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
35 EventHandler<LogEventArgs<TEvent>> handler = (sender, args) => writer.Write(args,args.Value);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
36
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
37 channel.Events += handler;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
38
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
39 Action unsubscribe = () => {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
40 channel.Events -= handler;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
41 };
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
42
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
43 m_subscriptions.Add(channel, unsubscribe);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
44 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
45 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
46
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
47 public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
48 if (channel == null)
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
49 throw new ArgumentNullException("channel");
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
50
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
51 lock (m_subscriptions) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
52 Action subscription;
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
53 if (m_subscriptions.TryGetValue(channel, out subscription)) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
54 subscription();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
55 m_subscriptions.Remove(channel);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
56 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
57 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
58 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
59
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
60 public void UnsubscribeAll() {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
61 lock (m_subscriptions) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
62 foreach (var subscription in m_subscriptions.Values)
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
63 subscription();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
64 m_subscriptions.Clear();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
65 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
66 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
67
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
68 #region ILogWriter implementation
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
69 public abstract void Write(LogEventArgs args, object entry);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
70 #endregion
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
71
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
72 #region ILogWriter implementation
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
73 public virtual void Write(LogEventArgs args, TraceEvent entry) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
74 Write(args, (object)entry);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
75 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
76 #endregion
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
77
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
78
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
79 protected override void Dispose(bool disposing) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
80 base.Dispose(disposing);
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
81 if (disposing) {
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
82 UnsubscribeAll();
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
83 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
84 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
85 }
04d4c92d0f28 Improved logging
cin
parents:
diff changeset
86 }