The log information in c sharp.NET is written to the solution in Windows logs

  • 2020-05-05 11:43:30
  • OfStack

1. The purpose of   is    
Application system development and maintenance is inseparable from the log system, the choice of a powerful log system solution is a very important part of the application system development process. There are many kinds of logging system solutions in the.net environment, and log4net is one of the best.  
In Windows2000 and above, there is an Windows logging system that includes application (Application) event logs, system (System) logs, and security (Security) logs. Event logs can also be custom logs. Classes and interfaces are also provided in.net   Framework to use application event logs or custom event logs. Using Windows logging enables better integration between the application system and the operating system. Compared with using a custom logging system, it is more convenient to query and manage logs with the support of the operating system. In the practical application, according to the actual situation, you can choose a suitable logging solution, or you can customize the logging system and Windows logging system two logging solutions to use at the same time.  
2.   USES Windows log method    
2.1 method overview    
A class EventLog is provided in.net   Framework, which allows you to add new event log entries or retrieve existing entries from the server event log. The EventLog class includes an WriteEntry() method that you can use to write a new event to the event log. When writing a new entry to the event log, a specific event source (event   source) is used to write the entry to the specific event log.  
The event source is unique to the event log. In Windows2000 and above, there is an event log: application (Application) event log, as well as system (System) and security (Security) logs, and the system allows custom event logs. Using the EventLog class, you can add log entries to the application (Application) event log or to the custom event log. The event source is 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 an event source, which can be created in the application (Application) event log or in the custom event log. The Windows logging system is shown in the following figure:
For the convenience of distinguishing and viewing logs between different application systems, event sources are generally created in the custom event logs, which can create multiple event logs, and an event log can also 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 write registry requires special permissions, so in Web project to create the event log and event source permissions and security problems, Application project does not exist in this problem, this paper focuses on Web Windows log to use in the project, Application using methods similar to Web project in the project, abandoned Web project permissions and security in the processing, this article is no longer here.
In the Web project, when using asp.net to create an event log or an event source into the system, you might get the following exception error message:   System.Security.SecurityException:   does not allow the requested registry access. This is because the default account for running the asp.net process is ASPNET (NetworkService below IIS6.0), and this account defaults to read only, not write, so you cannot create an event log or event source. The solution to this problem is to increase the permissions of the ASPNET account, not to create an event log or event source within the program, etc. There are mainly three solutions:
Before the program runs, define the event log and event source to use, open the registry editor, and manually add the event log and event source to the registry. The main steps are as follows:
Click the "start" menu, then click "run".
          in the "run" "open" box, enter "regedit", then press the OK button, open the registry editor.
          finds the following subkey in the registry editor:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
          right click "Eventlog", click "new", and then click "item", will create a new project in the next level of "Eventlog", the project will be named "TDDN",   TDDN item is the event log, you can name this item according to the actual situation, such as the name of the project.
          right click on the "TDDN" item, click "new", then click "item", will create a new item in the next level of "TDDN", this is named "Weblog",   Weblog item is the event source, this can also be named according to the actual situation.
          close the registry editor.
The event log and event source are set up, and if you need more than one event log or event source, repeat the process. This approach requires familiarity with the registry, which can be a bit complicated to operate. You can write a class to configure the registry, and then add the corresponding items as long as you run the class, eliminating the need for manual addition. This is the second solution:
B,     has an EventLogInstaller class in the System.Diagnostics namespace that can create and configure event logs and event sources to be read and written by the application at run time. You can use the EventLogInstaller class to create an event log and event source:
          use C# to create a "class library" called EventLogSourceInstaller.
(2)           in this project. Add to System Configuration. Install. dll references.
          will automatically generate Class1.cs renamed MyEventLogInstaller.cs.
          in MyEventLogInstaller.cs write the following code:
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   be   created.
myEventLogInstaller.Source   =   "WebLog";
//   Set   the   Log   that   source   is   created   in
myEventLogInstaller.Log   =   "TDDN";
Installers.Add(myEventLogInstaller);
}
}
}
(5)           generates the project to obtain 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 the event source.
This creates an event log in the system: TDDN, and an event source under the event log TDDN: WebLog.
The above two solutions are to manually add event log and event source for the system before the application system runs, which has no security permission problem and no security risks. A more convenient way is to have the program automatically create event logs and event sources while the application is running. This method must either increase the system operation privileges of the ASPNET account or give the asp.net process a mock account with greater privileges. The implementation of the emulated account is complex, and there is no difference in functionality and security from upgrading the ASPNET account. Here's the third solution:
C,     upgrade ASPNET account permissions can be directly in the Windows system management to ASPNET account to add access to the system, but there are serious security problems, asp. The method adopted here to enhance the ASPNET account permission is to only assign the operation permission of the system log to ASPNET account. In this way, although there is still a certain security risk, but the risk has been greatly reduced, and can freely create the event log and event source within the program, greatly improving the flexibility. The ASPNET account permission promotion method is as follows:
Click "start", "run", enter "regedit", open the registry editor.
          find the following subkey in the registry editor:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
          right click on the Eventlog project, select the "access permission" option in the popup menu, and then find the ASPNET account of the machine to add and give read and write rights.
This gives the ASPNET account permission to read and write to the system log, and allows you to create event logs and event sources in your program as you see fit. The way to create an event log and event source in a program is as follows:
The CreateEventSource   method of the   EventLog class is called and specifies the data source string and the event log name to be created. If the event log name is specified as empty   (""), the event log name defaults to Application. No new event logs are created, but the specified data source is created for the application (Application) event log. If a new event log is created, only the first 8   letters of the specified string are counted when determining whether the event log name is unique. The following code is shown:
System.Diagnostics.EventLog.CreateEventSource("WebLog",   "TDDN");
An event log is created as TDDN, under which an event source WebLog is created. To create a custom event log on a remote machine, specify this computer name as the third parameter. The following code provides an example:
System.Diagnostics.EventLog.CreateEventSource("   WebLog   ",   "   TDDN   ",   "myserver");
The event log and the event source will be created on the remote computer myserver.
2.3 the method of writing Windows log  
The issue of event log and event source creation is resolved, and the log information can then be written programmatically to the Windows system log. The write method is to first create an instance of the EventLog class, associate its Source attribute with the event source name, and finally call the WriteEntry method to add log information to the event log. Here is a simple example of writing to a 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 it does not, the CreateEventSource method is called to create the event source, and then the event source is associated with the Source attribute of the EventLog   class for a 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 event log is used to indicate the severity of the event log. Each event must have a single type that the application indicates when the event is reported. The event viewer USES this type to determine which icon to display in the list view of the log. It falls into five categories:
          Error: an error event that indicates a serious problem (usually a loss of functionality or data) that the user should be aware of.
          FailureAudit: failed audit event, which indicates the security event that occurs when an audit access attempt fails (e.g., an attempt to open a file fails).
          Information: information event, which indicates an important and successful operation.
        SuccessAudit: successful audit event, which indicates the security event that occurs when the audit access attempt is successful (e.g., successful login).
          Warning: warning event, which indicates a problem that is not immediately significant, but which may indicate a condition that will cause a problem in the future.
In practice, choosing the right type of event log can make logging clearer and help developers and maintainers better monitor and maintain the application system.
B,  , for the five categories of event logs, can write a generic class in the application and a method for each type to write to the event log.
The following is a snippet of the 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, you can write the other three types of event log writing methods. This class of methods passes the event source (sourceName) and the log message (message) as parameters, which increases the flexibility of writing the event log.
C,   calls
, which writes to the generic class event log The invocation of a generic class method is very simple, and the following code snippet is an example of invoking the Error method in a generic class:
string   strSourceName="WebLog";
CoustomEventLog   log=new   CoustomEventLog();
log.Error(strSourceName,"This   is   Error!");
strSourceName is the event source, log is an instance of the CoustomEventLog class (that is, the generic class), and then the Error method is called to log the message "This   is   Error!" Write to the event log.
You can also write the exception information thrown in the program to the event log:
string   strSourceName="WebLog";
CoustomEventLog   log=new   CoustomEventLog();
try{
// execute events

Related articles: