Thursday, September 17, 2009

EventLog Cycle (Provides interaction with Windows event logs.)



Introduction
Following are the important classes in Diagnostics namespace.
System.Diagnostics.EventLog
This component provides functionality to write to event logs, read event log entries, and create and delete event logs and event sources on the network.

Some related classes:
EventLog: Provides interaction with Windows event logs
EventLogEntry: Encapsulates a single record in the event log. This class cannot be inherited.
EventLogEntryCollection: Defines size and enumerators for a collection of EventLogEntry instances
EventLogInstaller: Allows you to install and configure an event log that your application reads from or writes to when running. This class is called by the installation utility, for example, InstallUtil.exe, when installing an event log.
EventLogPermission: Allows control of code access permissions for event logging.
• EventLogPermissionAttribute: Allows declarative permission checks for event logging
EventLogPermissionEntry: Defines the smallest unit of a code access security permission that is set for an EventLog
• EventLogPermissionEntryCollection: Contains a strongly typed collection of EventLogPermissionEntry objects
EventLogTraceListener: Provides a simple listener that directs tracing or debugging output to an EventLog

Examples: follow steps those are required to complete the eventlog process

1)Initialize Log component
private System.Diagnostics.EventLog eventLog1;
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();

this.ServiceName = "Your Service Name";
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

2)Create a source name and log name, which showing in system event log
if (!System.Diagnostics.EventLog.SourceExists("Log Source Name"))
{
System.Diagnostics.EventLog.CreateEventSource("Log Source Name", "Log Root Name");
}
eventLog1.Source = "Log Source Name";
eventLog1.Log = "Log Root Name";

3)How to write an error in defined source log
catch (Exception ex)
{
eventLog1.WriteEntry("Error :" +
ex.Message + "--" +
ex.StackTrace, System.Diagnostics.EventLogEntryType.Error);
Alert(ex);
}

No comments:

Loading...