1.1. Overview
The Log Module is an important part of the enterprise application system, it can help the system administrator record errors during system operation, help system operators record routine operation records of system users, and help developers debugProgramInternal state information of the program. The Log Module plays an important role in improving the robustness, reliability, and user friendliness of enterprise application systems.
Generally, a log module has the following features:
- Log Level: Based on the log Requirements of the application system, logs are classified into debugging, information, warning, and error levels. The log level can be used to classify logs, or to restrict log registration based on the log level.
- Log output target. Logs can be recorded to the database (various databases), files, console, and other devices as needed.
- Log format. You Can Customize various log formats as needed.
Our Log Module needs to achieve the following goals:
- Make sure thatCodeClear and intuitive.
- Ensure application conventions are better than configurations
- Make sure the Application Block is scalable
- Ensure that the performance impact is minimal or negligible (compared with the Log Code compiled manually to implement the same function)
1.2. Design
Currently, there are many excellent log systems on the market, such as log4net, nlog, and logging Application Block. These systems define their respective interfaces, and their configurations are relatively cumbersome and do not have the function of reading and analyzing logs, based on the mature log systems on the market, design a log interface with a common log interface, eliminate tedious log configurations and add the log reading and analysis functions based on the rule of better than the configuration.
We use a common interface, and its implementation cannot be bound to a specific log system. We need a design that can implement this binding outside the code. We adopt the factory method mode to select the specific log implementation.
Description of the introduced factory method mode:
1.3. Implementation
First, we need to define the log interface. The Code is as follows:
/// <Summary> /// Ilog provides an interface for Application Log operations /// </Summary> Public Interface Ilog { /// <Summary> /// Write log messages /// </Summary> /// <Param name = "message"> Log message </Param> /// <Param name = "level"> Log Level </Param> Void Write ( String Message, loglevel level );}
The Factory Code is as follows:
/// <Summary> /// Log Factory /// </Summary> Public Sealed Class Logfactory { Private Logfactory (){} /// <Summary> /// Create an instance by name /// </Summary> /// <Param name = "name"> Use the log Class Name </Param> /// <Returns> Log Interface </Returns> Public Static Ilog create ( String Name) {config cfg = New Config (); String Providername = cfg. properties [" Logging. providername " ]. Tolower (); If (Providername. Equals ( " Log4net " )){ Return New Log4netmanager (name );} Else { Return New Simplelogmanager ();}}}
The code for subclass of the specific implementation interface is as follows:
/// <Summary> /// Log4netmanager provides log4net-based application log operations /// </Summary> Public Class Log4netmanager: ilog { Private Log4net. ilog _ log; Public Log4netmanager (String Name) {_ log = Log4net. logmanager. getlogger (name );} Public Log4netmanager (type) {_ log = Log4net. logmanager. getlogger (type );} /// <Summary> /// Write log messages /// </Summary> /// <Param name = "level"> Log Level </Param> /// <Param name = "message"> Log message </Param> Public Void Write ( String Message, loglevel level ){ Switch (Level ){ Case Loglevel. Debug: If (_ Log. isdebugenabled) _ log. debug (Message ); Break ; Case Loglevel. Info: If (_ Log. isinfoenabled) _ log. Info (Message ); Break ; Case Loglevel. Warn: If (_ Log. iswarnenabled) _ log. Warn (Message ); Break ; Case Loglevel. Error: If (_ Log. iserrorenabled) _ log. Error (Message ); Break ;}} }
1.4. Summary
This chapter describes the features of a good Log Module, how to design and construct a log module, and how to use the factory method model in actual scenarios.
1.5. Resource
- . Net open-source project introduction and resource recommendation: Logging
- . Net design mode (5): factory Method)