Write the Windows event log using the EventLog class

Source: Internet
Author: User

It is often necessary to write the specified information (including exception information and normal processing information) to the log in the program. In c#3.0, you can use the EventLog class to write various information directly to the Windows log. The EventLog class is in the System.Diagnostics namespace. We can view the Windows logs we write in Administrative Tools > Event Viewer, as shown in:

The following is an example of writing a log using the EventLog class to an application (application), with the log type specified using the EventLogEntryType enumeration type.

EventLog Log= NewEventLog ();Try{log. Source= "My application"; Log. WriteEntry ("Processing Information 1", eventlogentrytype.information); Log. WriteEntry (" Processing Information 2", eventlogentrytype.information); throw new System.IO.FileNotFoundException ("Readme.txt File not found ");} Catch (System.IO.FileNotFoundException exception) {log. WriteEntry (" processing Information 2", EventLogEntryType.Error);}

After running the above code, the log information as shown will be written.

The first parameter of the WriteEntry method specifies the information that can be viewed by double-clicking the corresponding log, as shown in.

By default, the EventLog class writes logs in the application event, and other logs, such as system events, can be specified through the parameters of the EventLog constructor method, but be aware that when writing log information to non-application events, The event source needs to be added to the current event using the CreateEventSource method first. The following example shows how to add log information to the system event:

EventLog Log= NewEventLog ("System");//First, you should determine whether the log source exists, a log source can only be bound to an event at the same time s If(!EventLog.SourceExists ("My Application")) EventLog.CreateEventSource ("My Application","System");Try{log. Source= "My Application"; Log. WriteEntry ( " processing information 1" , eventlogentrytype.information);     log. WriteEntry ( " processing information 2" Span style= "color: #000000;" >, eventlogentrytype.information);     throw new System.IO.FileNotFoundException ( "readme.txt file not found " catch (System.IO.FileNotFoundException exception) {     log. WriteEntry (Exception. Message, EventLogEntryType.Error); }

After executing the above code, write the log to the system event as shown in.

We can also add our own events through the EventLog class, the code is as follows:

EventLog Log= NewEventLog ("MyEvent");//First, you should determine whether the log source exists, a log source can only be bound to an event at the same time s If(!EventLog.SourceExists ("NEW Application")) EventLog.CreateEventSource ("NEW Application","MyEvent");Try{log. Source= "New applications"; Log. WriteEntry ( " processing information 1" , eventlogentrytype.information);     log. WriteEntry ( " processing information 2" Span style= "color: #000000;" >, eventlogentrytype.information);     throw new System.IO.FileNotFoundException ( "readme.txt file not found " catch (System.IO.FileNotFoundException exception) {     log. WriteEntry (Exception. Message, EventLogEntryType.Error); }

The above code adds a MyEvent event that is added after the Event Viewer interface as shown in.

As you can see, there is more than one MyEvent event in the Event Viewer on the left. We can also use the EventLog class to enumerate the logs in the specified event, as shown in the following code:

If(EventLog.Exists ("MyEvent") {EventLog Log= new EventLog ( "myevent" Span style= "color: #000000;" >);     foreach (EventLogEntry entry in log. Entries)     {        TextBox1.Text += entry. Message + : ";    }

The above code enumerates all the logs in the MyEvent event that was just established and outputs the information for each log (that is, the information specified by the first parameter of the WriteEntry method).     In addition, we can use the Delete method to delete the specified event and use the DeleteEventSource method to delete the log source. When writing to the Windows Event log, be aware that if the event log file is full, you can increase the size of the log file in the Event Properties dialog box (choose the Properties menu item in the right-click menu of the event), or empty the log in the current event. The Event Properties dialog box appears as shown in.

Write the Windows event log using the EventLog class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.