ExceptionXmlPublisher for Microsoft.ApplicationBlocks.ExceptionManagement
using System;
using System.Xml;
using System.Collections.Specialized;
using System.IO;
namespace Microsoft.ApplicationBlocks.ExceptionManagement
{
/// <summary>
/// Summary description for ExceptionXmlPublisher.
/// </summary>
public class ExceptionXmlPublisher : IExceptionXmlPublisher
{
void IExceptionXmlPublisher.Publish(XmlDocument ExceptionInfo, NameValueCollection ConfigSettings)
{
string filename;
if (ConfigSettings != null)
{
filename = ConfigSettings["fileName"];
}
else
{
filename = @"C:\ErrorLog.xml";
}
XmlDocument xmlTarget = new XmlDocument();
FileStream fs;
if(File.Exists(filename))
{
fs = File.Open(filename, FileMode.Open, FileAccess.Read);
try
{
xmlTarget.Load(fs);
}
catch
{
xmlTarget = new XmlDocument();
xmlTarget.AppendChild(xmlTarget.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
xmlTarget.AppendChild(xmlTarget.CreateElement("RuntimeExceptions"));
}
finally
{
fs.Close();
}
}
else
{
xmlTarget.AppendChild(xmlTarget.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
xmlTarget.AppendChild(xmlTarget.CreateElement("RuntimeExceptions"));
}
XmlDocumentFragment docFrag = xmlTarget.CreateDocumentFragment();
docFrag.InnerXml = ExceptionInfo.OuterXml;
XmlNodeList stackTraceNodes = docFrag.SelectNodes("//StackTrace");
foreach(XmlNode node in stackTraceNodes)
{
node.InnerXml = "<![CDATA[" + node.InnerXml + "]]>";
}
xmlTarget.DocumentElement.AppendChild(docFrag);
fs = File.Open(filename, FileMode.Create, FileAccess.Write);
try
{
xmlTarget.Save(fs);
}
catch
{}
finally
{
fs.Close();
}
}
}
}