TraceListener.GetSupportedAttributes Method
.NET Framework 4
Gets the custom attributes supported by the trace listener.
Assembly: System (in System.dll)
The default implementation for GetSupportedAttributes returns null.
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"?> //<configuration> // <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=1.0.0.0, Culture=neutral, PublicKeyToken=null" initializeData="TestListener" Source="test"/> // </sharedListeners> // <trace autoflush="true" indentsize="4" /> // </system.diagnostics> //</configuration> 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 */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Error in Example Code
There is an error in the c# example code.
The Source property exposes a setter. However, this setter will not work, as the getter pulls the return value from the dictionary on every retrieval. Thus, the values assigned by the setter to the private field "source" are lost upon retreival:
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; }
}
The Source property exposes a setter. However, this setter will not work, as the getter pulls the return value from the dictionary on every retrieval. Thus, the values assigned by the setter to the private field "source" are lost upon retreival:
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; }
}
- 1/14/2011
- Pieter Muller
- 1/14/2011
- Pieter Muller