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 = new ArrayList ();
46 internal TraceListenerCollection ()
48 Add (new DefaultTraceListener ());
52 get {return listeners.Count;}
55 public TraceListener this [string name] {
57 foreach (TraceListener listener in listeners) {
58 if (listener.Name == name)
65 public TraceListener this [int index] {
66 get {return (TraceListener) listeners[index];}
68 InitializeListener (value);
69 listeners[index] = value;
73 object IList.this [int index] {
74 get {return listeners[index];}
76 this[index] = (TraceListener) value;
80 bool ICollection.IsSynchronized {
81 get {return listeners.IsSynchronized;}
84 object ICollection.SyncRoot {
85 get {return listeners.SyncRoot;}
88 bool IList.IsFixedSize {
89 get {return listeners.IsFixedSize;}
92 bool IList.IsReadOnly {
93 get {return listeners.IsReadOnly;}
96 public int Add (TraceListener listener)
98 InitializeListener (listener);
99 return listeners.Add (listener);
102 private void InitializeListener (TraceListener listener)
104 listener.IndentLevel = TraceImpl.IndentLevel;
105 listener.IndentSize = TraceImpl.IndentSize;
108 private void InitializeRange (IList listeners)
110 int e = listeners.Count;
111 for (int i = 0; i != e; ++i)
113 (TraceListener) listeners[i]);
116 public void AddRange (TraceListener[] value)
118 InitializeRange (value);
119 listeners.AddRange (value);
122 public void AddRange (TraceListenerCollection value)
124 InitializeRange (value);
125 listeners.AddRange (value.listeners);
133 public bool Contains (TraceListener listener)
135 return listeners.Contains (listener);
138 public void CopyTo (TraceListener[] listeners, int index)
140 listeners.CopyTo (listeners, index);
143 public IEnumerator GetEnumerator ()
145 return listeners.GetEnumerator ();
148 void ICollection.CopyTo (Array array, int index)
150 listeners.CopyTo (array, index);
153 int IList.Add (object value)
155 if (value is TraceListener)
156 return Add ((TraceListener) value);
157 throw new NotSupportedException (Locale.GetText (
158 "You can only add TraceListener objects to the collection"));
161 bool IList.Contains (object value)
163 if (value is TraceListener)
164 return listeners.Contains (value);
168 int IList.IndexOf (object value)
170 if (value is TraceListener)
171 return listeners.IndexOf (value);
175 void IList.Insert (int index, object value)
177 if (value is TraceListener) {
178 Insert (index, (TraceListener) value);
181 throw new NotSupportedException (Locale.GetText (
182 "You can only insert TraceListener objects into the collection"));
185 void IList.Remove (object value)
187 if (value is TraceListener)
188 listeners.Remove (value);
191 public int IndexOf (TraceListener listener)
193 return listeners.IndexOf (listener);
196 public void Insert (int index, TraceListener listener)
198 InitializeListener (listener);
199 listeners.Insert (index, listener);
202 public void Remove (string name)
204 TraceListener found = null;
206 foreach (TraceListener listener in listeners) {
207 if (listener.Name == name) {
214 listeners.Remove (found);
216 throw new ArgumentException (Locale.GetText (
217 "TraceListener " + name + " was not in the collection"));
220 public void Remove (TraceListener listener)
222 listeners.Remove (listener);
225 public void RemoveAt (int index)
227 listeners.RemoveAt (index);