Write exception information to custom Windows Event Logs

Source: Internet
Author: User
In. NET, writing information to Windows event logs may only take one sentence: Call the WriteEntry method of EventLog. In more cases, we need special categories to record some exception information ,. NET also provides us with a very convenient method: Call the CreateEventSource method of EventLog to create your own event source, and then write the exception information into it. But unfortunately, if you only want to do this, you will not be able to succeed when running the program. The reason is simple: the ASPNET user account does not have sufficient permissions to create event sources.

On Google, you can easily find out how to write information into Event Logs and create your own event sources. There are also related articles on major technical websites and blogs, however, few people mentioned permission issues. I doubt whether these authors have run the code after writing their articles. Since they are technical, when writing Technical Articles, the necessary rigor is not enough. It is not the case that Aunt Qiong Yao writes sad or funny dramas. You can simply write them.

You can create a custom event source in either of the following ways:

1. This method is not recommended to modify the Windows registry. It is risky. If the system crashes, this method will not be provided here.

2. Create your own event source by creating the Installer classand using the installutil.exe tool.

The specific steps are as follows:

Add a New Item (installer class) to your Project)

Then add the following code to the file:

System. Diagnostics. EventLogInstaller eventLogInstaller = new System. Diagnostics. EventLogInstaller ();


EventLogInstaller. Source = "Source ";

EventLogInstaller. Log = "Log ";


This. Installers. Add (eventLogInstaller );

Compile, and then enter installutil.exe in command line mode to the directory where the project. DLL file is located. Then the Log category named Log appears in Windows event view.

Next, we can enter the following code anywhere to record the information to the event Log named Log.
System. Diagnostics. EventLog. WriteEntry ("Source", "Test", System. Diagnostics. EventLogEntryType. Error );

Complete code download

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.