1. Purpose
Application system development and maintenance can not be separated from the log system, select a powerful log system solution is an important part of the application system development process. There are many kinds of log system solutions in the. NET environment, and Log4net is the leader.
In Windows2000 and above, there is a Windows logging system that includes the application (application) event log, System log, and security log, and the event log can also be a custom log. The appropriate classes and interfaces are also available in the. NET Framework to use the application event log or to customize the event log. The use of Windows logs allows the application system to be better integrated with the operating system, and it is easier to query and manage logs than simply using a custom log system because of the operating system support. In practical applications, depending on the actual situation, you can choose a suitable log solution, you can also customize the log system and the Windows log system of two log solutions to use simultaneously.
2. How to use Windows log
2.1. Overview of methods
A class EventLog is provided in the. NET Framework, using the EventLog class to add new event log entries or to get existing entries from the server event log. The EventLog class includes a WriteEntry () method that can be used to write a new event to the event log. When a new entry is written to the event log, the entry is written to a specific event log using a specific event source ("Event source").
Event sources are unique to the event log. In Windows2000 and above, includes an event log: Application (application) event log, System log and security log, and the system allows custom event logs. Using the EventLog class, you can add log entries to the application (application) event log, or you can add them to your custom event log. The event source is equivalent to the next level of the event log, and each log entry must correspond to an event source. The EventLog class can create a custom event log, or you can create an event source that can be created in the application (application) event log or in a custom event log. The Windows logging system looks like this:
In order to facilitate the separation and viewing of logs between different application systems, it is common to create event sources in a custom event log, you can create multiple event logs, and an event log can create multiple event sources.
2.2. Event log and Event source creation method
When you create a new event log or event source, you are actually adding an entry to the registry. Because writing the registry requires special permissions, there are permissions and security issues in creating event logs and event sources in a Web project, and this issue is not present in the application project, which focuses on how Windows logs are used in Web projects. The use of application projects is similar to Web projects, discarding permissions and handling security in Web projects, and this article does not repeat.
In a Web project, when you use ASP. NET to create an event log or an event source in the system, you may get the following exception error message: System.Security.SecurityException: The requested registry access is not allowed. This is because the default account for running the ASP. IIS6.0 is ASPNET (under NetworkService), and this account defaults to read and no write permissions, so you cannot create an event log or event source. The solution to this problem is to elevate the ASPNET account's permissions, not create an event log or event source inside the program, etc., mainly with the following three solutions:
A. Before the program runs, define the event logs and event sources to use, open Registry Editor, and manually add event logs and event sources to the registry. The main steps are as follows:
① click on "Start" menu and click "Run".
② enter regedit in the Open box in run, and then press the OK button to open the Registry Editor.
③ found the following subkey in Registry Editor:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
④ Right-click on "Eventlog", click "New", then "item", will create a new project at the next level of "Eventlog", Name the project "Tddn", the TDDN item is the event log, can be named according to the actual situation, such as can be named Project name.
⑤ Right-click on the "TDDN" Item, click "New", then "item", will create a new item at the next level of "Tddn", name it "Weblog", the Weblog item is the event source, this item can also be named according to the actual situation.
⑥ Close Registry Editor.
This allows the event log and event source to be built, repeating the process if more than one event log or event source is required. This method requires to be familiar with the registry, the operation may be a bit complex, you can write a class to implement the configuration registry, as long as the class can be run to add the corresponding project, exempt from the tedious manual add, this is the second solution, the method is as follows:
B, there is a EventLogInstaller class in the System.Diagnostics namespace that enables you to create and configure event logs and event sources that your application reads and writes to at run time. You can use the EventLogInstaller class to create an event log and event source by using the following steps:
① uses C # to create a class library called EventLogSourceInstaller.
② adds a reference to System.Configuration.Install.dll in this project.
③ the auto-generated Class1.cs to MyEventLogInstaller.cs.
④ writes the following code in the MyEventLogInstaller.cs:
Using System;
Using System.Diagnostics;
Using System.ComponentModel;
Using System.Configuration.Install;
Namespace EventLogSourceInstaller {
[Runinstaller (True)]
public class myeventloginstaller:installer{
Public EventLogInstaller myEventLogInstaller;
Public myEventLogInstaller () {
Create Instance of EventLogInstaller
myEventLogInstaller = new EventLogInstaller ();
Set the Source of Event Log, to is created.
Myeventloginstaller.source = "WebLog";
Set the Log that's source is created in
MyEventLogInstaller.Log = "TDDN";
Installers.add (myEventLogInstaller);
}
}
}
⑤ generates this project and gets EventLogSourceInstaller.dll.
⑥ open the Visual Studio. NET command Prompt and go to the directory where EventLogSourceInstaller.dll is located.
⑦ Run this command to create the event log and event source, run as: Enter the command InstallUtil EventLogSourceInstaller.dll.
The program creates an event log in the system: TDDN, which creates an event source under the event log TDDN: WebLog.
Both of these solutions are to manually add event logs and event sources to the system before the application runs, so there are no security permissions issues and there are no security implications. A more convenient approach can also allow the program to automatically create event logs and event sources when the application is running, either by raising the system operation permissions of the ASPNET account or by giving the ASP. NET process an impersonation account with greater permissions. The implementation of the demo account is complex, and there is no difference between the functionality and security and the promotion of the ASPNET account, and the method used here is to elevate the permissions of the ASPNET account. The following is the third solution:
C, the right to elevate the ASPNET account can be directly in Windows System Management to the ASPNET account to add read and write access to the system, but there is a serious security problem, the ASP. NET process has the right to read and write the operating system, will bring a great security risk to the system. The way to elevate the ASPNET account permissions here is to assign only the operating permissions of the system log to the ASPNET account, so while there are some security implications, the pitfalls are greatly reduced and the event logs and event sources can be created freely within the program, greatly improving flexibility. The ASPNET account privilege elevation method is as follows:
① Click "Start", "Run", enter "regedit" to open the Registry Editor.
② found the following subkey in Registry Editor:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
③ Right-click on the EventLog project, select the "Access Permissions" option in the pop-up menu, and then locate the local ASPNET account to add in and give read and write permissions.
In this way, the ASPNET account has permission to read and write to the system log, and the event log and event source can be freely created in the program as appropriate. The methods for creating event logs and event sources in your program are as follows:
Call the CreateEventSource method of the EventLog class and specify the data source string and the event log name to create, and if the event log name is specified as empty (""), the event log name will default to application, which will not create a new event log. However, the specified data source is created for the application (application) event log. If you create a new event log, only the first 8 letters of the specified string are evaluated when the event log name is determined to be unique. As shown in the following code:
System.Diagnostics.EventLog.CreateEventSource ("WebLog", "TDDN");
An event log is created as TDDN, and an event source weblog is created under the event log. To create a custom event log on a remote computer, specify the computer name as the third parameter. The following code provides an example:
System.Diagnostics.EventLog.CreateEventSource ("WebLog", "Tddn", "MyServer");
The event log and event source will be created on the remote computer myserver.
2.3. How to write to Windows log
The event log is resolved with an event source creation problem, and the log information can then be written to the Windows system log in the program. The Write method is to first create an instance of the EventLog class, associate its Source property with the event source name, and finally call the WriteEntry method to add log information to the event log. Here is a simple example code that writes to the system log:
EventLog EventLog = null;
if (! ( EventLog.SourceExists ("WebLog"))) {
EventLog.CreateEventSource ("WebLog", "TDDN");
}
if (EventLog = = null) {
EventLog = new EventLog ("Tddn");
EventLog.Source = "WebLog";
}
EventLog.WriteEntry ("This is error! ", EventLogEntryType.Error);
}
The above program segment first determines whether the data source "WebLog" exists, if there is no call to the CreateEventSource method to create the event source, and then associate the event source with the EventLog class's source property for the write operation. The first parameter passed to the WriteEntry method represents the log message to be logged, and any message can be written. The second parameter represents the type of event log.
A, event log type classification
The type of the event log is used to indicate the severity of the event log. Each event must have a single type, and the application will indicate that type when the event is reported. The Event Viewer uses this type to determine which icon is displayed in the list view of the log. It is divided into the following five categories:
①error: An error event that indicates a serious problem that the user should know (usually a loss of functionality or data).
②failureaudit: A Failure audit event that indicates a security event that occurs when an audit access attempt fails, such as a failed attempt to open a file.
③information: An informational event that indicates an important, successful operation.
④successaudit: A Success Audit event that indicates a security event that occurs when an audit access attempt succeeds, such as a successful logon.
⑤warning: A warning event that indicates an issue that does not immediately have importance, but this issue may indicate a condition that will cause the problem in the future.
In real-world applications, selecting the appropriate event log type can make logging clearer and help developers maintain better monitoring and maintenance of the application system.
B, for the five types of event log classification, you can write a common class in the application, write a method for each type to write to the event log.
The following is a code snippet for a custom generic class:
public void Error (string sourceName, String message) {
EventLog EventLog = null;
if (! ( EventLog.SourceExists (SourceName))) {
EventLog.CreateEventSource (SourceName, "Tddn");
}
if (EventLog = = null) {
EventLog = new EventLog ("Tddn");
EventLog.Source = SourceName;
}
EventLog.WriteEntry (message, EventLogEntryType.Error);
}
public void Warning (string sourceName, String message) {
EventLog EventLog = null;
if (! ( EventLog.SourceExists (SourceName))) {
EventLog.CreateEventSource (SourceName, "Tddn");
}
if (EventLog = = null) {
EventLog = new EventLog ("Tddn");
EventLog.Source = SourceName;
}
EventLog.WriteEntry (message,system.diagnostics.eventlogentrytype.warning);
}
Similarly, there are three other types of event log write methods that can be written. This class method passes event sources (sourceName) and log messages (message) as parameters, which improves the flexibility of event log writes.
C. Methods for calling common class event log writes
The invocation of a generic class method is very simple, and the following code snippet is an example of calling the error method in a generic class:
String Strsourcename= "WebLog";
Coustomeventlog log=new Coustomeventlog ();
Log. Error (Strsourcename, "This is error!");
Strsourcename is an event source, and log is an instance of the Coustomeventlog class (that is, a generic class) and then calls the Error method to log the message "This is error!" Written to the event log.
You can also write exception information that is thrown in the program to the event log:
String Strsourcename= "WebLog";
Coustomeventlog log=new Coustomeventlog ();
try{
Execution events
C#. NET log information to the Windows log in the solution