Wednesday, 8 April 2020

Custom Method for Error Log.

Custom Method for Error Log.

Step 1. Create class with Name 'AppExceptionManager' in class library.


using System;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;

namespace DotNetByPriyanshu
{
    public class AppExceptionManager
    {

         /// <summary>
        /// Log Error Description to log file.
        /// </summary>
        /// <param name="errorDescription"></param>
        public static void LogError(System.Exception errorDescription)
        {
            string dateTime = string.Format("{0:MM-dd-yyyy}", DateTime.Now);
            string path = HttpContext.Current.Server.MapPath("~/ErrorLog/");
            string ExistingFilePath = path + "LogError" + dateTime + ".txt";
            if (File.Exists(ExistingFilePath.Trim()))
            {
                AppendExitingLogErrorFile(errorDescription, dateTime, ExistingFilePath.Trim());
            }
            else
            {
                WriteLogErrorFile(errorDescription, dateTime);
            }

        }

         /// <summary>
        /// It returns Error line number where at which line number exception raised .
        /// </summary>
        /// <param name="ex"></param>
        private static string GetErrorLine(System.Exception ex)
        {
            StringBuilder errorLineNumber = new StringBuilder();
            try
            {
                if (ex != null)
                {
                    if (ex.StackTrace.LastIndexOf(@"\") != -1)
                    {
                        string stackSubString = ex.StackTrace.Substring(ex.StackTrace.LastIndexOf("line ") + 5);

                        if (stackSubString.IndexOf(' ') != -1)
                        {
                            errorLineNumber.Append(stackSubString.Remove(stackSubString.IndexOf(' ')));
                        }
                        else
                        {
                            errorLineNumber.Append(stackSubString);
                        }
                    }
                }
            }
            catch (System.Exception)
            {
            }
            return errorLineNumber.ToString();
        }



         /// <summary>
        /// Method for Append Exiting Log Error File
        /// </summary>
        /// <param name="errorDescription"></param>
        /// <param name="dateTime"></param>
        /// <param name="ExistingFilePath"></param>
        private static void AppendExitingLogErrorFile(System.Exception errorDescription, string  dateTime, string ExistingFilePath)
        {
            myStream = new FileStream(ExistingFilePath, FileMode.Append);
            StreamWriter myWriter = new StreamWriter(myStream);
            if (myWriter != null)
            {
                myWriter.WriteLine("---------------------------------------------------------");
                myWriter.WriteLine("--------------------------Start--------------------------");
                myWriter.WriteLine("Machine Name :" + Environment.MachineName);
                myWriter.WriteLine("Windows user :" + Environment.UserDomainName + "\\" + Environment.UserName);
                myWriter.WriteLine("Time stamp : " + System.DateTime.Now.ToString());
                myWriter.WriteLine("Inner Exception : " + errorDescription.InnerException);
                myWriter.WriteLine("Message : " + errorDescription.Message);
                myWriter.WriteLine("Exception Type : "+ errorDescription.GetType().ToString());
                myWriter.WriteLine("Error Line : " + GetErrorLine(errorDescription));
                myWriter.WriteLine("Stack trace : " + errorDescription.StackTrace);
                myWriter.WriteLine("OS Name : "+ Environment.OSVersion.ToString());
                myWriter.WriteLine("HelpLink : " + errorDescription.HelpLink);
                myWriter.WriteLine("-------------------------- End --------------------------");
                myWriter.Close();
                myStream.Close();
            }
        }

    }
}

  /// <summary>
        /// Method for Write Log Error File
        /// </summary>
        /// <param name="errorDescription"></param>
        /// <param name="dateTime"></param>
        private static void WriteLogErrorFile(System.Exception errorDescription, string dateTime)
        {
            string strFilePath = HttpContext.Current.Server.MapPath("~/ErrorLog/");
            using (StreamWriter myWriter = new StreamWriter(Path.Combine(strFilePath, "LogError" + dateTime + ".txt")))
            {
                if (myWriter != null)
                {

                    myWriter.WriteLine("---------------------------------------------------------");
                    myWriter.WriteLine("--------------------------Start--------------------------");
                    myWriter.WriteLine("Machine Name :" + Environment.MachineName);
                    myWriter.WriteLine("Windows user :" + Environment.UserDomainName + "\\" + Environment.UserName);
                    myWriter.WriteLine("Time stamp : "  + System.DateTime.Now.ToString());
                    myWriter.WriteLine("Inner Exception : " + errorDescription.InnerException);
                    myWriter.WriteLine("Message : "  + errorDescription.Message);
                    myWriter.WriteLine("Exception Type : " + errorDescription.GetType().ToString());
                    myWriter.WriteLine("Error Line : "  + GetErrorLine(errorDescription));
                    myWriter.WriteLine("Stack trace : " + errorDescription.StackTrace);
                    myWriter.WriteLine("OS Name : " + Environment.OSVersion.ToString());
                    myWriter.WriteLine("HelpLink : " + errorDescription.HelpLink);
                    myWriter.WriteLine("-------------------------- End --------------------------");
                    myWriter.Close();
                }
            }
        }

Step 2. Create ErrorLog folder in Application.


No comments:

Post a Comment