Wednesday 22 August 2012

.Net Tips : Write an Event Entry in Windows Event Log with C# Examples and VB.Net Examples

You can write or insert entry into the Windows event log using the static methods of the EventLog class, or  also you can create an EventLog object.
But you must decide in which event source you want to write your log entry.The event source is a string that uniquely identifies your application.

By default, there is three event log sources Application, System, and Security. Normally you will write into the Application event source, but you also specify you custom log. You do not need to explicitly create a custom log but when you register an event source against a log, if the specified log doesn’t exist, it’s created automatically.

You can also set type of log entry i.e Error, FailureAudit, Information,SuccessAudit, and Warning. Which you need to set in EventLog class's method.

Here is example for this
In this example we write one event entry in "Application" log section in windows event log.
We are using "EventLog.WriteEntry" Method.

C# Examples :
        // Write an event into the event log.
        EventLog.WriteEntry("Application",
                            "A simple demo event.", // Event entry message
                            EventLogEntryType.Information, // Type of Event
                            1,
                            0,
                            System.Text.Encoding.Default.GetBytes("Hi , This is simple event log entry") // Event data
                            );

VB.net Examples :
        '' Write an event into the event log.
        EventLog.WriteEntry("Application",
                            "A simple demo event.",
                            EventLogEntryType.Information,
                            1,
                            0,
                            System.Text.Encoding.Default.GetBytes("Hi , This is simple event log entry")
                            )

Output :

(To view original image , click on image)


You can also create an event source using this method "EventLog.CreateEventSource".

NOTE : You must have administrator privileges to create an event source.

Here is an example of Read Event Logs Entry and Display. Click Here...
 
This is very useful .Net Tips.

For Beginning .Net articles. Click Here...

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


2 comments:

  1. Dot Net framework provides benefits for different kinds of issues include security, exceptional handling, memory management etc., as you said Dot Net helps developer in various ways to deliver an application effectively
    .Net training
    best .net training institute in chennai

    ReplyDelete