After the system is deployed, the administrator needs a powerful log system to diagnose and fix configuration problems. This requires a set of configuration-based log record change methods. Experience shows that the Log Module is often an important part of the software development cycle. aicken has used log4net for a while and log4net is a good log framework, however, sometimes it still seems a little difficult. For example, you need to use different types of logs to record the behaviors of different classes and methods and different business behaviors. Sometimes, during system runtime, changes the record media or whether or not to record a Service Log. In this case, log4net may need to modifyCodeThe applogprovider module of isline framework can only modify the configuration file to meet the requirements. It has the following advantages:
- It provides applicationsProgramAccurate runtime environment for developers to find bugs in applications as soon as possible;
- Once the log output code is added to the program, logs can be generated and output during the program running without manual intervention.
- Different log information can be output to different places (such as the console, files, and databases) for future research.
These two providers are developed based on these needs. They are responsible for recording the running logs and runtime exceptions in the system, but the applogprovider of the isline framework is more flexible and easy to use than log4net, the advantages of this module are as follows:
- The render component defines the output media. As shown in the name, these components attach them to the log component and pass the output to the output stream. You can appender a component to multiple log objects.
- Flexible control of the log level. After the system is deployed, you can use the level control to quickly switch whether to record the information of a service block.
- By setting the log cache, the system collects logs and exception information of resources in a centralized manner. When a threshold value is reached, the system can efficiently write data to the storage media.
- Through the configuration file, you can quickly change the log recording media (files, databases), record the technology used, without changing the program file, you only need to change the configuration file.
- Users can precisely control the output of log information, reducing unnecessary information and improving log record performance. In addition, with the external configuration file, you can change the log behavior of the application without re-compiling the program, so that you can flexibly select the information to be recorded based on the situation.
Among the two providers, there is a very important concept: "render", which is the configuration node of the log exception module, it determines the target, record method, path, and other information of the system log record. The information can be obtained through configuration. It can be a database, text file, XML file, or even a TV set, however, apart from the built-in render of databases and files, other record media must inherit related interfaces and then expand the implementation.
Currently, this Provider supports the recording of SQL Server, Oracle, and text files. You can use SQL statements or stored procedures as recording methods.
The following is a render instance
<Oracleusingsprender>
<Appenderstyle> Oracle </appenderstyle>
<Appendertype> storedprocedure </appendertype>
<Appenderinf> isline. Data. configuration. oracleconnectstring </appenderinf>
<Appendercontent> sp_add_ilfwlog.add_ilfwlog </appendercontent>
<Layoutinf> </layoutinf>
<Level> debug </level>
</Oracleusingsprender>
<Oracleusingsprender> </oracleusingsprender> is a render node with the render name in brackets. The default render includes:
Oracleusingsprender: Use the stored procedure log of the Oracle database
Oracleusingtextrender: use SQL statements of the Oracle database to record logs.
Sqlserverusingsprender: Use the stored procedure log of the SQL Server database
Sqlserverusingtextrender: use SQL statements of the SQL Server database to record logs.
Filerender: records logs to text files.
Mailrender: Send Logs to recipients
The above logging method is already built into the isline framework and supports logging. For detailed configuration methods, refer to the author's blog. I will not elaborate on it here.
Render includes four parts: appenderstyle, appendertype, appenderinf, appendercontent, and level ).
"Appenderstyle" indicates the logging method, which can be databases, files, etc., which is an enumeration type;
"Appendertype" indicates the Attachment Form, which can be a file or a stored procedure name.
"Appendercontent" indicates the record content. When appenderstyle specifies that the database is used as the Log Record Medium and appendertype indicates the stored procedure, appendercontent indicates the name of the stored procedure; appendertype indicates the SQL statement, "appendercontent" is a specific SQL statement.
"Level" indicates the record level. When the level is smaller than the global log level, the log instance does not work. The Global log level is configured in Web. config, which is described below.
"Appenderinf" indicates the record path or the extended program used;
In the ilfw framework, by setting the configuration file, each log object is assigned a log priority level, that is, the value in the level node, at the same time, the configuration file also contains a global key value string. When the enumerated value in the level node is smaller than the global level, this log object will not be recorded. The level-level relationship is off> fatal> error> debug> warn> info> all. These relationships can be configured using the level keyword. The level information is as follows. configure global level in config:
<Add key = "isline. applog. configuration. Level" value = "debug"/>
In this way, render greater than or equal to the debug level will be recorded. When the level is set to all, the global level is the lowest, and applogprovider records information of all instances. When the level is set to off, the global level is the highest, applogprovider does not record any instance information.
● Isline. applog. applogprovider namespace:
The namespace provides the ability to record system behavior to the media in the previous volume logic. The upper-layer business can record the current breakpoint behavior by calling the writelog () method.
Before calling this method, you must obtain the log entity in advance and pass the object as a parameter to the preceding method. The log entity standardizes the recorded content and records the fields as standard. net exception fields, which correspond to the database or other appender. When the applogprovider obtains the log entity, it loads the render module to render the basic log information.
By loading configuration information such as record media, attachment mode, attachment format, and report level, render writes logs.
When the program calls applogprovider, it needs to be instantiated. During the instantiation process, it specifies which render to use, and different instances correspond to different configuration information, and different levels and recording media can be matched, as shown below:
Figure 4.1 Effect of constructor Configuration
As shown in, a log instance is instantiated before use. You can select the built-in method in Step 6 during instantiation. We Just configured the file node information, so select "file" here ":
// Define a log instance
Logprovider Lp = new logprovider (appenders. File)
// Define the log entity
Logentity Le = new logentity ();
// Assign a value to the log Content Field
Le. Exception = "isline test ";
Le. Message = "OK ";
// Write log Content to media
LP. writelog (LE );
In this way, the log writing process is completed, and the effect is as follows:
Figure 4.2 document log format
If you want to change the log write attribute after the day, for example, if you want to output the log to Oracle, you can set the "appenderstyle" node name value to "oracle" and enter the corresponding content in other nodes, in this way, the "filerender" render points to the Oracle media. The isline render module automatically renders an oracle bearer for use internally, without any modifications to the code. You can even define a custom log render. The input parameter in the new logprovider is the name of the custom node "yourname ". For more information, see the author's blog.
The Log Module allows the caching method, that is, when the system accumulates the log information to a threshold value, it writes the log information to the storage media in batches, thus enhancing flexibility. Applogprovider provides multiple methods to record one log message. You can create multiple applogproviders in an application.
Global cache. logs are written only when they are higher than the cache. For example, if you define that the number of logs in the cache is greater than 2 and then trigger log writing, you can add the following configuration information in Web. config:
<Add key = "isline. applog. configuration. buffersize" value = "2"/>
The default cache policy is disabled. The cache policy is enabled only when the above Code is added.
Isline applogprovider is a very useful Log Module and its flexibility is its feature. This section mainly describes log operations. In the next section, we will discuss exception handling.
This article has been published simultaneously in the software report and cannot be reproduced by any personal media.