Mercurial > pub > ImplabNet
comparison Implab/Diagnostics/ConsoleTraceListener.cs @ 40:fe33f4e02ad5
improved tracing
added text listeners (file,console)
author | cin |
---|---|
date | Tue, 15 Apr 2014 17:52:09 +0400 |
parents | 313f708a50e9 |
children | 7c2369f580b8 |
comparison
equal
deleted
inserted
replaced
39:6498078ae368 | 40:fe33f4e02ad5 |
---|---|
2 using System.Collections.Generic; | 2 using System.Collections.Generic; |
3 using System.Linq; | 3 using System.Linq; |
4 using System.Text; | 4 using System.Text; |
5 | 5 |
6 namespace Implab.Diagnostics { | 6 namespace Implab.Diagnostics { |
7 public class ConsoleTraceListener { | 7 public class ConsoleTraceListener: TextListenerBase { |
8 | 8 |
9 static readonly object _consoleLock = new object(); | 9 static readonly object _consoleLock = new object(); |
10 | 10 |
11 public void Subscribe() { | 11 protected override void WriteEntry(TraceContext context, EventText text) { |
12 LogChannel<TraceEvent>.Default.Events += Default_Events; | 12 var msg = new StringBuilder(); |
13 } | |
14 | 13 |
15 public void Unsubscribe() { | 14 for (int i = 0; i < text.indent; i++) |
16 LogChannel<TraceEvent>.Default.Events -= Default_Events; | |
17 } | |
18 | |
19 void Default_Events(object sender, ValueEventArgs<TraceEvent> e) { | |
20 LogEvent((TraceContext)sender, e.Value); | |
21 } | |
22 | |
23 void LogEvent(TraceContext context, TraceEvent evt) { | |
24 var msg = new StringBuilder(); | |
25 for (int i = 0; i < context.CurrentOperation.Level; i++) | |
26 msg.Append(" "); | 15 msg.Append(" "); |
27 msg.Append(evt.EventType); | 16 msg.AppendFormat("[{0}]: {1}", context.ThreadId, text.content); |
28 msg.AppendFormat("[{0}]: ",context.ThreadId); | |
29 msg.Append(evt.Message); | |
30 | 17 |
31 lock (_consoleLock) { | 18 lock (_consoleLock) { |
32 Console.ForegroundColor = (ConsoleColor)(context.ThreadId % 15 + 1); | 19 Console.ForegroundColor = (ConsoleColor)(context.ThreadId % 15 + 1); |
33 Console.WriteLine(msg.ToString()); | 20 Console.WriteLine(msg.ToString()); |
34 } | 21 } |