TextFileListener.cs
47 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
|
r205 | readonly object m_lock = new object(); | ||
cin
|
r40 | |||
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
|
r194 | msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, args.Channel, entry); | ||
cin
|
r40 | |||
cin
|
r205 | lock (m_lock) { | ||
cin
|
r40 | 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
|
r205 | lock (m_lock) { | ||
cin
|
r40 | Safe.Dispose(m_textWriter); | ||
} | ||||
} | ||||
} | ||||
} | ||||
} | ||||