Thursday, June 21, 2012

Error logging using Event log in C#


Error Logging-

 
Error logging is a process of logging or tracking error which comes in run-time. When you we run our application.  It is good practice to log application's error so that user can easily understand the error cause. This is also useful for developer to track the error and resolve those errors.

 .Net provides inbuilt class (EventLog) to log Application events; by including System.Diagnostics Namespace we can use this class to log events or errors. Using EventLog class we can create Error source and Error log.


Here Error source represent to the application which will generate the error. And Error log will hold all error related to that Error source. By this way errors can be easily identified that what is the source of error because it is maintain in one block only. 

In this example, I am showing - How to log error using Event log in C#?

Here is code-

Include this line on top of the form-


using System.Diagnostics;

Now I have defined this function-

//Function to log error

public void LogError(string strError)
        {
            // Creating the source, which will generate error
            if (!EventLog.SourceExists("MyApplication"))
            {
                //Creating log, where error will be logged
                EventLog.CreateEventSource("MyApplication", "MyLog");
            }

            // Creating object of EventLog to log error
            EventLog myAppLog = new EventLog();
            myAppLog.Source = "MyApplication";
         
            //Writing error to log
            myAppLog.WriteEntry(strError);
        }



In above code, I have defined one function (LogError) to log error. Here first I am Checking for existence of source which can be name of application, If it is not present then we need to create it, because under this only logger will be created. Using  CreateEventSource() method, I am creating log (MyLog) under source (MyApplication).

Suppose if source is already exists then directly I am writing entry under created log using WriteEntry(). For writing entry under log first we need to create object of EventLog class.
In this code, I am assigning source to using Source property of EventLog object.

Now to log error I have used this function like this-

private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
            //Explicitly generating error
                 int a = 10;
                int b = 0;
                a = a / b;
            }
            catch (Exception EX)
            {
              //Calling function to log error
                LogError(EX.Message);
            }
        }


In this code, I am explicitly generating error- DivideByZero . In catch block I have used LogError () to log generated error using EventLog.

Here I am getting generated error using EX.Message property of Exception object and passing to LogError () as string. After executing this lines of code error will be logged in Event Log and output will be-



Error logging
 
 

In output, we can easily see that Here Source is "MyApplication" and Log Name is "MyLog", both are created by code. Here all the errors which will be generated by this application will be logged in same block under "MyApplication" source and "MyLog" log. One application can have more that one Log also, based on category application log can be divided to different log.



Thanks

2 comments: