Export (0) Print
Expand All

TraceListener.GetSupportedAttributes Method

Gets the custom attributes supported by the trace listener.

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)

Protected Friend Overridable Function GetSupportedAttributes As String()

Return Value

Type: System.String()
A string array naming the custom attributes supported by the trace listener, or Nothing if there are no custom attributes.

The default implementation for GetSupportedAttributes returns Nothing.

Notes to Inheritors

When inheriting from the TraceListener class or a derived class, you can override the GetSupportedAttributes method to provide custom attributes for your class.

The following code sample shows an override of the GetSupportedAttributes method for a custom trace listener.

' This code example uses the following application configuration file: 
'<?xml version="1.0" encoding="utf-8"?> 
'  <system.diagnostics> 
'    <sources> 
'      <source name="TraceTest" switchName="SourceSwitch" switchType="System.Diagnostics.SourceSwitch" > 
'        <listeners> 
'          <add name="Testlistener" /> 
'        </listeners> 
'      </source> 
'    </sources> 
'    <switches> 
'      <add name="SourceSwitch" value="Warning" /> 
'    </switches> 
'    <sharedListeners> 
'      <add name="Testlistener" type="CustomTraceListener.TestListener, CustomTraceListener, Version=, Culture=neutral, PublicKeyToken=null" initializeData="TestListener" Source="test"/> 
'    </sharedListeners> 
'    <trace autoflush="true" indentsize="4" /> 
'  </system.diagnostics> 
Imports System
Imports System.Diagnostics
Imports System.Configuration
Imports System.Reflection
Imports System.Collections

Namespace CustomTraceListener

    Class Program

        Shared Sub Main(ByVal args() As String)
            Dim ts As New TraceSource("TraceTest")
            Dim traceListener As TraceListener
            For Each traceListener In ts.Listeners
                Console.Write("TraceListener: " + traceListener.Name + vbTab)
                ' The following output can be used to update the configuration file.
                Console.WriteLine("AssemblyQualifiedName = " + traceListener.GetType().AssemblyQualifiedName)
            Next traceListener
            ts.TraceEvent(TraceEventType.Error, 1, "test error message")

        End Sub 'Main
    End Class 'Program

    Public Class TestListener
        Inherits TraceListener
        Private [m_source] As String 
        Private m_name As String 

        Public Sub New(ByVal listenerName As String)
            m_name = listenerName
        End Sub 'New 

        Public Property [Source]() As String 
                Dim de As DictionaryEntry
                For Each de In Me.Attributes
                    If de.Key.ToString().ToLower() = "source" Then
                        [Source] = de.Value.ToString()
                    End If 
                Next de
                Return [m_source]
            End Get 
            Set(ByVal value As String)
                [m_source] = value
            End Set 
        End Property 

        Public Overrides Sub Write(ByVal s As String)
            Console.Write(m_name + " " + [Source] + ": " + s)

        End Sub 'Write

        Public Overrides Sub WriteLine(ByVal s As String)

        End Sub 'WriteLine

        Protected Overrides Function GetSupportedAttributes() As String()
            Return New String() {"Source"}

        End Function 'GetSupportedAttributes
    End Class 'TestListener 
End Namespace 'CustomTraceListener
' This code example creates the following output: 

'TraceListener: Default  AssemblyQualifiedName = System.Diagnostics.DefaultTraceL 
'istener, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e0 
'TraceListener: Testlistener     AssemblyQualifiedName = CustomTraceListener.Test 
'Listener, CustomTraceListener, Version=, Culture=neutral, PublicKeyToken= 
'TestListener test: TraceTest Error: 1 : test error message 

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
© 2015 Microsoft