Mercurial > pub > ImplabNet
annotate Implab/Diagnostics/TextFileListener.cs @ 213:9ee78a345738 v2
Minor code changes
author | cin |
---|---|
date | Tue, 11 Apr 2017 01:35:18 +0300 |
parents | 8200ab154c8a |
children | 6efb77590b15 |
rev | line source |
---|---|
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; |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
194
diff
changeset
|
8 readonly object m_lock = new object(); |
40 | 9 |
134 | 10 public TextFileListener(string fileName) { |
40 | 11 m_textWriter = File.CreateText(fileName); |
12 | |
13 m_textWriter.WriteLine("LOG {0}", DateTime.Now); | |
14 } | |
15 | |
134 | 16 #region implemented abstract members of ListenerBase |
17 | |
18 public override void Write(LogEventArgs args, object entry) { | |
40 | 19 var msg = new StringBuilder(); |
134 | 20 for (int i = 0; i < args.Operation.Level; i++) |
40 | 21 msg.Append(" "); |
194 | 22 msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, args.Channel, entry); |
40 | 23 |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
194
diff
changeset
|
24 lock (m_lock) { |
40 | 25 if (!IsDisposed) { |
47 | 26 // тут гарантировано еще не освобожден m_textWriter |
92 | 27 m_textWriter.WriteLine(msg); |
40 | 28 m_textWriter.Flush(); |
29 } | |
30 } | |
31 } | |
32 | |
134 | 33 #endregion |
34 | |
40 | 35 protected override void Dispose(bool disposing) { |
36 base.Dispose(disposing); | |
37 if (disposing) { | |
47 | 38 // IsDisposed = true |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
194
diff
changeset
|
39 lock (m_lock) { |
40 | 40 Safe.Dispose(m_textWriter); |
41 } | |
42 } | |
43 } | |
44 | |
45 | |
46 } | |
47 } |