Mercurial > pub > ImplabNet
comparison Implab/Diagnostics/SimpleTraceListener.cs @ 253:34df34841225 v3 v3.0.1-beta
Implab.Diagnostics drafts
author | cin |
---|---|
date | Mon, 12 Feb 2018 17:03:37 +0300 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
252:6f4630d0bcd9 | 253:34df34841225 |
---|---|
1 using System; | |
2 using System.Diagnostics; | |
3 using System.IO; | |
4 | |
5 namespace Implab.Diagnostics { | |
6 public class SimpleTraceListener : TextWriterTraceListener { | |
7 public SimpleTraceListener() { | |
8 } | |
9 | |
10 public SimpleTraceListener(Stream stream) : base(stream) { | |
11 } | |
12 | |
13 public SimpleTraceListener(TextWriter writer) : base(writer) { | |
14 } | |
15 | |
16 public SimpleTraceListener(string fileName) : base(fileName) { | |
17 } | |
18 | |
19 public SimpleTraceListener(Stream stream, string name) : base(stream, name) { | |
20 } | |
21 | |
22 public SimpleTraceListener(TextWriter writer, string name) : base(writer, name) { | |
23 } | |
24 | |
25 public SimpleTraceListener(string fileName, string name) : base(fileName, name) { | |
26 } | |
27 | |
28 public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) { | |
29 switch (id) { | |
30 case TraceEventCodes.StartLogicalOperation: | |
31 TraceEvent(eventCache, source, eventType, id, "+{0}", data); | |
32 break; | |
33 case TraceEventCodes.StopLogicalOperation: | |
34 TraceEvent(eventCache, source, eventType, id, FormatStopLogicalOperation(data)); | |
35 break; | |
36 default: | |
37 TraceEvent(eventCache, source, eventType, id, data?.ToString()); | |
38 break; | |
39 } | |
40 } | |
41 | |
42 string FormatStopLogicalOperation(object data) { | |
43 if (data is LogicalOperation op) { | |
44 return string.Format("-{0} ({1})", op, FormatTimespan(op.OperationStopwatch.Elapsed)); | |
45 } else { | |
46 return data?.ToString(); | |
47 } | |
48 } | |
49 | |
50 string FormatTimespan(TimeSpan value) { | |
51 if (value.TotalSeconds < 10) { | |
52 return value.Milliseconds.ToString() + "ms"; | |
53 } else if (value.TotalSeconds < 30) { | |
54 return string.Format("{0:0.###}s", value.TotalSeconds); | |
55 } else { | |
56 return value.ToString(); | |
57 } | |
58 } | |
59 | |
60 public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data) { | |
61 var prev = IndentLevel; | |
62 IndentLevel += eventCache.LogicalOperationStack.Count; | |
63 try { | |
64 base.TraceData(eventCache, source, eventType, id, data); | |
65 } finally { | |
66 IndentLevel = prev; | |
67 } | |
68 } | |
69 | |
70 public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id) { | |
71 var prev = IndentLevel; | |
72 IndentLevel += eventCache.LogicalOperationStack.Count; | |
73 try { | |
74 base.TraceEvent(eventCache, source, eventType, id); | |
75 } finally { | |
76 IndentLevel = prev; | |
77 } | |
78 } | |
79 | |
80 public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args) { | |
81 TraceEvent(eventCache, source, eventType, id, String.Format(format, args)); | |
82 } | |
83 | |
84 public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message) { | |
85 var prev = IndentLevel; | |
86 IndentLevel += eventCache.LogicalOperationStack.Count; | |
87 try { | |
88 LogicalOperation operation = null; | |
89 if (eventCache.LogicalOperationStack.Count > 0) | |
90 operation = eventCache.LogicalOperationStack.Peek() as LogicalOperation; | |
91 | |
92 if (operation != null) { | |
93 base.TraceData(eventCache, source, eventType, id, FormatTimespan(operation.OperationStopwatch.Elapsed) + ": " + message); | |
94 } else { | |
95 base.TraceData(eventCache, source, eventType, id, message); | |
96 } | |
97 } finally { | |
98 IndentLevel = prev; | |
99 } | |
100 } | |
101 | |
102 public override void TraceTransfer(TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId) { | |
103 var prev = IndentLevel; | |
104 IndentLevel += eventCache.LogicalOperationStack.Count; | |
105 try { | |
106 base.TraceTransfer(eventCache, source, id, message, relatedActivityId); | |
107 } finally { | |
108 IndentLevel = prev; | |
109 } | |
110 } | |
111 | |
112 | |
113 } | |
114 } |