Mercurial > pub > ImplabNet
annotate Implab/Diagnostics/TextFileListener.cs @ 234:8dd666e6b6bf v2
Added implab nuget spec
author | cin |
---|---|
date | Thu, 05 Oct 2017 09:21:23 +0300 |
parents | 6efb77590b15 |
children |
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(" "); |
217 | 22 msg.AppendFormat("[{0}]+{3}ms:{1}: {2}", args.ThreadId, args.Channel, entry, args.OperationTimeOffset); |
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 } |