Log4net implementing multi-instance logging

Source: Internet
Author: User
Tags log4net

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.