Popular Posts

Monday, 19 August 2013

CREATE LOG FILE FOR ERROR NOTIFICATIONS

CREATE LOG FILE


Yes the necessary Evil which only help us to study our error in more details and to ealisy understand when and why any error occurred in our hosted/running application.
So lets see how to bulid and track our errorssssss............................................

Its the simple process and can be done with one class and few line of codes(LOC sounds interesting and terrifying NA.....)

THE CODE  BEHOND:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;

/// <summary>
/// Summary description for CreateLogFiles
/// </summary>
public class CreateLogFiles
{
    private string sLogFormat;
    private string sErrorTime;

      public CreateLogFiles()
      {
        sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";

        //this variable used to create log filename format "
        //for example filename : ErrorLogYYYYMMDD
        string sYear = DateTime.Now.Year.ToString();
        string sMonth = DateTime.Now.Month.ToString();
        string sDay = DateTime.Now.Day.ToString();
        sErrorTime = sYear + sMonth + sDay;
    }
    public void ErrorLog(string sPathName, string sErrMsg)
    {
        StreamWriter sw=new StreamWriter(sPathName+sErrorTime,true);
        sw.WriteLine(sLogFormat + sErrMsg);
        sw.Flush();
        sw.Close();
       
   
    }
}

Yes the battle has been won ...............
The ERRORLog function takes the error msg from your try-catch block and saves it in the error file created with name ErrorLog2013813 i.e ErrorLogsErrorTime format.

NOW ALL YOU HAVE TO DO IS THIS.......................................

call this(ErrorLog(string sPathName, string sErrMsg)) function in the try-catch block of your code

1.            CreateLogFiles Err = new CreateLogFiles();
//Creating an instance of createLogFiles class
  2.     Err.ErrorLog(Server.MapPath("Logs/ErrorLog"), ex.Message);
//now call the errorlog function ..specifing the path where you want to save your log file and the error message from the exception class.



FOR EXAMPLE:
Try
{
//your code here...
}
catch (Exception Ex)
        {
            if (ANY CONDITION YOU WANT == true)
            {
                Response.Write("<script>alert('Invalid selected ’)</script>");
               
            }
            CreateLogFiles Err = new CreateLogFiles();
            Err.ErrorLog(Server.MapPath("ErrorLog"), Ex.Message);
        }





HENCE YOUR PROBLEM SOVED ..............................So JUST COPY AND (!!!!UNDERSTAND.......) AND GET AMAZED ON YOUR INTELLIGENCEIF ANY ERROR OR SUGGESTION  IS ,ALWAYS WELCOME....................................................................THANKS FOR BEARING..............................................................................................................................
 

No comments :

Post a Comment