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.
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
)
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
);
59 listeners
= new TraceListenerCollection ();
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; }
75 get { return source_switch.DisplayName; }
78 public SourceSwitch Switch
{
79 get { return source_switch; }
82 throw new ArgumentNullException ("value");
83 source_switch
= value;
89 lock (((ICollection
) listeners
).SyncRoot
) {
90 foreach (TraceListener tl
in listeners
)
97 lock (((ICollection
) listeners
).SyncRoot
) {
98 foreach (TraceListener tl
in listeners
)
103 [Conditional ("TRACE")]
104 public void TraceData (
105 TraceEventType eventType
, int id
, object data
)
107 if (!source_switch
.ShouldTrace (eventType
))
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
))
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
))
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
))
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
))
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
))
180 lock (((ICollection
) listeners
).SyncRoot
) {
181 foreach (TraceListener tl
in listeners
)
182 tl
.TraceTransfer (null, Name
, id
, message
, relatedActivityId
);
186 protected virtual string [] GetSupportedAttributes ()