While working in .net for some time, i today came up with a requirement in which i had to create a error logging system for an n-tier application. I could have easily done some file opening and dumping the logs in there in simple plane text. Instead, I thought having the logs in XML formatĀ  in a DOM structure would be great for some serious read-only filtering as well as processing. Long story short, i created code that would log the errors by creating a XML file that will have the name in the following format:

CurrentYear_CurrentMonthName_MonthDay_ProjectName_log.xml

The code given here will work only if you add System.Xml namespace.
Following is the code that i wrote to create the file name that i will be using as error log file. Everyday a logfile will be generated with the date on which the error occurred.

public static string GetErrorLogFileName(bool includeFolderPath)
        {
            //Sample : errorlogs/2014_mar_26_best_errorlog.xml
            string errorLogFileName = string.Empty;
            string year =   DateTime.Now.Year.ToString();
            string monthName = DateTime.Now.ToString("MMM");
            string monthDay = DateTime.Now.Day.ToString();

            if(includeFolderPath)
            {
                errorLogFileName = "errorlogs/" + year + "_" + monthName + "_" + monthDay + "_best_errorlog.xml";
            }
            else
            {

                errorLogFileName = year + "_" + monthName + "_" + monthDay + "_best_errorlog.xml";
            }
            return errorLogFileName;
        }

Below is the code that will actually call the above function to get the xml filename that you intend to create or open when you want to add an entry into the DB.
The below code will first check if a file with the given name exists or not.
If the file does not exist, it will create a new one. If its the other way round, it would just load the file with the returned file name.

        /// <summary>
        /// function to generate and log errors in xml following file format   
        /// <?xml version="1.0" encoding="utf-8" ?>
        /// <errors>
        ///        <error>
        ///            <pagename>Add New Clinic</pagename>
        ///            <errortime>2014-03-26 10:25:232 AM</errortime>
        ///            <errormessage>New record added with ID</errormessage>
        ///            <innererrormessage>New record added with ID</innererrormessage>
        ///        </error>
        ///</errors>
        ///</summary>
        /// 
        public static void LogError(Exception ex)
        {
            string fileName = Common.GetErrorLogFileName(true);
            //Debug.WriteLine(fileName);
            XmlDocument xmlDoc = new XmlDocument();

            if(!File.Exists(HttpContext.Current.Server.MapPath(fileName)))
            {
                XmlNode rootErrorsNode = xmlDoc.CreateElement("errors");
                xmlDoc.AppendChild(rootErrorsNode);
                XmlNode errorNode = xmlDoc.CreateElement("error");
                rootErrorsNode.AppendChild(errorNode);

                XmlNode pageNameNode = xmlDoc.CreateElement("pagename");
                pageNameNode.InnerText = HttpContext.Current.Request.RawUrl;
                errorNode.AppendChild(pageNameNode);

                XmlNode errorTimeNode = xmlDoc.CreateElement("errortime");
                errorTimeNode.InnerText = DateTime.Now.ToString();
                errorNode.AppendChild(errorTimeNode);

                XmlNode errorMessageNode = xmlDoc.CreateElement("errormessage");
                errorMessageNode.InnerText = ex.Message;
                errorNode.AppendChild(errorMessageNode);

                if (ex.InnerException != null)
                {
                    XmlNode innerErrorMessageNode = xmlDoc.CreateElement("innererrormessage");
                    innerErrorMessageNode.InnerText = ex.InnerException.Message;
                    errorNode.AppendChild(innerErrorMessageNode);
                }

            }
            else
            {
                xmlDoc.Load(HttpContext.Current.Server.MapPath(fileName));
                XmlNode rootErrorsNode = xmlDoc.DocumentElement;
                XmlNode errorNode = xmlDoc.CreateElement("error");
                rootErrorsNode.AppendChild(errorNode);

                XmlNode pageNameNode = xmlDoc.CreateElement("pagename");
                pageNameNode.InnerText = HttpContext.Current.Request.RawUrl;
                errorNode.AppendChild(pageNameNode);

                XmlNode errorTimeNode = xmlDoc.CreateElement("errortime");
                errorTimeNode.InnerText = DateTime.Now.ToString();
                errorNode.AppendChild(errorTimeNode);

                XmlNode errorMessageNode = xmlDoc.CreateElement("errormessage");
                errorMessageNode.InnerText = ex.Message;
                errorNode.AppendChild(errorMessageNode);

                if (ex.InnerException != null)
                {
                    XmlNode innerErrorMessageNode = xmlDoc.CreateElement("innererrormessage");
                    innerErrorMessageNode.InnerText = ex.InnerException.Message;
                    errorNode.AppendChild(innerErrorMessageNode);
                }

            }
            xmlDoc.Save(HttpContext.Current.Server.MapPath(fileName));

        } 
    }

In case you are having trouble understanding the code please do not hesitate to comment.