Using log4net

xiaoxiao2021-03-06  42

Log4net is a log assembly developed by Apache. Like its sister log4j, it is an open source project. You can apply in your system in your plug-in. Use only how to apply in the Web Forms project. Be the main log output component .

1. First you should download log4net.dll and introduce you in your project.

2. You need to modify your global.asa.cs. Load log4net configuration when configuring the Application object. This step is not missing.

protected void

Application_Start (Object Sender, Eventargs E)

{Log4net.config.domconfigurator.configure ();

3. You can see that there is no parameters above. It can be seen that the default configuration is loaded. This configuration must be set in Web.config.

Add the following section in the Web.cofig root node Configuration:

4. The config section declares another config section named log4net. The latter must be in the Web.config root node Configuration: The following is an Sample:

5. The above defines multiple appenders. Simply, every appender is an output medium.

6. The root node specifies the selected appender. This example selects logfileAppender. (Text file output). Define the output format in the Appender definition. And the location of the target text file. (The starting position is the application root directory. Web.config's directory).

7. Configure log4net to the current location. You can use it directly in our app.

8. The application method:

To output a log, you must first get the Logger with an alias.

Use the following command

(C #):

Log4net.ilog logger logger = log4net.logmanager.getlogger (this.gettype ());

(You can use GetType directly to get the current class name)

Afterward

Logger.info (String Message);

Logger.ERROR (String Message);

Logger.debug (string message);

You can output the log.

After debugging, you can find whether the application root directory has automatically created the XXXXXApplication.log.txt text file. And whether the log is output correctly.

Log4net is a very perfect log component. It has powerful configurability. Helps improve development efficiency.

About the log4net configuration. You can refer to the official documentation of the Apache organization.

Http://logging.apache.org/log4net

转载请注明原文地址:https://www.9cbs.com/read-70065.html

New Post(0)