XmlWriterTraceListener Class
 

Directs tracing or debugging output as XML-encoded data to a TextWriter or to a Stream, such as a FileStream.

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

Inheritance Hierarchy
SystemObject
   SystemMarshalByRefObject
     System.DiagnosticsTraceListener
       System.DiagnosticsTextWriterTraceListener
        System.DiagnosticsXmlWriterTraceListener

Syntax
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true)]
public class XmlWriterTraceListener : TextWriterTraceListener
[HostProtectionAttribute(SecurityAction::LinkDemand, Synchronization = true)]
public ref class XmlWriterTraceListener : TextWriterTraceListener
[<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true)>]
type XmlWriterTraceListener = 
    class
        inherit TextWriterTraceListener
    end
<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization := True)>
Public Class XmlWriterTraceListener
	Inherits TextWriterTraceListener
Constructors
NameDescription
System_CAPS_pubmethod XmlWriterTraceListener

Initializes a new instance of the XmlWriterTraceListener class, using the specified stream as the recipient of the debugging and tracing output.

System_CAPS_pubmethod XmlWriterTraceListener

Initializes a new instance of the XmlWriterTraceListener class with the specified name, using the specified stream as the recipient of the debugging and tracing output.

System_CAPS_pubmethod XmlWriterTraceListener

Initializes a new instance of the XmlWriterTraceListener class, using the specified file as the recipient of the debugging and tracing output.

System_CAPS_pubmethod XmlWriterTraceListener

Initializes a new instance of the XmlWriterTraceListener class with the specified name, using the specified file as the recipient of the debugging and tracing output.

System_CAPS_pubmethod XmlWriterTraceListener

Initializes a new instance of the XmlWriterTraceListener class using the specified writer as the recipient of the debugging and tracing output.

System_CAPS_pubmethod XmlWriterTraceListener

Initializes a new instance of the XmlWriterTraceListener class with the specified name, using the specified writer as the recipient of the debugging and tracing output.

Properties
NameDescription
System_CAPS_pubproperty

Gets the custom trace listener attributes defined in the application configuration file.(Inherited from TraceListener.)

System_CAPS_pubproperty

Gets and sets the trace filter for the trace listener.(Inherited from TraceListener.)

System_CAPS_pubproperty

Gets or sets the indent level.(Inherited from TraceListener.)

System_CAPS_pubproperty

Gets or sets the number of spaces in an indent.(Inherited from TraceListener.)

System_CAPS_pubproperty

Gets a value indicating whether the trace listener is thread safe. (Inherited from TraceListener.)

System_CAPS_pubproperty

Gets or sets a name for this TraceListener.(Inherited from TraceListener.)

System_CAPS_protproperty

Gets or sets a value indicating whether to indent the output.(Inherited from TraceListener.)

System_CAPS_pubproperty

Gets or sets the trace output options.(Inherited from TraceListener.)

System_CAPS_pubproperty

Gets or sets the text writer that receives the tracing or debugging output.(Inherited from TextWriterTraceListener.)

Methods
NameDescription
System_CAPS_pubmethod Close

Closes the for this listener so that it no longer receives tracing or debugging output.(Overrides TextWriterTraceListenerClose.)

System_CAPS_pubmethod CreateObjRef

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethod Dispose

Releases all resources used by the TraceListener.(Inherited from TraceListener.)

System_CAPS_protmethod Dispose

This API supports the product infrastructure and is not intended to be used directly from your code. Disposes this TextWriterTraceListener object.(Inherited from TextWriterTraceListener.)

System_CAPS_pubmethod Equals

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethod Fail

Emits an error message to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_pubmethod Fail

Writes trace information including an error message and a detailed error message to the file or stream.(Overrides TraceListenerFail.)

System_CAPS_protmethod Finalize

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethod Flush

Flushes the output buffer for the .(Inherited from TextWriterTraceListener.)

System_CAPS_pubmethod GetHashCode

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethod GetLifetimeService

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethod GetSupportedAttributes

Gets the custom attributes supported by the trace listener.(Inherited from TraceListener.)

System_CAPS_pubmethod GetType

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethod InitializeLifetimeService

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethod MemberwiseClone

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethod MemberwiseClone

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethod ToString

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethod TraceData

Writes trace information, a data object, and event information to the file or stream.(Overrides TraceListenerTraceData.)

System_CAPS_pubmethod TraceData

Writes trace information, data objects, and event information to the file or stream.(Overrides TraceListenerTraceData.)

System_CAPS_pubmethod TraceEvent

Writes trace and event information to the listener specific output.(Inherited from TraceListener.)

System_CAPS_pubmethod TraceEvent

Writes trace information, a message, and event information to the file or stream.(Overrides TraceListenerTraceEvent.)

System_CAPS_pubmethod TraceEvent

Writes trace information, a formatted message, and event information to the file or stream.(Overrides TraceListenerTraceEvent.)

System_CAPS_pubmethod TraceTransfer

Writes trace information including the identity of a related activity, a message, and event information to the file or stream.(Overrides TraceListenerTraceTransfer.)

System_CAPS_pubmethod Write

Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_pubmethod Write

Writes a category name and the value of the object's ToString method to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_pubmethod Write

Writes a verbatim message without any additional context information to the file or stream.(Overrides TextWriterTraceListenerWrite.)

System_CAPS_pubmethod Write

Writes a category name and a message to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_protmethod WriteIndent

Writes the indent to the listener you create when you implement this class, and resets the property to false.(Inherited from TraceListener.)

System_CAPS_pubmethod WriteLine

Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class, followed by a line terminator.(Inherited from TraceListener.)

System_CAPS_pubmethod WriteLine

Writes a category name and the value of the object's ToString method to the listener you create when you implement the TraceListener class, followed by a line terminator.(Inherited from TraceListener.)

System_CAPS_pubmethod WriteLine

Writes a verbatim message without any additional context information followed by the current line terminator to the file or stream.(Overrides TextWriterTraceListenerWriteLine.)

System_CAPS_pubmethod WriteLine

Writes a category name and a message to the listener you create when you implement the TraceListener class, followed by a line terminator.(Inherited from TraceListener.)

Remarks
System_CAPS_noteNote

You must have unmanaged code permission to create an instance of the XmlWriterTraceListener class.

The XmlWriterTraceListener class converts tracing and debugging information into an XML-encoded text stream. The description of the XML output is shown in the tables that follow. You can use the Service Trace Viewer Tool (SvcTraceViewer.exe) to display the XML output.

You can enable or disable an XmlWriterTraceListener through the application configuration file and then use the configured XmlWriterTraceListener in your application. Alternately, you can create an XmlWriterTraceListener in your code.

To configure an XmlWriterTraceListener, edit the configuration file that corresponds to the name of your application. In this file, you can add or remove a listener, set the properties for a listener, or remove listeners. The configuration file should be formatted like the following example.

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="xmlListener" 
          type="System.Diagnostics.XmlWriterTraceListener" 
          initializeData="xmlOutput.xml" 
          traceOutputOptions="ProcessId, DateTime" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

The XmlWriterTraceListener class inherits the property from the base class TraceListener. The property allows an additional level of trace output filtering at the listener. If there is a filter present, the Trace methods of the trace listener call the ShouldTrace method of the filter to determine whether to emit the trace.

System_CAPS_noteNote

If an attempt is made to write to a file that is in use or unavailable, the file name is automatically prefixed by a GUID.

System_CAPS_noteNote

Listener methods are intended to be called by methods of the Debug, Trace, and TraceSource classes. The listener methods are not intended to be called directly from application code. The XmlWriterTraceListener listener is primarily intended for use by the TraceSource class. The Write and WriteLine methods can be called by the Trace and Debug classes, and default values are provided for the XML elements that Trace and Debug do not supply values for.

The following table describes the elements and attributes of the XML output.

Element

Attributes

Output

Notes

CallStack

None

Depends on the presence of the Callstack flag in the property.

Special characters such as > or < are replaced with escape sequences. See the escaped character translation table that follows.

Computer

None

Always present.

The value of the property.

Correlation

ActivityID

Always present

If not specified, the default is an empty GUID.

RelatedActivityID

Depends on the presence of the relatedActivityId parameter in the Trace method call.

The relatedActivityId parameter of the TraceTransfer method.

DataItem

None

Depends on the data parameter of the TraceData method.

This element can contain an array of elements or one element, so the values are written as a set of DataItem nodes under the TraceData element.

The data output uses the ToString method of the passed-in data objects.

EventID

None

Always present.

Parameter input (id).

Execution

ProcessName

Always present.

From the TraceEventCache.

ProcessID

Always present.

From the TraceEventCache. On Windows 98 and Windows Me operating systems, if ProcessID is greater than 2,147,483,647 it is a positive representation of a negative number and should be converted to obtain the correct process identifier.

ThreadID

Always present.

From the TraceEventCache.

Level

None

Always present.

Parameter input (the numeric value of eventType). Parameter values greater than 255 are output as 255.

LogicalOperationStack

None

Depends on the presence of the LogicalOperationStack flag in the property.

There can be more than one logical operation, so the values are written as LogicalOperation nodes under the LogicalOperationStack element.

Message

None

Depends on the presence of a message in the Trace method call.

This element is a formatted message if formatting arguments are provided.

Source

Name

Always present.

Parameter input.

SubType

Name

Always present.

Parameter input.

TimeCreated

SystemTime

Always present.

If not present in the TraceEventCache, the default is the current time.

TimeStamp

None

Depends on the presence of the Timestamp flag in the property.

From the TraceEventCache.

Type

None

Always present.

Always the value 3.

The following table shows the characters that are escaped in the XML output. Escaping occurs in all the elements and attributes with the exception of the DataItem element, which is not escaped if the object passed to the data parameter of the TraceData method is an XPathNavigator object. If an XPathNavigator is used for the data object, the MoveToRoot method is called and the entire root node is traced as unescaped data.

Escaped character

Value

&

&amp;

<

&lt;

>

&gt;

"

&quot;

\

&apos;

0xD

&#xD;

0xA

&#xA;

Examples

The following code example shows the use of the XmlWriterTraceListener class to write both escaped and non-escaped data to file logs.

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Diagnostics;

class testClass
{
    static void Main()
    {
        File.Delete("NotEscaped.xml");
        TraceSource ts = new TraceSource("TestSource");
        ts.Listeners.Add(new XmlWriterTraceListener("NotEscaped.xml"));
        ts.Switch.Level = SourceLevels.All;
        string testString = "<Test><InnerElement Val=\"1\" /><InnerElement Val=\"Data\"/><AnotherElement>11</AnotherElement></Test>";
        XmlTextReader myXml = new XmlTextReader(new StringReader(testString));
        XPathDocument xDoc = new XPathDocument(myXml);
        XPathNavigator myNav = xDoc.CreateNavigator();
        ts.TraceData(TraceEventType.Error, 38, myNav);

        ts.Flush();
        ts.Close();

        File.Delete("Escaped.xml");
        TraceSource ts2 = new TraceSource("TestSource2");
        ts2.Listeners.Add(new XmlWriterTraceListener("Escaped.xml"));
        ts2.Switch.Level = SourceLevels.All;
        ts2.TraceData(TraceEventType.Error, 38, testString);

        ts2.Flush();
        ts2.Close();
    }
}
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports System.Diagnostics



Class testClass

    Shared Sub Main() 
        File.Delete("NotEscaped.xml")
        Dim ts As New TraceSource("TestSource")
        ts.Listeners.Add(New XmlWriterTraceListener("NotEscaped.xml"))
        ts.Switch.Level = SourceLevels.All
        Dim testString As String = "<Test><InnerElement Val=""1"" /><InnerElement Val=""Data""/><AnotherElement>11</AnotherElement></Test>"
        Dim myXml As New XmlTextReader(New StringReader(testString))
        Dim xDoc As New XPathDocument(myXml)
        Dim myNav As XPathNavigator = xDoc.CreateNavigator()
        ts.TraceData(TraceEventType.Error, 38, myNav)

        ts.Flush()
        ts.Close()

        File.Delete("Escaped.xml")
        Dim ts2 As New TraceSource("TestSource2")
        ts2.Listeners.Add(New XmlWriterTraceListener("Escaped.xml"))
        ts2.Switch.Level = SourceLevels.All
        ts2.TraceData(TraceEventType.Error, 38, testString)

        ts2.Flush()
        ts2.Close()

    End Sub 'Main
End Class 'testClass
Security
SecurityPermission

for the ability to call unmanaged code. Associated enumeration: .

Version Information
.NET Framework
Available since 2.0
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top