annotate Implab/Diagnostics/SimpleTraceListener.cs @ 254:12c00235b105 v3

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