다음을 통해 공유


TraceListener.GetSupportedAttributes 메서드

정의

추적 수신기에서 지원하는 사용자 지정 특성을 가져옵니다.

protected:
 virtual cli::array <System::String ^> ^ GetSupportedAttributes();
protected public:
 virtual cli::array <System::String ^> ^ GetSupportedAttributes();
protected virtual string[]? GetSupportedAttributes ();
protected virtual string[] GetSupportedAttributes ();
protected internal virtual string[] GetSupportedAttributes ();
abstract member GetSupportedAttributes : unit -> string[]
override this.GetSupportedAttributes : unit -> string[]
Protected Overridable Function GetSupportedAttributes () As String()
Protected Friend Overridable Function GetSupportedAttributes () As String()

반환

String[]

추적 수신기에서 지원하는 사용자 지정 특성의 이름을 지정하는 문자열 배열을 반환하며, 사용자 지정 특성이 없으면 null 을 반환합니다.

예제

다음 코드 샘플에서는 사용자 지정 추적 수신기에 GetSupportedAttributes 대한 메서드의 재정의를 보여 있습니다.

// This code example uses the following application configuration file:

//<?xml version="1.0" encoding="utf-8"?>
//      <source name="TraceTest" switchName="SourceSwitch" switchType="System.Diagnostics.SourceSwitch" >
//          <add name="Testlistener" />
//      <add name="SourceSwitch" value="Warning" />
//      <add name="Testlistener" type="CustomTraceListener.TestListener, CustomTraceListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" initializeData="TestListener" Source="test"/>
//    <trace autoflush="true" indentsize="4" />

using System;
using System.Diagnostics;
using System.Configuration;
using System.Reflection;
using System.Collections;
namespace CustomTraceListener
{
    class Program
    {
        static void Main(string[] args)
        {
            TraceSource ts = new TraceSource("TraceTest");
            Console.WriteLine(ts.Switch.DisplayName);
            foreach (TraceListener traceListener in ts.Listeners)
            {
                Console.Write("TraceListener: " + traceListener.Name + "\t");
                // The following output can be used to update the configuration file.
                Console.WriteLine("AssemblyQualifiedName = " +
                    (traceListener.GetType().AssemblyQualifiedName));
            }
            ts.TraceEvent(TraceEventType.Error, 1, "test error message");
        }
    }
    public class TestListener : TraceListener
    {
        string source;
        string name;
        public TestListener(string listenerName)
        {
            name = listenerName;
        }

        public string Source
        {
            get
            {
                foreach (DictionaryEntry de in this.Attributes)
                    if (de.Key.ToString().ToLower() == "source")
                        source = de.Value.ToString();
                return source;
            }
            set { source = value; }
        }

        public override void Write(string s)
        {
            Console.Write(name + " " + Source + ": " + s);
        }
        public override void WriteLine(string s)
        {
            Console.WriteLine(s);
        }
        protected override string[] GetSupportedAttributes()
        {
            return new string[] { "Source" };
        }
    }
}
// This code example creates the following output:
/*
SourceSwitch
TraceListener: Default  AssemblyQualifiedName = System.Diagnostics.DefaultTraceL
istener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e0
89
TraceListener: Testlistener     AssemblyQualifiedName = CustomTraceListener.Test
Listener, CustomTraceListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=
null
TestListener test: TraceTest Error: 1 : test error message
*/
' This code example uses the following application configuration file:
'<?xml version="1.0" encoding="utf-8"?>
'      <source name="TraceTest" switchName="SourceSwitch" switchType="System.Diagnostics.SourceSwitch" >
'          <add name="Testlistener" />
'      <add name="SourceSwitch" value="Warning" />
'      <add name="Testlistener" type="CustomTraceListener.TestListener, CustomTraceListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" initializeData="TestListener" Source="test"/>
'    <trace autoflush="true" indentsize="4" />
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")
            Console.WriteLine(ts.Switch.DisplayName)
            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
    End Class

    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


        Public Property [Source]() As String
            Get
                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

        Public Overrides Sub WriteLine(ByVal s As String)
            Console.WriteLine(s)

        End Sub

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

        End Function 'GetSupportedAttributes
    End Class
End Namespace 'CustomTraceListener
' This code example creates the following output:
'
'SourceSwitch
'TraceListener: Default  AssemblyQualifiedName = System.Diagnostics.DefaultTraceL
'istener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e0
'89
'TraceListener: Testlistener     AssemblyQualifiedName = CustomTraceListener.Test
'Listener, CustomTraceListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=
'null
'TestListener test: TraceTest Error: 1 : test error message
'

설명

에 대한 GetSupportedAttributes 기본 구현은 를 반환합니다 null.

상속자 참고

클래스 또는 파생 클래스에서 TraceListener 상속하는 경우 메서드를 재정의하여 클래스에 GetSupportedAttributes() 대한 사용자 지정 특성을 제공할 수 있습니다.

적용 대상