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 LogsThe 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:
- Click Start and then click Control Panel. Click Performance and Maintenance, then click Administrative Tools and then double-click Computer Management.
- 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:
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.
- catch (Exception e)
- {
- EventLog.WriteEntry("IN Application Exception Create",e.Message+"Trace"+
- e.StackTrace,EventLogEntryType.Error,121,short.MaxValue);
- }
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:
- Error
- Failure Audit
- information
- Success Audit
- Warning
Program in C#
- try
- {
- int i=0;
- int a=10;
- int c=a/i;
-
-
- }
-
- catch(Exception e)
- {
- EventLog.WriteEntry("IN Application Exception Create",e.Message+"Trace"+
- e.StackTrace,EventLogEntryType.Error,121,short.MaxValue);
-
- }
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:
- EventLog[] log = EventLog.GetEventLogs("your pc name");
- foreach (var item in log)
- {
- EventLogEntryCollection logg = item.Entries;
- Console.WriteLine("{0}" + Environment.NewLine + "{1}" + Environment.NewLine + "{2}" + Environment.NewLine + "{3}", item.LogDisplayName, item.MachineName, item.Source, item.Log);
- EventLogTraceListener aa = new EventLogTraceListener(item);
- }
I hope this question is useful for every lovely user.
If you have any query or information about this please comment below.