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.
33 using System.Collections;
34 using System.Diagnostics;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
39 namespace System.Diagnostics
41 public class DelimitedListTraceListener : TextWriterTraceListener
43 public DelimitedListTraceListener (string fileName)
48 public DelimitedListTraceListener (string fileName, string name)
49 : base (fileName, name)
53 public DelimitedListTraceListener (Stream stream)
58 public DelimitedListTraceListener (Stream stream, string name)
63 public DelimitedListTraceListener (TextWriter writer)
68 public DelimitedListTraceListener (TextWriter writer, string name)
73 static readonly string [] attributes = new string [] {"delimiter"};
74 string delimiter = ";";
76 public string Delimiter {
77 get { return delimiter; }
80 throw new ArgumentNullException ("value");
85 protected internal override string [] GetSupportedAttributes ()
90 public override void TraceData (TraceEventCache eventCache,
91 string source, TraceEventType eventType,
94 TraceCore (eventCache, source, eventType, id, null, data);
97 public override void TraceData (TraceEventCache eventCache,
98 string source, TraceEventType eventType,
99 int id, params object [] data)
101 TraceCore (eventCache, source, eventType, id, null, data);
104 public override void TraceEvent (TraceEventCache eventCache,
105 string source, TraceEventType eventType,
106 int id, string message)
108 TraceCore (eventCache, source, eventType, id, message);
111 public override void TraceEvent (TraceEventCache eventCache,
112 string source, TraceEventType eventType,
113 int id, string format, params object [] args)
115 TraceCore (eventCache, source, eventType, id, String.Format (format, args));
118 void TraceCore (TraceEventCache c, string source, TraceEventType eventType, int id, string message, params object [] data)
120 // source, eventType, id, message?, data-comma-separated
121 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}",
123 source != null ? "\"" + source.Replace ("\"", "\"\"") + "\"": null,
126 message != null ? "\"" + message.Replace ("\"", "\"\"") + "\"" : null,
128 IsTarget (c, TraceOptions.ProcessId) ? c.ProcessId.ToString () : null,
129 IsTarget (c, TraceOptions.LogicalOperationStack) ? FormatArray (c.LogicalOperationStack, ", ") : null,
130 IsTarget (c, TraceOptions.ThreadId) ? c.ThreadId : null,
131 IsTarget (c, TraceOptions.DateTime) ? c.DateTime.ToString ("o") : null,
132 IsTarget (c, TraceOptions.Timestamp) ? c.Timestamp.ToString () : null,
133 IsTarget (c, TraceOptions.Callstack) ? c.Callstack : null,
134 Environment.NewLine));
137 bool IsTarget (TraceEventCache c, TraceOptions opt)
139 return c != null && (TraceOutputOptions & opt) != 0;
142 string FormatData (object [] data)
144 if (data == null || data.Length == 0)
146 StringBuilder sb = new StringBuilder ();
147 for (int i = 0; i < data.Length; i++) {
148 if (data [i] != null)
149 sb.Append ('"').Append (data [i].ToString ().Replace ("\"", "\"\"")).Append ('"');
150 if (i + 1 < data.Length)
153 return sb.ToString ();