36
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Linq;
|
|
4 using System.Text;
|
|
5
|
|
6 namespace Implab.Diagnostics {
|
|
7 public class ConsoleTraceListener {
|
|
8
|
|
9 static readonly object _consoleLock = new object();
|
|
10
|
|
11 public void Subscribe() {
|
|
12 LogChannel<TraceEvent>.Default.Events += Default_Events;
|
|
13 }
|
|
14
|
|
15 public void Unsubscribe() {
|
|
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(" ");
|
|
27 msg.Append(evt.EventType);
|
|
28 msg.AppendFormat("[{0}]: ",context.ThreadId);
|
|
29 msg.Append(evt.Message);
|
|
30
|
|
31 lock (_consoleLock) {
|
|
32 Console.ForegroundColor = (ConsoleColor)(context.ThreadId % 15 + 1);
|
|
33 Console.WriteLine(msg.ToString());
|
|
34 }
|
|
35 }
|
|
36 }
|
|
37 }
|