annotate Implab/Diagnostics/TextFileListener.cs @ 44:e5ec543feee3

Рефакторинг, журналирование
author cin
date Wed, 16 Apr 2014 19:02:58 +0400
parents 7c2369f580b8
children b181f7bcb259
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
1 using System;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
2 using System.Collections.Generic;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
3 using System.IO;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
4 using System.Linq;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
5 using System.Text;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
6
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
7 namespace Implab.Diagnostics {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
8 public class TextFileListener: TextListenerBase {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
9 readonly TextWriter m_textWriter;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
10
44
e5ec543feee3 Рефакторинг, журналирование
cin
parents: 43
diff changeset
11 public TextFileListener(string fileName, bool local) : base(local) {
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
12 m_textWriter = File.CreateText(fileName);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
13
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
14 m_textWriter.WriteLine("LOG {0}", DateTime.Now);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
15 Register(this);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
16 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
17
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
18 protected override void WriteEntry(TraceContext context, EventText text) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
19 var msg = new StringBuilder();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
20 for (int i = 0; i < text.indent; i++)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
21 msg.Append(" ");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
22 msg.AppendFormat("[{0}]: {1}", context.ThreadId, text.content);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
23
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
24 lock (m_textWriter) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
25 if (!IsDisposed) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
26 m_textWriter.WriteLine(msg.ToString());
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
27 m_textWriter.Flush();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
28 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
29 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
30 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
31
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
32
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
33 protected override void Dispose(bool disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
34 base.Dispose(disposing);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
35 if (disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
36 lock (m_textWriter) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
37 Safe.Dispose(m_textWriter);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
38 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
39 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
40 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
41
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
42
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
43 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
44 }