5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright (C) 2007 Novell, Inc.
9 // Copyright 2013 Xamarin 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 defaultLevel)
51 throw new ArgumentNullException ("name");
53 throw new ArgumentException ("name");
55 Hashtable sources = DiagnosticsConfiguration.Settings ["sources"] as Hashtable;
56 TraceSourceInfo info = sources != null ? sources [name] as TraceSourceInfo : null;
57 source_switch = new SourceSwitch (name);
60 listeners = new TraceListenerCollection ();
61 source_switch.Level = defaultLevel;
63 source_switch.Level = info.Levels;
64 listeners = info.Listeners;
68 public StringDictionary Attributes {
69 get { return source_switch.Attributes; }
72 public TraceListenerCollection Listeners {
73 get { return listeners; }
77 get { return source_switch.DisplayName; }
80 public SourceSwitch Switch {
81 get { return source_switch; }
84 throw new ArgumentNullException ("value");
85 source_switch = value;
91 lock (((ICollection) listeners).SyncRoot) {
92 foreach (TraceListener tl in listeners)
99 lock (((ICollection) listeners).SyncRoot) {
100 foreach (TraceListener tl in listeners)
105 [Conditional ("TRACE")]
106 public void TraceData (
107 TraceEventType eventType, int id, object data)
109 if (!source_switch.ShouldTrace (eventType))
111 lock (((ICollection) listeners).SyncRoot) {
112 foreach (TraceListener tl in listeners)
113 tl.TraceData (new TraceEventCache(), Name, eventType, id, data);
117 [Conditional ("TRACE")]
118 public void TraceData (
119 TraceEventType eventType, int id, params object [] data)
121 if (!source_switch.ShouldTrace (eventType))
123 lock (((ICollection) listeners).SyncRoot) {
124 foreach (TraceListener tl in listeners)
125 tl.TraceData (new TraceEventCache(), Name, eventType, id, data);
129 [Conditional ("TRACE")]
130 public void TraceEvent (TraceEventType eventType, int id)
132 if (!source_switch.ShouldTrace (eventType))
134 lock (((ICollection) listeners).SyncRoot) {
135 foreach (TraceListener tl in listeners)
136 tl.TraceEvent (new TraceEventCache(), Name, eventType, id);
140 [Conditional ("TRACE")]
141 public void TraceEvent (TraceEventType eventType,
142 int id, string message)
144 if (!source_switch.ShouldTrace (eventType))
146 lock (((ICollection) listeners).SyncRoot) {
147 foreach (TraceListener tl in listeners)
148 tl.TraceEvent (new TraceEventCache(), Name, eventType, id, message);
152 [Conditional ("TRACE")]
153 public void TraceEvent (TraceEventType eventType,
154 int id, string format, params object [] args)
156 if (!source_switch.ShouldTrace (eventType))
158 lock (((ICollection) listeners).SyncRoot) {
159 foreach (TraceListener tl in listeners)
160 tl.TraceEvent (new TraceEventCache(), Name, eventType, id, format, args);
164 [Conditional ("TRACE")]
165 public void TraceInformation (string format)
167 TraceEvent (TraceEventType.Information, 0, format);
170 [Conditional ("TRACE")]
171 public void TraceInformation (
172 string format, params object [] args)
174 TraceEvent (TraceEventType.Information, 0, format, args);
177 [Conditional ("TRACE")]
178 public void TraceTransfer (int id, string message, Guid relatedActivityId)
180 if (!source_switch.ShouldTrace (TraceEventType.Transfer ))
182 lock (((ICollection) listeners).SyncRoot) {
183 foreach (TraceListener tl in listeners)
184 tl.TraceTransfer (new TraceEventCache(), Name, id, message, relatedActivityId);
188 protected virtual string [] GetSupportedAttributes ()