Use EventLog to write Windows event logs in C #

Source: Internet
Author: User

In a program, you often need to write the specified information (including exception information and normal processing information) to the log. From C #3.0, you can use the EventLog class (in the system. Diagnostics namespace) to write various information into windows logs. You can view the written windows logs in the management tools> Event Viewer.

The following is an example of using the EventLog class to write logs to an application. The log type is specified by the eventlogentrytype Enumeration type:

EventLog log = new EventLog ();
Try
{
Log. Source = "My applications ";
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, an error event will be written into the log information. WriteentryMethodThe information specified by the first parameter is displayed in the log description.

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 log = new EventLog ("system ");
// First, check whether the log source exists. One log source can only be bound to one event.
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", eventlogentrytype. information );
Throw new system. Io. filenotfoundexception ("readme.txt file not found ");
}
Catch (system. Io. filenotfoundexception exception)
{
Log. writeentry (exception. Message, eventlogentrytype. Error );
}

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 log = new EventLog ("myevent ");
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", 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, and adds a myevent event on the left side of the added Event Viewer. This event is at the same layer as "application" and "system.
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 log = 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 ). You can also 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.
 

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.