Mercurial > pub > ImplabNet
changeset 43:7c2369f580b8
improved tracing, TextListenerBase can be bound to logical operation scope.
author | cin |
---|---|
date | Wed, 16 Apr 2014 10:12:56 +0400 |
parents | 3ba6778ed336 |
children | e5ec543feee3 |
files | Implab.v11.suo Implab/Diagnostics/ConsoleTraceListener.cs Implab/Diagnostics/TextFileListener.cs Implab/Diagnostics/TextListenerBase.cs Implab/Diagnostics/TraceContext.cs Implab/Diagnostics/TraceEvent.cs |
diffstat | 6 files changed, 43 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/Implab/Diagnostics/ConsoleTraceListener.cs Wed Apr 16 00:33:09 2014 +0400 +++ b/Implab/Diagnostics/ConsoleTraceListener.cs Wed Apr 16 10:12:56 2014 +0400 @@ -8,6 +8,16 @@ static readonly object _consoleLock = new object(); + public ConsoleTraceListener() + : base(true) { + + } + + public ConsoleTraceListener(bool local) + : base(local) { + + } + protected override void WriteEntry(TraceContext context, EventText text) { var msg = new StringBuilder();
--- a/Implab/Diagnostics/TextFileListener.cs Wed Apr 16 00:33:09 2014 +0400 +++ b/Implab/Diagnostics/TextFileListener.cs Wed Apr 16 10:12:56 2014 +0400 @@ -8,7 +8,7 @@ public class TextFileListener: TextListenerBase { readonly TextWriter m_textWriter; - public TextFileListener(string fileName) { + public TextFileListener(string fileName) : base(true) { m_textWriter = File.CreateText(fileName); m_textWriter.WriteLine("LOG {0}", DateTime.Now);
--- a/Implab/Diagnostics/TextListenerBase.cs Wed Apr 16 00:33:09 2014 +0400 +++ b/Implab/Diagnostics/TextListenerBase.cs Wed Apr 16 10:12:56 2014 +0400 @@ -7,9 +7,15 @@ public abstract class TextListenerBase : ServiceLocator, IEventTextFormatter<object>, IEventTextFormatter<TraceEvent> { readonly Dictionary<object, Action> m_subscriptions = new Dictionary<object, Action>(); + readonly LogicalOperation m_boundOperation; + readonly int m_baseIndent; - protected TextListenerBase() { + protected TextListenerBase(bool local) { Register(this); + if (local) { + m_boundOperation = TraceContext.Current.CurrentOperation; + m_baseIndent = Math.Max(0, m_boundOperation.Level - 1); + } } public void Subscribe(Type eventType) { @@ -32,7 +38,12 @@ var formatter = GetService<IEventTextFormatter<TEvent>>(); EventHandler<ValueEventArgs<TEvent>> handler = (sender, args) => { - WriteEntry((TraceContext)sender, formatter.Format((TraceContext)sender, args.Value)); + TraceContext context = (TraceContext)sender; + var text = formatter.Format(context, args.Value); + text.indent -= m_baseIndent; + + if (IsRelated(context.CurrentOperation)) + WriteEntry(context, text); }; if (m_subscriptions.ContainsKey(channel)) @@ -48,6 +59,15 @@ } } + public bool IsRelated(LogicalOperation op) { + if (m_boundOperation == null) + return true; + + while (op != m_boundOperation && op.Level > m_boundOperation.Level) + op = op.Parent; + return op == m_boundOperation; + } + public void Unsubscribe<TEvent>(LogChannel<TEvent> channel) { if (channel == null) throw new ArgumentNullException("channel");
--- a/Implab/Diagnostics/TraceContext.cs Wed Apr 16 00:33:09 2014 +0400 +++ b/Implab/Diagnostics/TraceContext.cs Wed Apr 16 10:12:56 2014 +0400 @@ -92,6 +92,7 @@ try { action(); } finally { + _current.EndAllOperations(); _current = old; } } @@ -156,6 +157,14 @@ } } + /// <summary> + /// Заврешает все начатые в этом контексте операции + /// </summary> + public void EndAllOperations() { + while (m_bound != m_currentOperation) + EndLogicalOperation(); + } + void LogEvent(TraceEventType type, string format, params object[] args) { LogChannel<TraceEvent>.Default.LogEvent(this, TraceEvent.Create(type, format, args)); }
--- a/Implab/Diagnostics/TraceEvent.cs Wed Apr 16 00:33:09 2014 +0400 +++ b/Implab/Diagnostics/TraceEvent.cs Wed Apr 16 10:12:56 2014 +0400 @@ -25,7 +25,7 @@ } public static TraceEvent Create(TraceEventType type, string format, params object[] args) { - return new TraceEvent(type, String.Format(format ?? String.Empty, args)); + return new TraceEvent(type, format == null ? String.Empty : String.Format(format, args)); } } }