c#3.0 on the use of the EventLog class to write Windows event logs

Source: Internet
Author: User

Operating system: Windows XP SP3

Development tools: Visual Studio 2008

Language: C # 3.0

. NET framework:3.5

It is often necessary to write the specified information, including exception information and normal processing information, to the log in the program. You can use the EventLog class to write all kinds of information directly to the Windows log in c#3.0. 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 illustration:

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

EventLog log = new EventLog();
try
{
     log.Source = "我的应用程序";
     log.WriteEntry("处理信息1", EventLogEntryType.Information);
     log.WriteEntry("处理信息2", EventLogEntryType.Information);
     throw new System.IO.FileNotFoundException("readme.txt文件未找到");
}
catch (System.IO.FileNotFoundException exception)
{
     log.WriteEntry("处理信息2", EventLogEntryType.Error);
}

After you run the above code, you will write the log information as shown in the following illustration.

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.