Reader Level:
ARTICLE

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

Posted by Shirsendu Nandi Articles | Exception Handling C# July 11, 2011
In this article I will describe how to use the "ErrorHandling" dll when an exception happens and how to register the Exception in a text file.
  • 0
  • 0
  • 7216
Download Files:
 


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:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ErrorHandiling;

namespace ErrorhandlingAppln
{
    public partial class _Default : System.Web.UI.Page
    {     
        int i = 10,d=0;
        ErrorHandling objError = new ErrorHandling();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                int result = (i /d) ;
            }
            catch (Exception ee)
            {
                string errorMessage =  ErrorHandiling .Class1.CreateErrorMessage(ee);
                ErrorHandiling .Class1.LogFileWrite(errorMessage);

            }

        }
    }
}


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.

COMMENT USING

Trending up