0% found this document useful (0 votes)
48 views

Step 1: Start by Creating An Error Folder Where All Errors

The document describes steps to create an error handling mechanism that logs errors to text files. It involves: 1) Creating an "Error" folder to store log files 2) Creating an "ErrHandler" class to write error messages to text files with file names based on the date 3) Adding code to a button's click handler that throws an exception, catches it, and calls the error handling class to log the message.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Step 1: Start by Creating An Error Folder Where All Errors

The document describes steps to create an error handling mechanism that logs errors to text files. It involves: 1) Creating an "Error" folder to store log files 2) Creating an "ErrHandler" class to write error messages to text files with file names based on the date 3) Adding code to a button's click handler that throws an exception, catches it, and calls the error handling class to log the message.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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 todays 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.Ser ver.MapPath(path))) { File.Create(System.Web.HttpContext.Current.Serve r.MapPath(path)).Close(); } using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.S erver.MapPath(path))) { w.WriteLine("\r\nLog Entry : "); w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCultu re)); string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToStr ing() + ". Error Message:" + errorMessage; w.WriteLine(err); w.WriteLine("__________________________"); w.Flush(); w.Close(); } } catch (Exception ex) { WriteError(ex.Message); } }

Handling errors at Page Level In the Default.aspx, drag and drop a button from the toolbox. Rename this button to btnError and set the Text as Throw Handled Exception. Here we will throw an exception. Since we have a catch block defined, the exception will be caught and the error will be logged in the Error folder. Since a text file with todays date, does not exists, a new text file will be created by the code. The button click handler will look similar to the following: C# protected void btnHandled_Click(object sender, EventArgs e) { try { throw new Exception("Sample Exception"); } catch (Exception ex) { // Log the error to a text file in the Error folder ErrHandler.WriteError(ex.Message); } }

You might also like