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