2 // System.Diagnostics.TraceListenerCollection.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation
8 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
10 // (C) 2002 Jonathan Pryor
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System
.Collections
;
37 using System
.Diagnostics
;
38 using System
.Globalization
;
40 namespace System
.Diagnostics
{
42 public class TraceListenerCollection
: IList
, ICollection
, IEnumerable
{
44 private ArrayList listeners
= ArrayList
.Synchronized (new ArrayList (1));
46 internal TraceListenerCollection ()
51 internal TraceListenerCollection (bool addDefault
)
54 Add (new DefaultTraceListener ());
58 get {return listeners.Count;}
61 public TraceListener
this [string name
] {
63 lock (listeners
.SyncRoot
) {
64 foreach (TraceListener listener
in listeners
) {
65 if (listener
.Name
== name
)
73 public TraceListener
this [int index
] {
74 get {return (TraceListener) listeners[index];}
76 InitializeListener (value);
77 listeners
[index
] = value;
81 object IList
.this [int index
] {
82 get {return listeners[index];}
84 TraceListener l
= (TraceListener
) value;
85 InitializeListener (l
);
90 bool ICollection
.IsSynchronized
{
91 get {return listeners.IsSynchronized;}
94 object ICollection
.SyncRoot
{
95 get {return listeners.SyncRoot;}
98 bool IList
.IsFixedSize
{
99 get {return listeners.IsFixedSize;}
102 bool IList
.IsReadOnly
{
103 get {return listeners.IsReadOnly;}
106 public int Add (TraceListener listener
)
108 InitializeListener (listener
);
109 return listeners
.Add (listener
);
112 internal void Add (TraceListener listener
, TraceImplSettings settings
)
114 listener
.IndentLevel
= settings
.IndentLevel
;
115 listener
.IndentSize
= settings
.IndentSize
;
116 listeners
.Add (listener
);
119 private void InitializeListener (TraceListener listener
)
121 listener
.IndentLevel
= TraceImpl
.IndentLevel
;
122 listener
.IndentSize
= TraceImpl
.IndentSize
;
125 private void InitializeRange (IList listeners
)
127 int e
= listeners
.Count
;
128 for (int i
= 0; i
!= e
; ++i
)
130 (TraceListener
) listeners
[i
]);
133 public void AddRange (TraceListener
[] value)
135 InitializeRange (value);
136 listeners
.AddRange (value);
139 public void AddRange (TraceListenerCollection
value)
141 InitializeRange (value);
142 listeners
.AddRange (value.listeners
);
150 public bool Contains (TraceListener listener
)
152 return listeners
.Contains (listener
);
155 public void CopyTo (TraceListener
[] listeners
, int index
)
157 listeners
.CopyTo (listeners
, index
);
160 public IEnumerator
GetEnumerator ()
162 return listeners
.GetEnumerator ();
165 void ICollection
.CopyTo (Array array
, int index
)
167 listeners
.CopyTo (array
, index
);
170 int IList
.Add (object value)
172 if (value is TraceListener
)
173 return Add ((TraceListener
) value);
174 throw new NotSupportedException (Locale
.GetText (
175 "You can only add TraceListener objects to the collection"));
178 bool IList
.Contains (object value)
180 if (value is TraceListener
)
181 return listeners
.Contains (value);
185 int IList
.IndexOf (object value)
187 if (value is TraceListener
)
188 return listeners
.IndexOf (value);
192 void IList
.Insert (int index
, object value)
194 if (value is TraceListener
) {
195 Insert (index
, (TraceListener
) value);
198 throw new NotSupportedException (Locale
.GetText (
199 "You can only insert TraceListener objects into the collection"));
202 void IList
.Remove (object value)
204 if (value is TraceListener
)
205 listeners
.Remove (value);
208 public int IndexOf (TraceListener listener
)
210 return listeners
.IndexOf (listener
);
213 public void Insert (int index
, TraceListener listener
)
215 InitializeListener (listener
);
216 listeners
.Insert (index
, listener
);
219 public void Remove (string name
)
221 TraceListener found
= null;
223 lock (listeners
.SyncRoot
) {
224 foreach (TraceListener listener
in listeners
) {
225 if (listener
.Name
== name
) {
232 listeners
.Remove (found
);
234 throw new ArgumentException (Locale
.GetText (
235 "TraceListener " + name
+ " was not in the collection"));
239 public void Remove (TraceListener listener
)
241 listeners
.Remove (listener
);
244 public void RemoveAt (int index
)
246 listeners
.RemoveAt (index
);