Error logging in asp.net application using text file.Following class can help us in doing the same.

public class clsErrLogging
    {
        #region Function
        /// <summary>
        /// This function is used to open the file and write the error messages in that same file.
        /// </summary>
        /// <param name="msg"></param>
        public void LoggingErrors(string strMsg)
        {
            try
            {            
                string strPath = System.Configuration.ConfigurationManager.AppSettings["LogFilePath"];
                StreamWriter StrWriter = new StreamWriter(strPath, true);
                StrWriter.AutoFlush = true;
                StrWriter.WriteLine("———————" + DateTime.Now + "————————");
                StrWriter.WriteLine(DateTime.Now + " :: " + strMsg);
                StrWriter.WriteLine("———————" + DateTime.Now + "————————");
                StrWriter.Close();
              
              
            }
            catch (Exception ex)
            {
                string strErr = ex.Message;
            }          
        }
        #endregion Function
    }

Advertisements