Bewerken

Delen via


XmlWriterTraceListener Class

Definition

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

public ref class XmlWriterTraceListener : System::Diagnostics::TextWriterTraceListener
public class XmlWriterTraceListener : System.Diagnostics.TextWriterTraceListener
type XmlWriterTraceListener = class
    inherit TextWriterTraceListener
Public Class XmlWriterTraceListener
Inherits TextWriterTraceListener
Inheritance

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.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
End Class

Remarks

Note

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 create an XmlWriterTraceListener in your code. Alternatively, for .NET Framework apps, you can enable or disable an XmlWriterTraceListener through the application configuration file and then use the configured XmlWriterTraceListener in your application. 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.

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 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.
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
& &
< <
> >
" "
|'
0xD &#xD;
0xA &#xA;

Constructors

XmlWriterTraceListener(Stream)

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

XmlWriterTraceListener(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.

XmlWriterTraceListener(String)

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

XmlWriterTraceListener(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.

XmlWriterTraceListener(TextWriter)

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

XmlWriterTraceListener(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.

Properties

Attributes

Gets the custom trace listener attributes defined in the application configuration file.

(Inherited from TraceListener)
Filter

Gets or sets the trace filter for the trace listener.

(Inherited from TraceListener)
IndentLevel

Gets or sets the indent level.

(Inherited from TraceListener)
IndentSize

Gets or sets the number of spaces in an indent.

(Inherited from TraceListener)
IsThreadSafe

Gets a value indicating whether the trace listener is thread safe.

(Inherited from TraceListener)
Name

Gets or sets a name for this TraceListener.

(Inherited from TraceListener)
NeedIndent

Gets or sets a value indicating whether to indent the output.

(Inherited from TraceListener)
TraceOutputOptions

Gets or sets the trace output options.

(Inherited from TraceListener)
Writer

Gets or sets the text writer that receives the tracing or debugging output.

(Inherited from TextWriterTraceListener)

Methods

Close()

Closes the Writer for this listener so that it no longer receives tracing or debugging output.

CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose()

Releases all resources used by the TraceListener.

(Inherited from TraceListener)
Dispose(Boolean)

Disposes this TextWriterTraceListener object.

(Inherited from TextWriterTraceListener)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Fail(String)

Emits an error message to the listener you create when you implement the TraceListener class.

(Inherited from TraceListener)
Fail(String, String)

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

Flush()

Flushes the output buffer for the Writer.

(Inherited from TextWriterTraceListener)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetSupportedAttributes()

Gets the custom attributes supported by the trace listener.

(Inherited from TraceListener)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()
Obsolete.

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
ToString()

Returns a string that represents the current object.

(Inherited from Object)
TraceData(TraceEventCache, String, TraceEventType, Int32, Object)

Writes trace information, a data object, and event information to the file or stream.

TraceData(TraceEventCache, String, TraceEventType, Int32, Object[])

Writes trace information, data objects, and event information to the file or stream.

TraceEvent(TraceEventCache, String, TraceEventType, Int32)

Writes trace and event information to the listener specific output.

(Inherited from TraceListener)
TraceEvent(TraceEventCache, String, TraceEventType, Int32, String)

Writes trace information, a message, and event information to the file or stream.

TraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[])

Writes trace information, a formatted message, and event information to the file or stream.

TraceTransfer(TraceEventCache, String, Int32, String, Guid)

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

Write(Object)

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

(Inherited from TraceListener)
Write(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)
Write(String)

Writes a verbatim message without any additional context information to the file or stream.

Write(String, String)

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

(Inherited from TraceListener)
WriteIndent()

Writes the indent to the listener you create when you implement this class, and resets the NeedIndent property to false.

(Inherited from TraceListener)
WriteLine(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)
WriteLine(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)
WriteLine(String)

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

WriteLine(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)

Applies to