5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (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.
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Configuration;
36 namespace System.Diagnostics
38 public class TraceSource
40 SourceSwitch source_switch;
41 TraceListenerCollection listeners;
43 public TraceSource (string name)
44 : this (name, SourceLevels.Off)
48 public TraceSource (string name, SourceLevels sourceLevels)
51 throw new ArgumentNullException ("name");
52 Hashtable sources = DiagnosticsConfiguration.Settings ["sources"] as Hashtable;
53 TraceSourceInfo info = sources != null ? sources [name] as TraceSourceInfo : null;
54 source_switch = new SourceSwitch (name);
57 listeners = new TraceListenerCollection ();
59 source_switch.Level = info.Levels;
60 listeners = info.Listeners;
64 public StringDictionary Attributes {
65 get { return source_switch.Attributes; }
68 public TraceListenerCollection Listeners {
69 get { return listeners; }
73 get { return source_switch.DisplayName; }
76 public SourceSwitch Switch {
77 get { return source_switch; }
80 throw new ArgumentNullException ("value");
81 source_switch = value;
87 lock (((ICollection) listeners).SyncRoot) {
88 foreach (TraceListener tl in listeners)
95 lock (((ICollection) listeners).SyncRoot) {
96 foreach (TraceListener tl in listeners)
101 [Conditional ("TRACE")]
102 public void TraceData (
103 TraceEventType eventType, int id, object data)
105 if (!source_switch.ShouldTrace (eventType))
107 lock (((ICollection) listeners).SyncRoot) {
108 foreach (TraceListener tl in listeners)
109 tl.TraceData (new TraceEventCache(), Name, eventType, id, data);
113 [Conditional ("TRACE")]
114 public void TraceData (
115 TraceEventType eventType, int id, params object [] data)
117 if (!source_switch.ShouldTrace (eventType))
119 lock (((ICollection) listeners).SyncRoot) {
120 foreach (TraceListener tl in listeners)
121 tl.TraceData (new TraceEventCache(), Name, eventType, id, data);
125 [Conditional ("TRACE")]
126 public void TraceEvent (TraceEventType eventType, int id)
128 if (!source_switch.ShouldTrace (eventType))
130 lock (((ICollection) listeners).SyncRoot) {
131 foreach (TraceListener tl in listeners)
132 tl.TraceEvent (new TraceEventCache(), Name, eventType, id);
136 [Conditional ("TRACE")]
137 public void TraceEvent (TraceEventType eventType,
138 int id, string message)
140 if (!source_switch.ShouldTrace (eventType))
142 lock (((ICollection) listeners).SyncRoot) {
143 foreach (TraceListener tl in listeners)
144 tl.TraceEvent (new TraceEventCache(), Name, eventType, id, message);
148 [Conditional ("TRACE")]
149 public void TraceEvent (TraceEventType eventType,
150 int id, string format, params object [] args)
152 if (!source_switch.ShouldTrace (eventType))
154 lock (((ICollection) listeners).SyncRoot) {
155 foreach (TraceListener tl in listeners)
156 tl.TraceEvent (new TraceEventCache(), Name, eventType, id, format, args);
160 [Conditional ("TRACE")]
161 public void TraceInformation (string format)
163 TraceEvent (TraceEventType.Information, 0, format);
166 [Conditional ("TRACE")]
167 public void TraceInformation (
168 string format, params object [] args)
170 TraceEvent (TraceEventType.Information, 0, format, args);
173 [Conditional ("TRACE")]
174 public void TraceTransfer (int id, string message, Guid relatedActivityId)
176 if (!source_switch.ShouldTrace (TraceEventType.Transfer ))
178 lock (((ICollection) listeners).SyncRoot) {
179 foreach (TraceListener tl in listeners)
180 tl.TraceTransfer (new TraceEventCache(), Name, id, message, relatedActivityId);
184 protected virtual string [] GetSupportedAttributes ()