System.Diagnostics.XmlWriterTraceListener Class

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

See Also: XmlWriterTraceListener Members

Syntax

public class XmlWriterTraceListener : TextWriterTraceListener

Remarks

Note:

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

The System.Diagnostics.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 tp://go.microsoft.com/fwlink/p/?LinkId=240129 to display the XML output.

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

To configure an System.Diagnostics.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.

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 System.Diagnostics.XmlWriterTraceListener class inherits the TraceListener.Filter property from the base class System.Diagnostics.TraceListener. The TraceListener.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 TraceFilter.ShouldTrace(TraceEventCache, string, TraceEventType, int, string, Object[], object, Object[]) method of the filter to determine whether to emit the trace.

Note:

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.

Note:

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

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

CallStack

None

Depends on the presence of the TraceOptions.Callstack flag in the TraceListener.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 Environment.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 XmlWriterTraceListener.TraceTransfer(TraceEventCache, string, int, string, Guid) method.

DataItem

None

Depends on the data parameter of the erload:System.Diagnostics.XmlWriterTraceListener.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 System.Diagnostics.TraceEventCache.

ProcessID

Always present.

From the System.Diagnostics.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 System.Diagnostics.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 TraceOptions.LogicalOperationStack flag in the TraceListener.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 System.Diagnostics.TraceEventCache, the default is the current time.

TimeStamp

None

Depends on the presence of the TraceOptions.Timestamp flag in the TraceListener.TraceOutputOptions property.

From the System.Diagnostics.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 erload:System.Diagnostics.XmlWriterTraceListener.TraceData method is an System.Xml.XPath.XPathNavigator object. If an System.Xml.XPath.XPathNavigator is used for the data object, the System.Xml.XPath.XPathNavigator.MoveToRoot method is called and the entire root node is traced as unescaped data.

&

&amp;

<

&lt;

>

&gt;

"

&quot;

\

&apos;

0xD

&#xD;

0xA

&#xA;

Requirements

Namespace: System.Diagnostics
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0