TextFileListener.cs
46 lines
| 1.4 KiB
| text/x-csharp
|
CSharpLexer
cin
|
r40 | using System; | ||
using System.IO; | ||||
using System.Text; | ||||
namespace Implab.Diagnostics { | ||||
cin
|
r134 | public class TextFileListener: ListenerBase { | ||
cin
|
r40 | readonly TextWriter m_textWriter; | ||
cin
|
r134 | public TextFileListener(string fileName) { | ||
cin
|
r40 | m_textWriter = File.CreateText(fileName); | ||
m_textWriter.WriteLine("LOG {0}", DateTime.Now); | ||||
} | ||||
cin
|
r134 | #region implemented abstract members of ListenerBase | ||
public override void Write(LogEventArgs args, object entry) { | ||||
cin
|
r40 | var msg = new StringBuilder(); | ||
cin
|
r134 | for (int i = 0; i < args.Operation.Level; i++) | ||
cin
|
r40 | msg.Append(" "); | ||
cin
|
r134 | msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, args.ChannelName, entry); | ||
cin
|
r40 | |||
lock (m_textWriter) { | ||||
if (!IsDisposed) { | ||||
cin
|
r47 | // тут гарантировано еще не освобожден m_textWriter | ||
cin
|
r92 | m_textWriter.WriteLine(msg); | ||
cin
|
r40 | m_textWriter.Flush(); | ||
} | ||||
} | ||||
} | ||||
cin
|
r134 | #endregion | ||
cin
|
r40 | protected override void Dispose(bool disposing) { | ||
base.Dispose(disposing); | ||||
if (disposing) { | ||||
cin
|
r47 | // IsDisposed = true | ||
cin
|
r40 | lock (m_textWriter) { | ||
Safe.Dispose(m_textWriter); | ||||
} | ||||
} | ||||
} | ||||
} | ||||
} | ||||