##// END OF EJS Templates
improved tracing, TextListenerBase can be bound to logical operation scope.
cin -
r43:7c2369f580b8 default
parent child
Show More
@@ -8,6 +8,16 namespace Implab.Diagnostics {
8 8
9 9 static readonly object _consoleLock = new object();
10 10
11 public ConsoleTraceListener()
12 : base(true) {
13
14 }
15
16 public ConsoleTraceListener(bool local)
17 : base(local) {
18
19 }
20
11 21 protected override void WriteEntry(TraceContext context, EventText text) {
12 22 var msg = new StringBuilder();
13 23
@@ -8,7 +8,7 namespace Implab.Diagnostics {
8 8 public class TextFileListener: TextListenerBase {
9 9 readonly TextWriter m_textWriter;
10 10
11 public TextFileListener(string fileName) {
11 public TextFileListener(string fileName) : base(true) {
12 12 m_textWriter = File.CreateText(fileName);
13 13
14 14 m_textWriter.WriteLine("LOG {0}", DateTime.Now);
@@ -7,9 +7,15 namespace Implab.Diagnostics {
7 7 public abstract class TextListenerBase : ServiceLocator, IEventTextFormatter<object>, IEventTextFormatter<TraceEvent> {
8 8
9 9 readonly Dictionary<object, Action> m_subscriptions = new Dictionary<object, Action>();
10 readonly LogicalOperation m_boundOperation;
11 readonly int m_baseIndent;
10 12
11 protected TextListenerBase() {
13 protected TextListenerBase(bool local) {
12 14 Register(this);
15 if (local) {
16 m_boundOperation = TraceContext.Current.CurrentOperation;
17 m_baseIndent = Math.Max(0, m_boundOperation.Level - 1);
18 }
13 19 }
14 20
15 21 public void Subscribe(Type eventType) {
@@ -32,7 +38,12 namespace Implab.Diagnostics {
32 38 var formatter = GetService<IEventTextFormatter<TEvent>>();
33 39
34 40 EventHandler<ValueEventArgs<TEvent>> handler = (sender, args) => {
35 WriteEntry((TraceContext)sender, formatter.Format((TraceContext)sender, args.Value));
41 TraceContext context = (TraceContext)sender;
42 var text = formatter.Format(context, args.Value);
43 text.indent -= m_baseIndent;
44
45 if (IsRelated(context.CurrentOperation))
46 WriteEntry(context, text);
36 47 };
37 48
38 49 if (m_subscriptions.ContainsKey(channel))
@@ -48,6 +59,15 namespace Implab.Diagnostics {
48 59 }
49 60 }
50 61
62 public bool IsRelated(LogicalOperation op) {
63 if (m_boundOperation == null)
64 return true;
65
66 while (op != m_boundOperation && op.Level > m_boundOperation.Level)
67 op = op.Parent;
68 return op == m_boundOperation;
69 }
70
51 71 public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) {
52 72 if (channel == null)
53 73 throw new ArgumentNullException("channel");
@@ -92,6 +92,7 namespace Implab.Diagnostics {
92 92 try {
93 93 action();
94 94 } finally {
95 _current.EndAllOperations();
95 96 _current = old;
96 97 }
97 98 }
@@ -156,6 +157,14 namespace Implab.Diagnostics {
156 157 }
157 158 }
158 159
160 /// <summary>
161 /// Заврешает все начатые в этом контексте операции
162 /// </summary>
163 public void EndAllOperations() {
164 while (m_bound != m_currentOperation)
165 EndLogicalOperation();
166 }
167
159 168 void LogEvent(TraceEventType type, string format, params object[] args) {
160 169 LogChannel<TraceEvent>.Default.LogEvent(this, TraceEvent.Create(type, format, args));
161 170 }
@@ -25,7 +25,7 namespace Implab.Diagnostics {
25 25 }
26 26
27 27 public static TraceEvent Create(TraceEventType type, string format, params object[] args) {
28 return new TraceEvent(type, String.Format(format ?? String.Empty, args));
28 return new TraceEvent(type, format == null ? String.Empty : String.Format(format, args));
29 29 }
30 30 }
31 31 }
1 NO CONTENT: file was removed, binary diff hidden
General Comments 0
You need to be logged in to leave comments. Login now