40
|
1 using System;
|
|
2 using System.IO;
|
|
3 using System.Text;
|
|
4
|
|
5 namespace Implab.Diagnostics {
|
134
|
6 public class TextFileListener: ListenerBase {
|
40
|
7 readonly TextWriter m_textWriter;
|
|
8
|
134
|
9 public TextFileListener(string fileName) {
|
40
|
10 m_textWriter = File.CreateText(fileName);
|
|
11
|
|
12 m_textWriter.WriteLine("LOG {0}", DateTime.Now);
|
|
13 }
|
|
14
|
134
|
15 #region implemented abstract members of ListenerBase
|
|
16
|
|
17 public override void Write(LogEventArgs args, object entry) {
|
40
|
18 var msg = new StringBuilder();
|
134
|
19 for (int i = 0; i < args.Operation.Level; i++)
|
40
|
20 msg.Append(" ");
|
194
|
21 msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, args.Channel, entry);
|
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
|
134
|
32 #endregion
|
|
33
|
40
|
34 protected override void Dispose(bool disposing) {
|
|
35 base.Dispose(disposing);
|
|
36 if (disposing) {
|
47
|
37 // IsDisposed = true
|
40
|
38 lock (m_textWriter) {
|
|
39 Safe.Dispose(m_textWriter);
|
|
40 }
|
|
41 }
|
|
42 }
|
|
43
|
|
44
|
|
45 }
|
|
46 }
|