XmlTextWriter and Non-Wellformed Output to LogFile ?
I have an ASP.NET 2.0 Webservice that does some report processing and am trapping errors and writing to a log file using a .txt file to write Xml using XmlTextWriter and then using An Xml File with the Txt file included to view as outlined in this MSDN articlehttp://msdn2.microsoft.com/en-us/library/Aa302289.aspx ::Efficient Techniques for Modifying Large XML Files
The issue I am having is in when writing the StackTrace output to using the code below what is happening is that the StackTrace CDATA section is getting truncated as shown in the Xml output below thus not being wellformed to view thru my xml include file.
You'll note that not only have used CDATA where I thought it was needed - I also tried to HTML Encode the StackTrace and I've also ran Replace("\r\n","<br/>") on the StackTrace to see if that was causing the problem since I noted that return-newline was showing in the debug window when the error is thrown.
What I am wondering is that since this is running in a Web service am I stepping all over myself by not serializing the calls to write to the log file? If so how would I need to change the code and/or is there some characters possibly within the CDATA section that I need to capture and replace that I'm not doing?
Thanks for your input!
Code Example for Writing to Log (.txt) file
public
staticvoid WriteLogEntry(Exception inerr){try{
StreamWriter sw =File.AppendText(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings[Constants.ApplicationKeys.NotifyLogFile]));XmlTextWriter xtw =newXmlTextWriter(sw);xtw.WriteStartElement(
"logentry");xtw.WriteAttributeString(
"type","exception");// -xtw.WriteStartElement(
"datetime");xtw.WriteCData(System.
DateTime.Now.ToString());xtw.WriteEndElement();
// -xtw.WriteElementString(
"source", inerr.Source);// -xtw.WriteStartElement(
"message");xtw.WriteCData(inerr.Message);
xtw.WriteEndElement();
// -xtw.WriteStartElement(
"stacktrace");xtw.WriteElementString(
"length", inerr.StackTrace.Length.ToString());xtw.WriteCData(
HttpContext.Current.Server.HtmlEncode(inerr.StackTrace.ToString().Replace("\r\n","<br/>").ToString()));xtw.WriteEndElement();
// -xtw.WriteStartElement(
"targetsite");xtw.WriteCData(inerr.TargetSite.ToString());
xtw.WriteEndElement();
// -xtw.WriteStartElement(
"innerexception");if (inerr.InnerException !=null) { xtw.WriteCData(inerr.InnerException.ToString()); }xtw.WriteEndElement();
// -xtw.Close();
}
catch (Exception eBidErr) {//do something here!}
Output Won't load thru my Xml File include because it's not well formed - truncated!
<logentry type="exception"><datetime><![CDATA[8/17/2007 5:12:27 PM]]></datetime><source>Microsoft.ReportViewer.WebForms</source><message><![CDATA[An error occurred during local report processing.]]></message><stacktrace><length>908</length><![CDATA[ at Microsoft.Reporting.WebForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, CreateAndRegisterStream createStreamCallback, Warning[]& warnings)<br/> at Microsoft.Reporting.WebForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)<br/> at Microsoft.Reporting.WebForms.LocalReport.Render(String format, String deviceInfo, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)<br/> at eXchangeNotify.Service1.FaxSolicitationDefaultReport(Int32 templateID, Int32 s

