Export (0) Print
Expand All

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)

[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true)]
public class XmlWriterTraceListener : TextWriterTraceListener

The XmlWriterTraceListener type exposes the following members.

  NameDescription
Public methodXmlWriterTraceListener(Stream)Initializes a new instance of the XmlWriterTraceListener class, using the specified stream as the recipient of the debugging and tracing output.
Public methodXmlWriterTraceListener(String)Initializes a new instance of the XmlWriterTraceListener class, using the specified file as the recipient of the debugging and tracing output.
Public methodXmlWriterTraceListener(TextWriter)Initializes a new instance of the XmlWriterTraceListener class using the specified writer as the recipient of the debugging and tracing output.
Public methodXmlWriterTraceListener(Stream, String)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.
Public methodXmlWriterTraceListener(String, String)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.
Public methodXmlWriterTraceListener(TextWriter, String)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.
Top

  NameDescription
Public propertyAttributesGets the custom trace listener attributes defined in the application configuration file. (Inherited from TraceListener.)
Public propertyFilterGets and sets the trace filter for the trace listener. (Inherited from TraceListener.)
Public propertyIndentLevelGets or sets the indent level. (Inherited from TraceListener.)
Public propertyIndentSizeGets or sets the number of spaces in an indent. (Inherited from TraceListener.)
Public propertyIsThreadSafeGets a value indicating whether the trace listener is thread safe. (Inherited from TraceListener.)
Public propertyNameGets or sets a name for this TraceListener. (Inherited from TraceListener.)
Protected propertyNeedIndentGets or sets a value indicating whether to indent the output. (Inherited from TraceListener.)
Public propertyTraceOutputOptionsGets or sets the trace output options. (Inherited from TraceListener.)
Public propertyWriterGets or sets the text writer that receives the tracing or debugging output. (Inherited from TextWriterTraceListener.)
Top

  NameDescription
Public methodCloseCloses the Writer for this listener so that it no longer receives tracing or debugging output. (Overrides TextWriterTraceListener.Close().)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodDispose()Releases all resources used by the TraceListener. (Inherited from TraceListener.)
Protected methodDispose(Boolean)Infrastructure. Disposes this TextWriterTraceListener object. (Inherited from TextWriterTraceListener.)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodFail(String)Emits an error message to the listener you create when you implement the TraceListener class. (Inherited from TraceListener.)
Public methodFail(String, String)Writes trace information including an error message and a detailed error message to the file or stream. (Overrides TraceListener.Fail(String, String).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodFlushFlushes the output buffer for the Writer. (Inherited from TextWriterTraceListener.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodGetSupportedAttributesGets the custom attributes supported by the trace listener. (Inherited from TraceListener.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodMemberwiseClone()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodTraceData(TraceEventCache, String, TraceEventType, Int32, Object)Writes trace information, a data object, and event information to the file or stream. (Overrides TraceListener.TraceData(TraceEventCache, String, TraceEventType, Int32, Object).)
Public methodTraceData(TraceEventCache, String, TraceEventType, Int32, Object[])Writes trace information, data objects, and event information to the file or stream. (Overrides TraceListener.TraceData(TraceEventCache, String, TraceEventType, Int32, Object[]).)
Public methodTraceEvent(TraceEventCache, String, TraceEventType, Int32)Writes trace and event information to the listener specific output. (Inherited from TraceListener.)
Public methodTraceEvent(TraceEventCache, String, TraceEventType, Int32, String)Writes trace information, a message, and event information to the file or stream. (Overrides TraceListener.TraceEvent(TraceEventCache, String, TraceEventType, Int32, String).)
Public methodTraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[])Writes trace information, a formatted message, and event information to the file or stream. (Overrides TraceListener.TraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[]).)
Public methodTraceTransferWrites trace information including the identity of a related activity, a message, and event information to the file or stream. (Overrides TraceListener.TraceTransfer(TraceEventCache, String, Int32, String, Guid).)
Public methodWrite(Object)Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class. (Inherited from TraceListener.)
Public methodWrite(String)Writes a verbatim message without any additional context information to the file or stream. (Overrides TextWriterTraceListener.Write(String).)
Public methodWrite(Object, String)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.)
Public methodWrite(String, String)Writes a category name and a message to the listener you create when you implement the TraceListener class. (Inherited from TraceListener.)
Protected methodWriteIndentWrites the indent to the listener you create when you implement this class, and resets the NeedIndent property to false. (Inherited from TraceListener.)
Public methodWriteLine(Object)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.)
Public methodWriteLine(String)Writes a verbatim message without any additional context information followed by the current line terminator to the file or stream. (Overrides TextWriterTraceListener.WriteLine(String).)
Public methodWriteLine(Object, String)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.)
Public methodWriteLine(String, String)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.)
Top

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

NoteNote

The HostProtectionAttribute attribute applied to this type or member 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 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();
    }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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