Tracking can monitor application execution and obtain required information. Through tracking, you can collect some useful information about the program during execution. In. net, you can easily implement a simple tracing mechanism through the trace class. By default, defaulttracelistener is used to listen and store trace messages. In addition, the. NET Framework also provides some specialized listeners, which are derived from the tracelistener class like the default listeners.
- Textwritertracelistener outputs the collected messages to the stream, which can be easily saved as a file.
- Eventlogtracelistener output to Event Log
- Consoletracelistener can send tracking information to the standard output stream.
- Xmlwritertracelistener, of course, outputs information to the stream in XML format, which is also convenient for saving as an XML fragment file.
The listener can be added or removed by calling the trace. listeners. add and remove methods in the code. You can also set the message tracing, listener, and tracing Switch Level by setting the <system. Diagnostics> element in the application configuration file. The <trace> sub-element can be used to set message tracing.
<Configuration>
<System. Diagnostics>
<Trace>
<Listeners>
<Add name = "mylistener" type = "system. diagnostics. xmlwritertracelistener, system, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 "initializedata =" C: \ mylistener. XML "/>
<Remove name = "default"/>
</Listeners>
</Trace>
</System. Diagnostics>
</Configuration>
In the preceding example, a listener named mylistener named xmlwritertracelistener is added and the default listener is removed.
If you have other special requirements for tracing listening, you can also implement your own listeners and add them in a similar way. The following example shows a custom listener class derived from textwritertracelistener. Its functions are also very simple. By Rewriting the writeline method, the listener can simply format the output format and add time. The write and writeif methods are commonly used.
Public class mytracelistener: system. Diagnostics. textwritertracelistener
{
Public mytracelistener (string filename)
: Base (filename)
{
}
Public override void writeline (string Str)
{
Base. writeline (string. Format ("My trace name = '{0}' time = '{1}'", STR, datetime. Now. tostring ()));
}
}
P.s. For xmlwritertracelistener listeners, it will output messages to XML fragments in a specific format. To generate a standard XML file, further operations are required. The most direct method is to store the tracing results in a temporary file first, and add the generated XML fragments to the formal trace file after the tracing is completed. However, in some cases, the trace file will become very large as time increases and operations increase. Due to the XML structure requirements, we cannot perform simple append operations at the end as in general documents. Frequent reading and navigation may consume more resources. In this case, we can use reference external entities to merge XML documents. Xinclude? It seems that too few support it...
P.s. Disable DTD when using. Net to process XML files using xmldocument. If you want to process the XML document mentioned above, set the prohibitdtd attribute of xmlreadersettings to false and import it when loading the document.