2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System / System.Diagnostics / TraceSource.cs
blob52eb37f4d8823a989a9551de3ff45022e882f6a4
1 //
2 // TraceSource.cs
3 //
4 // Author:
5 // Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (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.
31 #if NET_2_0
33 using System;
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Configuration;
38 namespace System.Diagnostics
40 public class TraceSource
42 SourceSwitch source_switch;
43 TraceListenerCollection listeners;
45 public TraceSource (string name)
46 : this (name, SourceLevels.Off)
50 public TraceSource (string name, SourceLevels sourceLevels)
52 if (name == null)
53 throw new ArgumentNullException ("name");
54 Hashtable sources = DiagnosticsConfiguration.Settings ["sources"] as Hashtable;
55 TraceSourceInfo info = sources != null ? sources [name] as TraceSourceInfo : null;
56 source_switch = new SourceSwitch (name);
58 if (info == null)
59 listeners = new TraceListenerCollection ();
60 else {
61 source_switch.Level = info.Levels;
62 listeners = info.Listeners;
66 public StringDictionary Attributes {
67 get { return source_switch.Attributes; }
70 public TraceListenerCollection Listeners {
71 get { return listeners; }
74 public string Name {
75 get { return source_switch.DisplayName; }
78 public SourceSwitch Switch {
79 get { return source_switch; }
80 set {
81 if (value == null)
82 throw new ArgumentNullException ("value");
83 source_switch = value;
87 public void Close ()
89 lock (((ICollection) listeners).SyncRoot) {
90 foreach (TraceListener tl in listeners)
91 tl.Close ();
95 public void Flush ()
97 lock (((ICollection) listeners).SyncRoot) {
98 foreach (TraceListener tl in listeners)
99 tl.Flush ();
103 [Conditional ("TRACE")]
104 public void TraceData (
105 TraceEventType eventType, int id, object data)
107 if (!source_switch.ShouldTrace (eventType))
108 return;
109 lock (((ICollection) listeners).SyncRoot) {
110 foreach (TraceListener tl in listeners)
111 tl.TraceData (null, Name, eventType, id, data);
115 [Conditional ("TRACE")]
116 public void TraceData (
117 TraceEventType eventType, int id, params object [] data)
119 if (!source_switch.ShouldTrace (eventType))
120 return;
121 lock (((ICollection) listeners).SyncRoot) {
122 foreach (TraceListener tl in listeners)
123 tl.TraceData (null, Name, eventType, id, data);
127 [Conditional ("TRACE")]
128 public void TraceEvent (TraceEventType eventType, int id)
130 if (!source_switch.ShouldTrace (eventType))
131 return;
132 lock (((ICollection) listeners).SyncRoot) {
133 foreach (TraceListener tl in listeners)
134 tl.TraceEvent (null, Name, eventType, id);
138 [Conditional ("TRACE")]
139 public void TraceEvent (TraceEventType eventType,
140 int id, string message)
142 if (!source_switch.ShouldTrace (eventType))
143 return;
144 lock (((ICollection) listeners).SyncRoot) {
145 foreach (TraceListener tl in listeners)
146 tl.TraceEvent (null, Name, eventType, id, message);
150 [Conditional ("TRACE")]
151 public void TraceEvent (TraceEventType eventType,
152 int id, string format, params object [] args)
154 if (!source_switch.ShouldTrace (eventType))
155 return;
156 lock (((ICollection) listeners).SyncRoot) {
157 foreach (TraceListener tl in listeners)
158 tl.TraceEvent (null, Name, eventType, id, format, args);
162 [Conditional ("TRACE")]
163 public void TraceInformation (string format)
165 TraceEvent (TraceEventType.Information, 0, format);
168 [Conditional ("TRACE")]
169 public void TraceInformation (
170 string format, params object [] args)
172 TraceEvent (TraceEventType.Information, 0, format, args);
175 [Conditional ("TRACE")]
176 public void TraceTransfer (int id, string message, Guid relatedActivityId)
178 if (!source_switch.ShouldTrace (TraceEventType.Transfer ))
179 return;
180 lock (((ICollection) listeners).SyncRoot) {
181 foreach (TraceListener tl in listeners)
182 tl.TraceTransfer (null, Name, id, message, relatedActivityId);
186 protected virtual string [] GetSupportedAttributes ()
188 return null;
193 #endif