System.Diagnostics.TraceListenerCollection Class

Assembly: System.dll
Namespace: System.Diagnostics
Summary
Provides a thread-safe list of TraceListener objects.
C# Syntax:
public class TraceListenerCollection : IList, ICollection, IEnumerable
Remarks
The TraceListenerCollection list is used to specify the output listeners for the Trace.Listeners and the Debug.Listeners collections. You cannot create an instance of this class.

This list is thread-safe, however the methods used to access the list and the enumerator do not take synchronization locks. Instead, the collection is copied, the copy is modified, and a reference is set to the copy of the collection. Methods like TraceListenerCollection.Add, TraceListenerCollection.Remove, and TraceListenerCollection.Clear modify the elements in the collection.

The TraceListenerCollection class provides the TraceListenerCollection.Count property for information about the list. It also provides the following methods: TraceListenerCollection.Contains, TraceListenerCollection.GetEnumerator, TraceListenerCollection.IndexOf.

This class also provides the following methods to modify the list: TraceListenerCollection.Add, TraceListenerCollection.Clear, TraceListenerCollection.Insert, and TraceListenerCollection.Remove. The TraceListenerCollection.CopyTo method copies a part of the list to an array. The TraceListenerCollection.RemoveAt method deletes the list member at a specified index number.

Example
The following example creates a TextWriterTraceListener that writes to the console screen. The code then adds the new listener to the Trace.Listeners.
/* Create a listener, which outputs to the console screen, and 
  * add it to the trace listeners. */
 TextWriterTraceListener myWriter = new 
    TextWriterTraceListener(System.Console.Out);
 Trace.Listeners.Add(myWriter);


    
See also:
System.Diagnostics Namespace | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

System.Diagnostics.TraceListenerCollection Member List:

Public Properties
Count Read-only

Gets the number of listeners in the list.
Item Read-only

Overloaded:
Item[string name] {get

Gets the first TraceListener in the list with the specified name.
Item Read-write

Overloaded:
Item[int i] {get

Gets or sets the TraceListener at the specified index.
Public Methods
Add Adds a TraceListener to the list.
AddRange Overloaded:
AddRange(TraceListener[] value)

Adds an array of TraceListener objects to the list.
AddRange Overloaded:
AddRange(TraceListenerCollection value)

Adds the contents of another TraceListenerCollection to the list.
Clear Clears all the listeners from the list.
Contains Checks whether the list contains the specified listener.
CopyTo Copies a section of the current TraceListenerCollection list to the specified array at the specified index.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetEnumerator Gets an enumerator for this list.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
IndexOf Gets the index of the specified listener.
Insert Inserts the listener at the specified index.
Remove Overloaded:
Remove(string name)

Removes from the collection the first TraceListener with the specified name.
Remove Overloaded:
Remove(TraceListener listener)

Removes from the collection the specified TraceListener.
RemoveAt Removes from the collection the TraceListener at the specified index.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Diagnostics.TraceListenerCollection Member Details

Property: Count (read-only)
Summary
Gets the number of listeners in the list.
C# Syntax:
public int Count {get;}
Implements:
ICollection.Count
See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Overloaded Property: Item (read-only)
Summary
Gets the first TraceListener in the list with the specified name.
C# Syntax:
public TraceListener this[string name] {get;}
Parameters:

name

The name of the TraceListener to get from the list.

Remarks
The TraceListenerCollection.Item property is case-sensitive when searching for names. That is, if two listeners exist with the names "Lname" and "lname", TraceListenerCollection.Item property will find only the TraceListener with the TraceListener.Name that you specify, not both.
See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Overloaded Property: Item (read-write)
Summary
Gets or sets the TraceListener at the specified index.
C# Syntax:
public TraceListener this[int i] {get; set;}
Parameters:

i

The zero-based index of the TraceListener to get from the list.

Remarks
The index is zero-based. Therefore, you must subtract one from the numerical position of a particular TraceListener to access that TraceListener. For example, to get the third TraceListener, you need to specify myTraceListenerColl[2] .
See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Method: Add(
   TraceListener listener
)
Summary
Adds a TraceListener to the list.
C# Syntax:
public int Add(
   TraceListener listener
);
Parameters:

listener

A TraceListener to add to the list.

Return Value:
The position at which the new listener was inserted.
Example
The following example creates a TextWriterTraceListener that outputs to the console screen. The code then adds the new listener to the Trace.Listeners.
/* Create a listener, which outputs to the console screen, and 
  * add it to the trace listeners. */
 TextWriterTraceListener myWriter = new TextWriterTraceListener();
 myWriter.Writer = System.Console.Out;
 Trace.Listeners.Add(myWriter);
 

    
See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Overloaded Method: AddRange(
   TraceListener[] value
)
Summary
Adds an array of TraceListener objects to the list.
C# Syntax:
public void AddRange(
   TraceListener[] value
);
Parameters:

value

An array of TraceListener objects to add to the list.

See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Overloaded Method: AddRange(
   TraceListenerCollection value
)
Summary
Adds the contents of another TraceListenerCollection to the list.
C# Syntax:
public void AddRange(
   TraceListenerCollection value
);
Parameters:

value

Another TraceListenerCollection whose contents are added to the list.

See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Method: Clear()
Summary
Clears all the listeners from the list.
C# Syntax:
public void Clear();
Implements:
IList.Clear
See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Method: Contains(
   TraceListener listener
)
Summary
Checks whether the list contains the specified listener.
C# Syntax:
public bool Contains(
   TraceListener listener
);
Parameters:

listener

A TraceListener to find in the list.

Return Value:
true if the listener is in the list; otherwise, false.
Remarks
The TraceListenerCollection.Contains method can confirm the existence of a TraceListener before you perform further operations.
See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Method: CopyTo(
   TraceListener[] listeners,
   int index
)
Summary
Copies a section of the current TraceListenerCollection list to the specified array at the specified index.
C# Syntax:
public void CopyTo(
   TraceListener[] listeners,
   int index
);
Parameters:

listeners

An array of type Array to copy the elements into. An array of type Array to copy the elements into.

index

The starting index number in the current list to copy from.

See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~TraceListenerCollection();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetEnumerator()
Summary
Gets an enumerator for this list.
C# Syntax:
public IEnumerator GetEnumerator();
Return Value:
An enumerator of type IEnumerator.
Implements:
IEnumerable.GetEnumerator
See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: IndexOf(
   TraceListener listener
)
Summary
Gets the index of the specified listener.
C# Syntax:
public int IndexOf(
   TraceListener listener
);
Parameters:

listener

A TraceListener to find in the list.

Return Value:
The index of the listener, if it can be found in the list; otherwise, -1.
See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Method: Insert(
   int index,
   TraceListener listener
)
Summary
Inserts the listener at the specified index.
C# Syntax:
public void Insert(
   int index,
   TraceListener listener
);
Parameters:

index

The position in the list to insert the new TraceListener. The position in the list to insert the new TraceListener.

listener

A TraceListener to insert in the list.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException The index is not a valid index in the list.
Remarks
If the index equals the number of items in the list, then the listener is appended to the end of the list.

The index is zero-based. Therefore, if you want to insert the listener into the third position, you must call myTraceListenerColl.Insert(2, myNewListener) .

See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: Remove(
   string name
)
Summary
Removes from the collection the first TraceListener with the specified name.
C# Syntax:
public void Remove(
   string name
);
Parameters:

name

The name of the TraceListener to remove from the list.

Exceptions
Exception Type Condition
ArgumentException A listener with the given name does not exist in the list.
Remarks
The name parameter is case-sensitive. That is, if two listeners exist with the names "Rname" and "rname", the TraceListenerCollection.Remove method will find the specific listener you name.
See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Overloaded Method: Remove(
   TraceListener listener
)
Summary
Removes from the collection the specified TraceListener.
C# Syntax:
public void Remove(
   TraceListener listener
);
Parameters:

listener

A TraceListener to remove from the list.

Exceptions
Exception Type Condition
ArgumentException The listener does not exist in the list.
See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Method: RemoveAt(
   int index
)
Summary
Removes from the collection the TraceListener at the specified index.
C# Syntax:
public void RemoveAt(
   int index
);
Parameters:

index

The zero-based index of the TraceListener to remove from the list. The zero-based index of the TraceListener to remove from the list.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException The index is not a valid index in the list.
Implements:
IList.RemoveAt
Remarks
The index is zero-based. Therefore, you must subtract one from the numerical position of a particular TraceListener to delete that TraceListener. For example, to remove the third TraceListener, you need to specify myTraceListenerColl.RemoveAt(2) .
See also:
TraceListenerCollection | TraceListener | DefaultTraceListener | TextWriterTraceListener | Debug | Trace

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.