annotate Implab/Diagnostics/TextFileListener.cs @ 247:fb70574741a1 v3

working on promises
author cin
date Fri, 26 Jan 2018 18:46:27 +0300
parents 6efb77590b15
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
1 using System;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
2 using System.IO;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
3 using System.Text;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
4
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
5 namespace Implab.Diagnostics {
134
04d4c92d0f28 Improved logging
cin
parents: 92
diff changeset
6 public class TextFileListener: ListenerBase {
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
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
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
9
134
04d4c92d0f28 Improved logging
cin
parents: 92
diff changeset
10 public TextFileListener(string fileName) {
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
11 m_textWriter = File.CreateText(fileName);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
12
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
13 m_textWriter.WriteLine("LOG {0}", DateTime.Now);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
14 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
15
134
04d4c92d0f28 Improved logging
cin
parents: 92
diff changeset
16 #region implemented abstract members of ListenerBase
04d4c92d0f28 Improved logging
cin
parents: 92
diff changeset
17
04d4c92d0f28 Improved logging
cin
parents: 92
diff changeset
18 public override void Write(LogEventArgs args, object entry) {
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
19 var msg = new StringBuilder();
134
04d4c92d0f28 Improved logging
cin
parents: 92
diff changeset
20 for (int i = 0; i < args.Operation.Level; i++)
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
21 msg.Append(" ");
217
6efb77590b15 More verbose output with TextFileListener
cin
parents: 205
diff changeset
22 msg.AppendFormat("[{0}]+{3}ms:{1}: {2}", args.ThreadId, args.Channel, entry, args.OperationTimeOffset);
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
23
205
8200ab154c8a Added ResetState to RunnableComponent to reset in case of failure
cin
parents: 194
diff changeset
24 lock (m_lock) {
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
25 if (!IsDisposed) {
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 44
diff changeset
26 // тут гарантировано еще не освобожден m_textWriter
92
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
27 m_textWriter.WriteLine(msg);
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
28 m_textWriter.Flush();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
29 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
30 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
31 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
32
134
04d4c92d0f28 Improved logging
cin
parents: 92
diff changeset
33 #endregion
04d4c92d0f28 Improved logging
cin
parents: 92
diff changeset
34
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
35 protected override void Dispose(bool disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
36 base.Dispose(disposing);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
37 if (disposing) {
47
b181f7bcb259 refactoring, interactive tarce log almost complete
cin
parents: 44
diff changeset
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
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
40 Safe.Dispose(m_textWriter);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
41 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
42 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
43 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
44
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
45
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
46 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
47 }