2 // DelimitedListTraceFilter.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // (C) 2007 Novell, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Collections
;
35 using System
.Diagnostics
;
36 using System
.Runtime
.CompilerServices
;
37 using System
.Runtime
.InteropServices
;
40 namespace System
.Diagnostics
42 public class DelimitedListTraceListener
: TextWriterTraceListener
44 public DelimitedListTraceListener (string fileName
)
49 public DelimitedListTraceListener (string fileName
, string name
)
50 : base (fileName
, name
)
54 public DelimitedListTraceListener (Stream stream
)
59 public DelimitedListTraceListener (Stream stream
, string name
)
64 public DelimitedListTraceListener (TextWriter writer
)
69 public DelimitedListTraceListener (TextWriter writer
, string name
)
74 static readonly string [] attributes
= new string [] {"delimiter"}
;
75 string delimiter
= ";";
77 public string Delimiter
{
78 get { return delimiter; }
81 throw new ArgumentNullException ("value");
86 protected internal override string [] GetSupportedAttributes ()
91 public override void TraceData (TraceEventCache eventCache
,
92 string source
, TraceEventType eventType
,
95 TraceCore (eventCache
, source
, eventType
, id
, null, data
);
98 public override void TraceData (TraceEventCache eventCache
,
99 string source
, TraceEventType eventType
,
100 int id
, params object [] data
)
102 TraceCore (eventCache
, source
, eventType
, id
, null, data
);
105 public override void TraceEvent (TraceEventCache eventCache
,
106 string source
, TraceEventType eventType
,
107 int id
, string message
)
109 TraceCore (eventCache
, source
, eventType
, id
, message
);
112 public override void TraceEvent (TraceEventCache eventCache
,
113 string source
, TraceEventType eventType
,
114 int id
, string format
, params object [] args
)
116 TraceCore (eventCache
, source
, eventType
, id
, String
.Format (format
, args
));
119 void TraceCore (TraceEventCache c
, string source
, TraceEventType eventType
, int id
, string message
, params object [] data
)
121 // source, eventType, id, message?, data-comma-separated
122 Write (String
.Format ("{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}{6}{0}{7}{0}{8}{0}{9}{0}{10}{0}{11}{12}",
124 source
!= null ? "\"" + source
.Replace ("\"", "\"\"") + "\"": null,
127 message
!= null ? "\"" + message
.Replace ("\"", "\"\"") + "\"" : null,
129 IsTarget (c
, TraceOptions
.ProcessId
) ? c
.ProcessId
.ToString () : null,
130 IsTarget (c
, TraceOptions
.LogicalOperationStack
) ? FormatArray (c
.LogicalOperationStack
, ", ") : null,
131 IsTarget (c
, TraceOptions
.ThreadId
) ? c
.ThreadId
: null,
132 IsTarget (c
, TraceOptions
.DateTime
) ? c
.DateTime
.ToString ("o") : null,
133 IsTarget (c
, TraceOptions
.Timestamp
) ? c
.Timestamp
.ToString () : null,
134 IsTarget (c
, TraceOptions
.Callstack
) ? c
.Callstack
: null,
135 Environment
.NewLine
));
138 bool IsTarget (TraceEventCache c
, TraceOptions opt
)
140 return c
!= null && (TraceOutputOptions
& opt
) != 0;
143 string FormatData (object [] data
)
145 if (data
== null || data
.Length
== 0)
147 StringBuilder sb
= new StringBuilder ();
148 for (int i
= 0; i
< data
.Length
; i
++) {
149 if (data
[i
] != null)
150 sb
.Append ('"').Append (data
[i
].ToString ().Replace ("\"", "\"\"")).Append ('"');
151 if (i
+ 1 < data
.Length
)
154 return sb
.ToString ();