Error handling Logging

12/03/2012 10:09

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using System.Xml.Linq;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
 
namespace Cflow
{
 
    public class Logging
    {
 
 
        //Write Logs in XML File
        public static void WriteErrorLog(string errorMsg, string source, string stacktrace)
        {
            try
            {
 
 
                string appDir = System.Web.HttpContext.Current.Server.MapPath("./Log/") + "Error_" + DateTime.Now.ToString("MMMM-dd-yyyy") + ".txt";
 
                //Application.StartupPath.ToLower().Replace("\\bin\\debug", "") + "\\Log\\watchdawg.xml";
 
                string _FilePath = System.Web.HttpContext.Current.Server.MapPath("./Log/") + "Error_" + DateTime.Now.ToString("MMMM-dd-yyyy") + ".txt";
 
                if (!File.Exists(_FilePath))
                {
                    // Create a file to write to.
                    using (StreamWriter sw = File.CreateText(_FilePath))
                    {
                        sw.WriteLine("------------------------------------------------------------------------------------------");
                        sw.WriteLine("CFlow v 1.0: New Log Created");
                        sw.WriteLine("------------------------------------------------------------------------------------------");
                        sw.Close();
                    }
                }
 
                using (System.IO.StreamWriter Writer = new System.IO.StreamWriter(_FilePath, true))
                {
                    //Writer.WriteLine("------------------------------------------------------------------------------------------");
                    string lNewText = DateTime.Now.ToString("MMMM-dd-yyyy hh:mm:ss") + " : ";
                    Writer.WriteLine(lNewText);
                    Writer.WriteLine("Error Message: " + errorMsg);
                    Writer.WriteLine("Source: " + source);
                    Writer.WriteLine("ST: " + stacktrace);
                    Writer.WriteLine("******************************************************************************************");
                    Writer.Close();
                }
 
                //XDocument xmlDoc = XDocument.Load(appDir);
 
                //xmlDoc.Element("ErrorFile").Add
                //    (new XElement("Error",
                //        new XElement("Date", DateTime.Now.ToString("MMM-dd-yyyy")),
                //        new XElement("Time", DateTime.Now.ToString("hh:mm:ss tt")),
                //        new XElement("ErrorMessage", errorMsg),
                //        new XElement("Source", source),
                //        new XElement("StackTrace", stacktrace)));
 
                //xmlDoc.Save(appDir);
            }
 
            catch (Exception ex)
            {
                // throw new Exception(ex.Message);
            }
 
        }
 
        // Write Logs in XML File
        public static void WriteLog(string message, string user)
        {
            try
            {
 
 
                string appDir = System.Web.HttpContext.Current.Server.MapPath("./Log/") + "Error_" + DateTime.Now.ToString("MMMM-dd-yyyy") + ".txt";
 
 
 
                string _FilePath = System.Web.HttpContext.Current.Server.MapPath("./Log/") + "Error_" + DateTime.Now.ToString("MMMM-dd-yyyy") + ".txt";
 
                if (!File.Exists(_FilePath))
                {
                    // Create a file to write to.
                    using (StreamWriter sw = File.CreateText(_FilePath))
                    {
                        sw.WriteLine("New Log Created");
                        sw.Close();
                    }
                }
 
                using (System.IO.StreamWriter Writer = new System.IO.StreamWriter(_FilePath, true))
                {
                    string lNewText = DateTime.Now.ToString("MMMM-dd-yyyy hh:mm:ss") + " " + user + " : " + message;
                    //  Debug.WriteLine(lNewText);
                    Writer.WriteLine(lNewText);
                    Writer.Close();
 
                }
 
                //XDocument xmlDoc = XDocument.Load(appDir);
 
                //xmlDoc.Element("LogFile").Add
                //    (new XElement("Log",
                //        new XElement("Date", DateTime.Now.ToString("MMMM-dd-yyyy hh:mm:ss tt")),
                //        new XElement("User", user),
                //        new XElement("Message", message)));
 
                //xmlDoc.Save(appDir);
            }
            catch (Exception ex)
            {
                // throw new Exception(ex.Message);
            }
        }
 
 
    }
}