Friday, August 20, 2010

XML based log file.

XML file defines the structure:


<messagelog>
  <msgn>
    <msg> message </msg>
    <mdtls> message details </mdtls>
    <mtype> message type </mtype>
    <sname> Service Name</sname>
    <dt> datetime </dt>
    </msgn>
  </messagelog>

 

Code file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace FXA_LogFile

        ///
        /// This class is used to log the message across the application in XML file.
        ///

        public class LogHandler
        {
            ///
            /// This is used to identify the message type in the xml file.
            ///

            enum MessageType
            {
                Error=1,
                Warning=2,
                Inforamation=3
            }
   
            ///
            /// This method is used to record the message of error type
            ///

            ///
Short error message
            /// Error message description
            /// Name of service.
            public static void LogErrorMessage(string errorMessage, string errorDetails, string serviceName)
            {
                WriteMessageToXMLFile(errorMessage, errorDetails, MessageType.Error, serviceName);     
            }

            ///
            /// This method is used to record the message of warning type
            ///

            /// Short warning message.
            /// Warning message description.
            /// Name of service.
            public static void LogWarningMessage(string warningMessage, string warningDetails, string serviceName)
            {
                WriteMessageToXMLFile(warningMessage, warningDetails, MessageType.Warning, serviceName);
            }

            ///
            /// This method is used to record the message of information type
            ///

            /// Short information message.
            /// Information message description.
            /// Name of service.
            public static void LogInformationMessage(string informationMessage, string informationDetails, string serviceName)
            {
                WriteMessageToXMLFile(informationMessage, informationDetails, MessageType.Inforamation, serviceName);
            }

            ///
            /// This method append the message in the xml file.
            ///

            /// Short message.
            /// Message description.
            /// Type of message.
            /// Name of service.
            private static void WriteMessageToXMLFile(string message, string messageDetails, MessageType varMessageType, string serviceName)
            {
                XmlDocument doc = new XmlDocument();
                string xmlPath = @"FXA_Logfile.xml";
                doc.Load(@xmlPath);
                XmlNode newXMLNode, oldXMLNode;
                oldXMLNode = doc.ChildNodes[1].ChildNodes[0];
                newXMLNode = oldXMLNode.CloneNode(true);
                newXMLNode.ChildNodes[0].InnerText = DateTime.Now.ToString();
                newXMLNode.ChildNodes[1].InnerText = message;
                newXMLNode.ChildNodes[2].InnerText = messageDetails;
                newXMLNode.ChildNodes[3].InnerText = varMessageType.ToString();
                newXMLNode.ChildNodes[4].InnerText = serviceName;            
                doc.ChildNodes[1].AppendChild(newXMLNode);
                doc.Save(@xmlPath);
                doc.RemoveAll();
            }
        }
}

No comments: