I was wanting the output of the
XmlWriterTraceListener to be formatted, so I figured I could just adjust some settings similar to the
XmlWriter class and get formatting, indentation, etc. However, upon viewing the
XmlWriterTraceListener code in Reflector, I found that it writes out its XML using a plain
TextWriter, which means no intelligent XML formatting. I imagine this is for performance reasons or some other good reason, but if you really want or need formatted log output, you’re out of luck.
I decided the only way to keep the built-in behavior of the
XmlWriterTraceListener but still allow formatted output was to re-implement the CLR class and override the output to use an
XmlWriter. Using Reflector, I painstakingly implemented the exact same behavior and logic using an
XmlWriter for output. My new class,
XmlWriterTraceListener for consistency and integration with the .Net tracing system. The real strange part was the constructors because I couldn’t pass down the file name or else it would try to start up the base class
TextWriter logic. Instead, this implementation keeps track of its
XmlWriter and path information in the instance. Properties have been added to support the various
XmlWriter indentation and formatting options.
The source for the
FormattedXmlWriterTraceListener is available on gist.github.com.