Original address: Implementing multiple Log4net Logger instances
This article is excerpt, please check the original text.
Logging with log4net is convenient for. NET framework developers, and it is often easy to write a configuration file and simple coding to easily implement the application's logging capabilities. But because there are times when we do not have time to read the documentation in detail, there are many mistakes that are inadvertently made, and the following is a mistake caused by not reading the profile document carefully.
Application Environment:
A Web application with a three-tier architecture, so logging is divided into application logs, data access logs, and operational logging three logs. Three logs are saved in separate files, thus creating three log classes, each with an instance of the Log4net logger, which produces the following problems:
1. Corresponding to the logger name in the configuration file
In general, the Logger node is added in the configuration file, which corresponds with the name attribute. When creating a logger (Logger) in the source code, use the GetLogger method and the logger name to get the corresponding logger instance. The name of my log record is obtained by the class name, using GetType (...). The property of the name is set.
2. Control of the logging level of each logger in the configuration file
The level tag can be used to control the output levels of each logger, respectively, fatal (fatal error), error, Warning, Info, Debug, corresponding to the logger method of the same name.
3. Solve the problem of writing to other loggers in the same level log (focus on resolution)
The root element is also used to add the appender to the root element, and we will append the Appender to the respective logger tags. In fact, logger and root have inherited relationships, which means that if you have a logger corresponding Appender is appended to the root node, Even if the other logger does not append this appender will also gain the ability to output logs to this appender by inheriting the properties of root. The way to let individual logger do not inherit root is to set false on the Logger property additivity.
Log4net implementing multi-instance logging