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();
            }
            
        }
    }
}

1 Comment

  • Yep. That's a very valid comment. Without yet having read your link I'd persume that a solution where separate files are generated for each exception would be better. Then a transformation could render all the documents for browserviewing or aggregated logging.



    But then again. Our apps have no exceptions so it doesn't matter:)

Comments have been disabled for this content.