annotate Implab/Diagnostics/ConsoleTraceListener.cs @ 38:1cedc4e58acc diagnostics

Закрыть ветку diagnostics
author cin
date Tue, 15 Apr 2014 02:46:33 +0400
parents 313f708a50e9
children fe33f4e02ad5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
1 using System;
313f708a50e9 improved log concept
cin
parents:
diff changeset
2 using System.Collections.Generic;
313f708a50e9 improved log concept
cin
parents:
diff changeset
3 using System.Linq;
313f708a50e9 improved log concept
cin
parents:
diff changeset
4 using System.Text;
313f708a50e9 improved log concept
cin
parents:
diff changeset
5
313f708a50e9 improved log concept
cin
parents:
diff changeset
6 namespace Implab.Diagnostics {
313f708a50e9 improved log concept
cin
parents:
diff changeset
7 public class ConsoleTraceListener {
313f708a50e9 improved log concept
cin
parents:
diff changeset
8
313f708a50e9 improved log concept
cin
parents:
diff changeset
9 static readonly object _consoleLock = new object();
313f708a50e9 improved log concept
cin
parents:
diff changeset
10
313f708a50e9 improved log concept
cin
parents:
diff changeset
11 public void Subscribe() {
313f708a50e9 improved log concept
cin
parents:
diff changeset
12 LogChannel<TraceEvent>.Default.Events += Default_Events;
313f708a50e9 improved log concept
cin
parents:
diff changeset
13 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
14
313f708a50e9 improved log concept
cin
parents:
diff changeset
15 public void Unsubscribe() {
313f708a50e9 improved log concept
cin
parents:
diff changeset
16 LogChannel<TraceEvent>.Default.Events -= Default_Events;
313f708a50e9 improved log concept
cin
parents:
diff changeset
17 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
18
313f708a50e9 improved log concept
cin
parents:
diff changeset
19 void Default_Events(object sender, ValueEventArgs<TraceEvent> e) {
313f708a50e9 improved log concept
cin
parents:
diff changeset
20 LogEvent((TraceContext)sender, e.Value);
313f708a50e9 improved log concept
cin
parents:
diff changeset
21 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
22
313f708a50e9 improved log concept
cin
parents:
diff changeset
23 void LogEvent(TraceContext context, TraceEvent evt) {
313f708a50e9 improved log concept
cin
parents:
diff changeset
24 var msg = new StringBuilder();
313f708a50e9 improved log concept
cin
parents:
diff changeset
25 for (int i = 0; i < context.CurrentOperation.Level; i++)
313f708a50e9 improved log concept
cin
parents:
diff changeset
26 msg.Append(" ");
313f708a50e9 improved log concept
cin
parents:
diff changeset
27 msg.Append(evt.EventType);
313f708a50e9 improved log concept
cin
parents:
diff changeset
28 msg.AppendFormat("[{0}]: ",context.ThreadId);
313f708a50e9 improved log concept
cin
parents:
diff changeset
29 msg.Append(evt.Message);
313f708a50e9 improved log concept
cin
parents:
diff changeset
30
313f708a50e9 improved log concept
cin
parents:
diff changeset
31 lock (_consoleLock) {
313f708a50e9 improved log concept
cin
parents:
diff changeset
32 Console.ForegroundColor = (ConsoleColor)(context.ThreadId % 15 + 1);
313f708a50e9 improved log concept
cin
parents:
diff changeset
33 Console.WriteLine(msg.ToString());
313f708a50e9 improved log concept
cin
parents:
diff changeset
34 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
35 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
36 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
37 }