Write an Exception in Event Log

Hi friends. In this article we learn how to write an Application Exception to the Event Log. During an application's life many exceptions are created, like stack overflow, Object Reference not set and so many exceptions. 
 
 
Event Logs

The event log contains System Errors, Application Errors and System Internal Error.  The Event log stores every type of information like the name of the error, the time created, Error Numbr, Type of Error and so on. If you want more information about the Event Log click here.

How to open the Event Log: 
  1. Click Start and then click Control Panel. Click Performance and Maintenance, then click Administrative Tools and then double-click Computer Management.

  2. In the console tree, click Event Viewer.
Event Log in C#

First of all we need the reference the Event log class or namespace. The namespace is System.Diagnostics.
 
Problem

I make an application. The application handles the user account related query and solves it. The user has no knowledge of the validation nor the proper format of the data. I don't know who the user may be, whetehr children, young guys or anyone. If the user enters the data in the wrong format or is the wrong input our the program suddenly stops or throws an exception then the user can't understand the problem. This type of problem we can handled with a try catch or something having tools. Okay, stay with me. Look at this example.
 
Program written in C#
 
int a=0;
int b=10; 
int  c=0 
 c=b/a;
 
 The preceding simple program throws the error. "Attempted to divided by Zero".  Note if we are not use the try and catch block, it's confirmed stop our program. 
 
Try and Catch Flow: 
 
    more Info click here 
 
How to Write an Exception to the Event Log
 
Note that Exception is the base class of exceptions. All Exception classes are inherited by this  class. It can be called a father class of all exception classes.
  1. catch (Exception e)  
  2. {  
  3.    EventLog.WriteEntry("IN Application Exception Create",e.Message+"Trace"+   
  4.    e.StackTrace,EventLogEntryType.Error,121,short.MaxValue);  
  5. }  
EventLog is a class. if you want to know what type of class then just a simple work. Select the EventLog and press the function key "F12". You will then see all the details of this class.
 
 
The following shows how to write to the Event Log: 

Eventlog.WriteEntery(“your source message”, “your exception message”,EventLogEntry.Type.(have five type of Entry type), specified EventID, Short category);

Event Log Entry type:
  1. Error
  2. Failure Audit
  3. information
  4. Success Audit
  5. Warning
 
 
Program in C# 
  1. try  
  2. {  
  3.    int i=0;  
  4.    int a=10;  
  5.    int c=a/i;  
  6.   
  7.   
  8. }  
  9.   
  10. catch(Exception e)  
  11. {  
  12.    EventLog.WriteEntry("IN Application Exception Create",e.Message+"Trace"+   
  13.    e.StackTrace,EventLogEntryType.Error,121,short.MaxValue);  
  14.   
  15. }  
An exception is createed in the Event Log like this.
 
 
 
You can check the above after the event log in the Event viewer. 
 
The following shows how to get all the exceptions:
  1.                 EventLog[] log = EventLog.GetEventLogs("your pc name");  
  2.                 foreach (var item in log)  
  3.                 {  
  4. EventLogEntryCollection logg = item.Entries;  
  5. Console.WriteLine("{0}" + Environment.NewLine + "{1}" + Environment.NewLine + "{2}" + Environment.NewLine + "{3}", item.LogDisplayName, item.MachineName, item.Source, item.Log);  
  6.                     EventLogTraceListener aa = new EventLogTraceListener(item);  
  7.                 }  
 I hope this question is useful for every lovely user. 
 
If you have any query or information about this please comment below.

Up Next
    Ebook Download
    View all
    Learn
    View all