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