comparison Implab/Diagnostics/ConsoleTraceListener.cs @ 36:313f708a50e9 diagnostics

improved log concept
author cin
date Tue, 15 Apr 2014 02:00:09 +0400
parents
children fe33f4e02ad5
comparison
equal deleted inserted replaced
35:2880242f987a 36:313f708a50e9
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 }