2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System / System.Diagnostics / DelimitedListTraceListener.cs
blobe37be03dcf94162796f8e3fffe5c4f376e193138
1 //
2 // DelimitedListTraceFilter.cs
3 //
4 // Author:
5 // Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C) 2007 Novell, Inc.
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
30 #if NET_2_0
32 using System;
33 using System.IO;
34 using System.Collections;
35 using System.Diagnostics;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.Text;
40 namespace System.Diagnostics
42 public class DelimitedListTraceListener : TextWriterTraceListener
44 public DelimitedListTraceListener (string fileName)
45 : base (fileName)
49 public DelimitedListTraceListener (string fileName, string name)
50 : base (fileName, name)
54 public DelimitedListTraceListener (Stream stream)
55 : base (stream)
59 public DelimitedListTraceListener (Stream stream, string name)
60 : base (stream, name)
64 public DelimitedListTraceListener (TextWriter writer)
65 : base (writer)
69 public DelimitedListTraceListener (TextWriter writer, string name)
70 : base (writer, name)
74 static readonly string [] attributes = new string [] {"delimiter"};
75 string delimiter = ";";
77 public string Delimiter {
78 get { return delimiter; }
79 set {
80 if (value == null)
81 throw new ArgumentNullException ("value");
82 delimiter = value;
86 protected internal override string [] GetSupportedAttributes ()
88 return attributes;
91 public override void TraceData (TraceEventCache eventCache,
92 string source, TraceEventType eventType,
93 int id, object data)
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}",
123 delimiter,
124 source != null ? "\"" + source.Replace ("\"", "\"\"") + "\"": null,
125 eventType,
127 message != null ? "\"" + message.Replace ("\"", "\"\"") + "\"" : null,
128 FormatData (data),
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)
146 return null;
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)
152 sb.Append (',');
154 return sb.ToString ();
158 #endif