C #3.0 Miscellaneous (1): Use EventLog to write windows Event Logs

Source: Internet
Author: User
This article is original. If you need to reprint it, please indicate the author and the source. Thank you!

Operating System: Windows XP SP3

Development tools: Visual Studio 2008

Language: C #3.0.

. NET Framework: 3.5

InProgramYou often need to write the specified information (including exception information and normal processing information) to the log. In C #3.0, you can use the EventLog class to directly write various information into windows logs. The EventLog class is in the system. Diagnostics namespace. You can view the Windows logs that we write in "Administrative Tools"> "Event Viewer", as shown in:

The following is an example of using the EventLog class to write logs to an application.Specify the eventlogentrytype Enumeration type.

EventLog =   New EventLog ();
Try
{
Log. Source =   " My applications " ;
Log. writeentry ( " Process Information 1 " , Eventlogentrytype. information );
Log. writeentry ( " Process Information 2 " , Eventlogentrytype. information );
Throw   New System. Io. filenotfoundexception ( " Readme.txt file not found " );
}
Catch (System. Io. filenotfoundexception exception)
{
Log. writeentry ( " Process Information 2 " , Eventlogentrytype. Error );
}

InCodeAs shown in.

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

By default, the EventLog class writes logs in the Application Event. Other logs, such as system events, can be specified through the parameters of the EventLog constructor, however, when writing log information to a non-application event, you must first use the createeventsource method to add the event source to the current event. The following example shows how to add log information to a system event: EventLog =   New EventLog ( " System " );
// First, determine whether the log source exists. One log source can only be bound to one event at a time.
If ( ! EventLog. sourceexists ( " My application " ))
EventLog. createeventsource ( " My application " , " System " );
Try
{
Log. Source =   " My application " ;
Log. writeentry ( " Process Information 1 " , Eventlogentrytype. information );
Log. writeentry ( " Process Information 2 " , Eventlogentrytype. information );
Throw   New System. Io. filenotfoundexception ( " Readme.txt file not found " );
}
Catch (System. Io. filenotfoundexception exception)
{
Log. writeentry (exception. Message, eventlogentrytype. Error );
}

Shows the logs written to the system event after the above code is executed.

 

 

You can also add your own events through the EventLog class. The Code is as follows:

EventLog =   New EventLog ( " Myevent " );
// First, determine whether the log source exists. One log source can only be bound to one event at a time.
If ( ! EventLog. sourceexists ( " New Application " ))
EventLog. createeventsource ( " New Application " , " Myevent " );
Try
{
Log. Source =   " New applications " ;
Log. writeentry ( " Process Information 1 " , Eventlogentrytype. information );
Log. writeentry ( " Process Information 2 " , 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. The added Event Viewer interface is shown in.

As you can see, the event viewer on the left contains multiple myevent events.
You can also use the EventLog class to enumerate logs in a specified event, as shown in the following code:

 

If (Eventlog. exists ( " Myevent " ))
{
EventLog =   New EventLog ( " Myevent " );
Foreach (Eventlogentry entry In Log. Entries)
{
Textbox1.text + = Entry. Message +   " : " ;
}
}

The above code enumerated all the logs in the myevent event just created, and output the information of each log (that is, the information specified by the first parameter of the writeentry method ). In addition, you can use the delete method to delete the specified event and the deleteeventsource method to delete the log source.
When writing Windows event logs, note that if the event log file is full, you can click the event Properties dialog box (right-click the event and choose "attribute" to bring up this dialog box) or clear the logs in the current event. Shows the event Properties dialog box.

 

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.