comparison Implab/Diagnostics/TextFileListener.cs @ 134:04d4c92d0f28 v2

Improved logging
author cin
date Wed, 11 Feb 2015 02:12:15 +0300
parents 4c0e5ef99986
children d45bdf510514
comparison
equal deleted inserted replaced
133:6c49d02a9a05 134:04d4c92d0f28
1 using System; 1 using System;
2 using System.IO; 2 using System.IO;
3 using System.Text; 3 using System.Text;
4 4
5 namespace Implab.Diagnostics { 5 namespace Implab.Diagnostics {
6 public class TextFileListener: TextListenerBase { 6 public class TextFileListener: ListenerBase {
7 readonly TextWriter m_textWriter; 7 readonly TextWriter m_textWriter;
8 8
9 public TextFileListener(string fileName, bool global) 9 public TextFileListener(string fileName) {
10 : base(global) {
11 m_textWriter = File.CreateText(fileName); 10 m_textWriter = File.CreateText(fileName);
12 11
13 m_textWriter.WriteLine("LOG {0}", DateTime.Now); 12 m_textWriter.WriteLine("LOG {0}", DateTime.Now);
14 Register(this);
15 } 13 }
16 14
17 protected override void WriteEntry(LogEventArgs args, EventText text, string channel) { 15 #region implemented abstract members of ListenerBase
16
17 public override void Write(LogEventArgs args, object entry) {
18 var msg = new StringBuilder(); 18 var msg = new StringBuilder();
19 for (int i = 0; i < text.indent; i++) 19 for (int i = 0; i < args.Operation.Level; i++)
20 msg.Append(" "); 20 msg.Append(" ");
21 msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, channel, text.content); 21 msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, args.ChannelName, entry);
22 22
23 lock (m_textWriter) { 23 lock (m_textWriter) {
24 if (!IsDisposed) { 24 if (!IsDisposed) {
25 // тут гарантировано еще не освобожден m_textWriter 25 // тут гарантировано еще не освобожден m_textWriter
26 m_textWriter.WriteLine(msg); 26 m_textWriter.WriteLine(msg);
27 m_textWriter.Flush(); 27 m_textWriter.Flush();
28 } 28 }
29 } 29 }
30 } 30 }
31 31
32 32 #endregion
33
33 protected override void Dispose(bool disposing) { 34 protected override void Dispose(bool disposing) {
34 base.Dispose(disposing); 35 base.Dispose(disposing);
35 if (disposing) { 36 if (disposing) {
36 // IsDisposed = true 37 // IsDisposed = true
37 lock (m_textWriter) { 38 lock (m_textWriter) {