annotate Implab/Diagnostics/ConsoleTraceListener.cs @ 43:7c2369f580b8

improved tracing, TextListenerBase can be bound to logical operation scope.
author cin
date Wed, 16 Apr 2014 10:12:56 +0400
parents fe33f4e02ad5
children d9d794b61bb9
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 {
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
7 public class ConsoleTraceListener: TextListenerBase {
36
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
43
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
11 public ConsoleTraceListener()
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
12 : base(true) {
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
13
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
14 }
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
15
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
16 public ConsoleTraceListener(bool local)
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
17 : base(local) {
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
18
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
19 }
7c2369f580b8 improved tracing, TextListenerBase can be bound to logical operation scope.
cin
parents: 40
diff changeset
20
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
21 protected override void WriteEntry(TraceContext context, EventText text) {
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
22 var msg = new StringBuilder();
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
23
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
24 for (int i = 0; i < text.indent; i++)
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
25 msg.Append(" ");
40
fe33f4e02ad5 improved tracing
cin
parents: 36
diff changeset
26 msg.AppendFormat("[{0}]: {1}", context.ThreadId, text.content);
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
27
313f708a50e9 improved log concept
cin
parents:
diff changeset
28 lock (_consoleLock) {
313f708a50e9 improved log concept
cin
parents:
diff changeset
29 Console.ForegroundColor = (ConsoleColor)(context.ThreadId % 15 + 1);
313f708a50e9 improved log concept
cin
parents:
diff changeset
30 Console.WriteLine(msg.ToString());
313f708a50e9 improved log concept
cin
parents:
diff changeset
31 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
32 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
33 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
34 }