Saturday, July 27, 2013

.NET Basic All Errors into a single Xml document Helper Functions

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Diagnostics;
using System.Web;
using System.Configuration;

namespace IDS.Rapport.Dell.Helpers
{
    public class ErrorHandler
    {
        string _strErrorMessage, _strDetails, _strClassName, _strMethodName;
        DateTime _dtOccuranceTime = new DateTime();

        public ErrorHandler() { }

        /// <summary>
        /// overloaded constructor for quick error logging with limited details
        /// </summary>
        /// <param name="time">Error datetime</param>
        /// <param name="className">Class in which error occurred</param>
        /// <param name="methodName">Method in which error occurred</param>
        /// <param name="errorMessage">Error stack</param>
        /// <param name="details">Error details</param>
        public ErrorHandler(DateTime time, string className, string methodName, string errorMessage, string details)
        {
            _dtOccuranceTime = time;
            _strClassName = className;
            _strDetails = details;
            _strErrorMessage = errorMessage;
            _strMethodName = methodName;
        }

        /// <summary>
        /// Log error in XML file
        /// </summary>
        /// <param name="ex">Exception details</param>
        public static void WriteError(Exception ex)
        {
            WriteError(ex, "");
        }

        /// <summary>
        /// Log error in XML file
        /// Copy the existing node from xml and append the the new node by copying the structure existing node
        /// Its mandatory to have errorlog.xml in application with one record for structure
        /// its picks the errorlog.xml path from web.config ("logfilepath" key)
        /// </summary>
        /// <param name="ex">Exception details<</param>
        /// <param name="fileName">File in which error occurred</param>
        public static void WriteError(Exception ex, string fileName)
        {
            if (Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["Error_Logging"]) == true)
            {
                XmlDocument doc = new XmlDocument();
                string strRootPath = HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["logfilepath"]).Trim());
                string xmlPath = strRootPath;
                doc.Load(@xmlPath);
                XmlNode newXMLNode, oldXMLNode;
                oldXMLNode = doc.ChildNodes[1].ChildNodes[0];
                newXMLNode = oldXMLNode.CloneNode(true);

                StackTrace stackTrace = new StackTrace();
                StackFrame stackFrame = stackTrace.GetFrame(1);
                MethodBase methodBase = stackFrame.GetMethod();

                newXMLNode.ChildNodes[0].InnerText = DateTime.Now.ToString();
                newXMLNode.ChildNodes[1].InnerText = fileName;
                newXMLNode.ChildNodes[2].InnerText = methodBase.DeclaringType.FullName;
                newXMLNode.ChildNodes[3].InnerText = methodBase.Name;
                newXMLNode.ChildNodes[4].InnerText = ex.TargetSite.Name;
                newXMLNode.ChildNodes[5].InnerText = ex.Message;
                newXMLNode.ChildNodes[6].InnerText = ex.StackTrace;
                newXMLNode.ChildNodes[7].InnerText = System.Web.HttpContext.Current.Request.UserHostAddress;
                newXMLNode.ChildNodes[8].InnerText = System.Web.HttpContext.Current.Request.Url.OriginalString;
                doc.ChildNodes[1].AppendChild(newXMLNode);
                doc.Save(@xmlPath);
            }
        }
    }
}


Calling these functions:

ErrorHandler.WriteError(ex, "Logging.cs Perform_Logging");





No comments:

Post a Comment