Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XmlWriterTraceListener Class

Note: This class is new in the .NET Framework version 2.0.

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)

public class XmlWriterTraceListener : TextWriterTraceListener
public class XmlWriterTraceListener extends TextWriterTraceListener
public class XmlWriterTraceListener extends TextWriterTraceListener

NoteNote

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

NoteNote

The HostProtectionAttribute attribute applied to this class has the following Resources property value: Synchronization. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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 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 Filter property from the base class TraceListener. The Filter 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.

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.

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 TraceOutputOptions 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 MachineName 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 TraceOutputOptions 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 TraceOutputOptions 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;

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();
    }
}

System.Object
   System.MarshalByRefObject
     System.Diagnostics.TraceListener
       System.Diagnostics.TextWriterTraceListener
        System.Diagnostics.XmlWriterTraceListener

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

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft