40
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.IO;
|
|
4 using System.Linq;
|
|
5 using System.Text;
|
|
6
|
|
7 namespace Implab.Diagnostics {
|
|
8 public class TextFileListener: TextListenerBase {
|
|
9 readonly TextWriter m_textWriter;
|
|
10
|
48
|
11 public TextFileListener(string fileName, bool global)
|
|
12 : base(global) {
|
40
|
13 m_textWriter = File.CreateText(fileName);
|
|
14
|
|
15 m_textWriter.WriteLine("LOG {0}", DateTime.Now);
|
|
16 Register(this);
|
|
17 }
|
|
18
|
48
|
19 protected override void WriteEntry(TraceContext context, EventText text, string channel) {
|
40
|
20 var msg = new StringBuilder();
|
|
21 for (int i = 0; i < text.indent; i++)
|
|
22 msg.Append(" ");
|
48
|
23 msg.AppendFormat("[{0}]:{1}: {2}", context.ThreadId, channel, text.content);
|
40
|
24
|
|
25 lock (m_textWriter) {
|
|
26 if (!IsDisposed) {
|
47
|
27 // тут гарантировано еще не освобожден m_textWriter
|
40
|
28 m_textWriter.WriteLine(msg.ToString());
|
|
29 m_textWriter.Flush();
|
|
30 }
|
|
31 }
|
|
32 }
|
|
33
|
|
34
|
|
35 protected override void Dispose(bool disposing) {
|
|
36 base.Dispose(disposing);
|
|
37 if (disposing) {
|
47
|
38 // IsDisposed = true
|
40
|
39 lock (m_textWriter) {
|
|
40 Safe.Dispose(m_textWriter);
|
|
41 }
|
|
42 }
|
|
43 }
|
|
44
|
|
45
|
|
46 }
|
|
47 }
|