Tuesday, 17 May 2016

Error Log in c#

/// <summary>
/// Summary description for ErrorLog.
/// </summary>

    public  class Errorlog
    {
        SiteData sd = null;
        public static string folderpath;
        public  Errorlog()
        {
            try
            {
                sd = System.Windows.Application.Current.Resources["sd"];
                if (sd != null)
                {
                    folderpath = sd.accountfoldernamepath;
                }
            }
            catch(Exception ex)
            {
                WriteToErrorLogRoot(ex.Message, ex.StackTrace, "Error");
            }
        }





        /// <summary>
        ///  Open or create an error log and submit error message
        /// </summary>
        /// <param name="msg">message to be written to error file</param>
        /// <param name="stkTrace">stack trace from error message</param>
        /// <param name="title">title of the error file entry</param>
        public static void WriteToErrorLog(string msg, string stkTrace, string title)
        {

            if (!(System.IO.Directory.Exists(folderpath + "\\Errors\\")))
            {
                System.IO.Directory.CreateDirectory(folderpath + "\\Errors\\");
            }
            FileStream errorLogFileStream = new FileStream(folderpath + "\\Errors\\ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter s = new StreamWriter(errorLogFileStream);
            s.Close();
            errorLogFileStream.Close();
            FileStream errorLogFileStream1 = new FileStream(folderpath + "\\Errors\\ErrorLog.txt", FileMode.Append, FileAccess.Write);
            StreamWriter errorLogStreamWriter = new StreamWriter(errorLogFileStream1);
            errorLogStreamWriter.Write("Title: " + title + Environment.NewLine);
            errorLogStreamWriter.Write("Message: " + msg + Environment.NewLine);
            errorLogStreamWriter.Write("StackTrace: " + stkTrace + Environment.NewLine);
            errorLogStreamWriter.Write("Date/Time: " + DateTime.Now.ToString() + Environment.NewLine);
            errorLogStreamWriter.Write("===========================================================================================" + Environment.NewLine);
            errorLogStreamWriter.Close();
            errorLogFileStream1.Close();
        }



        /// <summary>
        ///  Open or create an error log and submit error message
        /// </summary>
        /// <param name="msg">message to be written to error file</param>
        /// <param name="stkTrace">stack trace from error message</param>
        /// <param name="title">title of the error file entry</param>
        /// <param name="methodname">Stores the method name</param>
        public static void WriteToErrorLog(string msg, string stkTrace, string title, string methodname)
        {
            if (!(System.IO.Directory.Exists(folderpath + "\\Errors\\")))
            {
                System.IO.Directory.CreateDirectory(folderpath + "\\Errors\\");
            }
            FileStream errorLogFileStream = new FileStream(folderpath + "\\Errors\\ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter s = new StreamWriter(errorLogFileStream);
            s.Close();
            errorLogFileStream.Close();
            FileStream errorLogFileStream1 = new FileStream(folderpath + "\\Errors\\ErrorLog.txt", FileMode.Append, FileAccess.Write);
            StreamWriter errorLogStreamWriter = new StreamWriter(errorLogFileStream1);
            errorLogStreamWriter.Write("Title: " + title + Environment.NewLine);
            errorLogStreamWriter.Write("Message: " + msg + Environment.NewLine);
            errorLogStreamWriter.Write("Method: " + methodname + Environment.NewLine);
            errorLogStreamWriter.Write("StackTrace: " + stkTrace + Environment.NewLine);
            errorLogStreamWriter.Write("Date/Time: " + DateTime.Now.ToString() + Environment.NewLine);
            errorLogStreamWriter.Write("===========================================================================================" + Environment.NewLine);
            errorLogStreamWriter.Close();
            errorLogFileStream1.Close();

        }


        /// <summary>
        ///  Open or create an error log and submit error message
        /// </summary>
        /// <param name="msg">message to be written to error file</param>
        /// <param name="stkTrace">stack trace from error message</param>
        /// <param name="title">title of the error file entry</param>
        /// <param name="methodname">Stores the method name</param>
        public static void WriteToErrorLog(string msg, string stkTrace, string title, string methodname, string devMessage)
        {
            if (!(System.IO.Directory.Exists(folderpath + "\\Errors\\")))
            {
                System.IO.Directory.CreateDirectory(folderpath + "\\Errors\\");
            }
            FileStream errorLogFileStream = new FileStream(folderpath + "\\Errors\\ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter s = new StreamWriter(errorLogFileStream);
            s.Close();
            errorLogFileStream.Close();
            FileStream errorLogFileStream1 = new FileStream(folderpath + "\\Errors\\ErrorLog.txt", FileMode.Append, FileAccess.Write);
            StreamWriter errorLogStreamWriter = new StreamWriter(errorLogFileStream1);
            errorLogStreamWriter.Write("Title: " + title + Environment.NewLine);
            errorLogStreamWriter.Write("Message: " + msg + Environment.NewLine);
            errorLogStreamWriter.Write("Method: " + methodname + Environment.NewLine);
            errorLogStreamWriter.Write("Developer Message: " + devMessage + Environment.NewLine);
            errorLogStreamWriter.Write("StackTrace: " + stkTrace + Environment.NewLine);
            errorLogStreamWriter.Write("Date/Time: " + DateTime.Now.ToString() + Environment.NewLine);
            errorLogStreamWriter.Write("===========================================================================================" + Environment.NewLine);
            errorLogStreamWriter.Close();
            errorLogFileStream1.Close();
        }
       


        /// <summary>
        ///  Open or create an error log and submit error message
        /// </summary>
        /// <param name="msg">message to be written to error file</param>
        /// <param name="stkTrace">stack trace from error message</param>
        /// <param name="title">title of the error file entry</param>
        public static void WriteToErrorLogRoot(string msg, string stkTrace, string title)
        {
            if (!(System.IO.Directory.Exists(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\")))
            {
                System.IO.Directory.CreateDirectory(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\");
            }
            FileStream errorLogFileStream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter s = new StreamWriter(errorLogFileStream);
            s.Close();
            errorLogFileStream.Close();
            FileStream errorLogFileStream1 = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\ErrorLog.txt", FileMode.Append, FileAccess.Write);
            StreamWriter errorLogStreamWriter = new StreamWriter(errorLogFileStream1);
            errorLogStreamWriter.Write("Title: " + title + Environment.NewLine);
            errorLogStreamWriter.Write("Message: " + msg + Environment.NewLine);
            errorLogStreamWriter.Write("StackTrace: " + stkTrace + Environment.NewLine);
            errorLogStreamWriter.Write("Date/Time: " + DateTime.Now.ToString() + Environment.NewLine);
            errorLogStreamWriter.Write("===========================================================================================" + Environment.NewLine);
            errorLogStreamWriter.Close();
            errorLogFileStream1.Close();           
        }
       

   
        /// <summary>
        ///  Open or create an error log and submit error message
        /// </summary>
        /// <param name="msg">message to be written to error file</param>
        /// <param name="stkTrace">stack trace from error message</param>
        /// <param name="title">title of the error file entry</param>
        /// <param name="methodname">Stores the method name</param>
        public static void WriteToErrorLogRoot(string msg,  string stkTrace, string title,string methodname)
        {
            if (!(System.IO.Directory.Exists(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\")))
            {
                System.IO.Directory.CreateDirectory(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\");
            }
            FileStream errorLogFileStream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter s = new StreamWriter(errorLogFileStream);
            s.Close();
            errorLogFileStream.Close();
            FileStream errorLogFileStream1 = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\ErrorLog.txt", FileMode.Append, FileAccess.Write);
            StreamWriter errorLogStreamWriter = new StreamWriter(errorLogFileStream1);
            errorLogStreamWriter.Write("Title: " + title + Environment.NewLine);
            errorLogStreamWriter.Write("Message: " + msg + Environment.NewLine);
            errorLogStreamWriter.Write("Method: " + methodname + Environment.NewLine);
            errorLogStreamWriter.Write("StackTrace: " + stkTrace + Environment.NewLine);
            errorLogStreamWriter.Write("Date/Time: " + DateTime.Now.ToString() + Environment.NewLine);
            errorLogStreamWriter.Write("===========================================================================================" + Environment.NewLine);
            errorLogStreamWriter.Close();
            errorLogFileStream1.Close();
          
        }


        /// <summary>
        ///  Open or create an error log and submit error message
        /// </summary>
        /// <param name="msg">message to be written to error file</param>
        /// <param name="stkTrace">stack trace from error message</param>
        /// <param name="title">title of the error file entry</param>
        /// <param name="methodname">Stores the method name</param>
        public static void WriteToErrorLogRoot(string msg, string stkTrace, string title, string methodname,string devMessage)
        {
            if (!(System.IO.Directory.Exists(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\")))
            {
                System.IO.Directory.CreateDirectory(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\");
            }
            FileStream errorLogFileStream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter s = new StreamWriter(errorLogFileStream);
            s.Close();
            errorLogFileStream.Close();
            FileStream errorLogFileStream1 = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "\\Errors\\ErrorLog.txt", FileMode.Append, FileAccess.Write);
            StreamWriter errorLogStreamWriter = new StreamWriter(errorLogFileStream1);
            errorLogStreamWriter.Write("Title: " + title + Environment.NewLine);
            errorLogStreamWriter.Write("Message: " + msg + Environment.NewLine);
            errorLogStreamWriter.Write("Method: " + methodname + Environment.NewLine);
            errorLogStreamWriter.Write("Developer Message: " + devMessage + Environment.NewLine);
            errorLogStreamWriter.Write("StackTrace: " + stkTrace + Environment.NewLine);
            errorLogStreamWriter.Write("Date/Time: " + DateTime.Now.ToString() + Environment.NewLine);
            errorLogStreamWriter.Write("===========================================================================================" + Environment.NewLine);
            errorLogStreamWriter.Close();
            errorLogFileStream1.Close();       
        }
       
   


    /// <summary>
    /// Logs an event to a text file.
    /// </summary>
    /// <param name="category">The event category</param>
    /// <param name="description">The event description</param>
    public static void LogEvent(string category, string description)
    {
        TextWriter file = new StreamWriter(@"C:\DDSLog.txt", true);
        file.WriteLine(category + ": " + description);
        file.Close();
    }


    /// <summary>
    /// Logs an event to a text file.
    /// </summary>
    /// <param name="category">The event category</param>
    /// <param name="description">The event description</param>
    /// <param name="ServiceName">The event Service Name</param>
    public static void LogEvent(string category, string description, string ServiceName)
    {
        string Fname = "";
        if (ServiceName == "DDS")
            Fname = @"c:\DDS_LogFile.txt";
        else
            Fname = @"c:\DDS.txt";

        TextWriter file = new StreamWriter(Fname, true);
        if (category.Trim().Equals(""))
            file.WriteLine(description);
        else
            file.WriteLine(category + ": " + description);
        file.Close();
    }

  

    }

No comments:

Post a Comment