Exception Error Handling Log Maintenance in a Text File: Part 2

In my previous article we got to know how to create two methods that handle the log maintenance for exceptions. After that just build the application and we will get the "dll" of the project. The link of my previous article is
 
http://www.c-sharpcorner.com/UploadFile/b19d5a/8225/

Now in this article I will describe how to use the "ErrorHandling" dll when an exception happens and to register the Exception in a text file. The textfile will be created inside the "LogError" folder in the C drive. So before attaching the dll you have to create the folder in your C drive.

Now here is the Program.

Step 1

First create a webapplication named "Errorhandlingappln".

Step 2

After that in the reference folder add a reference for "Errorhandling.dll". In this project I will give you the dll.
 
ErrHand1.gif

Step 3

After attaching the dll it will appear in your reference folder and it will appear like the following figure:

ErrHand2.gif

Step 4

Now in the default.aspx.cs first register the dll.

ErrHand3.gif

Step 5

Now see the following code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using ErrorHandiling;  
  8. namespace ErrorhandlingAppln  
  9. {  
  10.     public partial class _Default : System.Web.UI.Page  
  11.     {        
  12.         int i = 10,d=0;  
  13.         ErrorHandling objError = new ErrorHandling();   
  14.         protected void Page_Load(object sender, EventArgs e)  
  15.         {  
  16.             try  
  17.             {  
  18.                 int result = (i /d) ;  
  19.             }  
  20.             catch (Exception ee)  
  21.             {  
  22.                 string errorMessage =  ErrorHandiling .Class1.CreateErrorMessage(ee);  
  23.                 ErrorHandiling .Class1.LogFileWrite(errorMessage);  
  24.   
  25.             }  
  26.   
  27.         }  
  28.     }  
Here I simply divide the integer i to 0. That means the ArithmaticException will occur.

So in the Catch block 
  1. First create the Exception error message with inner Exception by pasing the Exception object. ErrorHandling is our dll class name.
  2. Secondly we are passing the "errorMessage" to the method name " LogFileWrite" to create the txtfile.
Step 6

Now run the program. Obliviously the Exception will occur. Now go to the "LogError" folder and you will see the logtext file name "ProgramLog-20110711" just like the following figure.

ErrHand4.gif

Step 7

Now open the text file; you will see exception details information that have been generated by our program exception like the following figure.
 
ErrHand5.gif
Conclusion

So in this article we have seen how to use the "ErrorHandling" dll when an exception happens and how to register the exception in a text file.

Next Recommended Readings