ASP.NET - how to create Exception Error Log or Error File For Website

Asked By vanchi nathan on 13-May-11 10:35 AM
hi friends,

how to create Exception Error Log or Error File For Website

Thanks
tvn
dipa ahuja replied to vanchi nathan on 13-May-11 10:41 AM
you can create one error.aspx file which shows related errors..

and put your code in between try catch block and in the catch part you can redirected to that error.aspx page
Jitendra Faye replied to vanchi nathan on 13-May-11 10:42 AM
Follow this-

Step 1: Start by creating an Error folder where all errors will be logged. Right click the website > New Folder. Rename the folder to “Error”. Also add a web.config file, if one does not already exist in your site. Right click the website > Add New Item > Web.config.
Step 2: Now we will create the error handler code. To do so, right click your website > Add New Item > select Class. Rename the class to ‘ErrHandler.cs’ and click on ‘Add’. When you do so, you will be prompted with a message to place the class in ‘App_Code’ folder. Accept the message to place the class in the 'App_Code' folder.
Step 3: Now let us add functionality to the ErrHandler class. This class will accept the error message and write the message in a text file. One text file will be created for each day. If the text file already exists, the message will be appended to the text file. If not, a new text file will be created based on today’s date and error message will be written in it.
The code will look similar to the following:
C#
/// Handles error by accepting the error message
    /// Displays the page on which the error occured
    public static void WriteError(string errorMessage)
    {
      try
      {
        string path = "~/Error/" + DateTime.Today.ToString("dd-mm-yy") + ".txt";
        if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
        {
          File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
        }
        using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
        {
          w.WriteLine("\r\nLog Entry : ");
          w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
          string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
                ". Error Message:" + errorMessage;
          w.WriteLine(err);
          w.WriteLine("__________________________");
          w.Flush();
          w.Close();
        }
      }
      catch (Exception ex)
      {
        WriteError(ex.Message);
      }
 
    }


for more detail follow this-

http://www.dotnetcurry.com/ShowArticle.aspx?ID=94
vanchi nathan replied to vanchi nathan on 14-May-11 02:41 AM
thanks friend.

it works fine and very easily
vanchi nathan replied to Jitendra Faye on 14-May-11 02:42 AM
thanks vicky it works fine and very easy. keep contributing more.
shilpa replied to vanchi nathan on 21-Jun-11 06:07 AM
thanks for sharing the link...
its very useful!!!