Home> Java> javaTutorial> body text

A detailed introduction to Log4net logging and a detailed introduction to the application code

黄舟
Release: 2017-03-08 10:26:54
Original
1685 people have browsed it

This article mainly introduces the detailed introduction of Log4net logging and related application information. Friends who need it can refer to

Log4net log

Summary :

Log4net is a tool that helps programmers output log information to various targets (console, file, database, etc.). The log is the black box of the program. You can use the log to view the running process of the system and discover system problems. The function of logs: record the steps, successes and failures of the running process, record key data, and then analyze system problems. Because for websites, exception information cannot be displayed to users, and exception information can only be recorded in the log. When there is a problem with the website, the developer knows the problem by looking at the logs.

1. How to configure the Log4net environment

1. Add the "Application Configuration File" (App.config);

2. Add configuration in App.config or Web.config:

                 
Copy after login

3. Add Log4net.dll reference;

4. Set the property "Copy to output directory" of log4net.config to "Always copy";

5. Initialization:

Add log4net.Config.XmlConfigurator.Configure at the beginning of the program () Let the current Log4net work;

6. Write LonManager.GetLogger(typeof(Program)).Debug("information"); where you want to print the log.

Pass the name of the log class to be recorded through LonManager.GetLogger() to obtain the class ILog, so that in the log file you will know which class outputs the log, and then call the Debug method to output the message. Because logs need to be printed in more than one place within a class, ILog is generally declared as a static field.

7. Use the Ilog.Error method to output error information. The second parameter can pass an Exception object. Log.Error("****Error"+ex);

2. Log4NetDemo

namespace Log4NetDemo { class Program { static void Main(string[] args) { //使用log4net记录日志。 log4net.Config.XmlConfigurator.Configure(); ILog logWriter =log4net.LogManager.GetLogger("Test"); logWriter.Info("消息"); logWriter.Warn("警告"); logWriter.Error("异常"); logWriter.Fatal("错误"); } }}

        

三、Appender

可以在配置文件中使用Log4net的Appender方法进行优化;

        
  更多内容,推荐阅读: //m.sbmmt.com/;/a> 以上是我对Log4net日志记录的简单理解,请路过的大鸟指点。 


        
Copy after login


The above is the detailed content of A detailed introduction to Log4net logging and a detailed introduction to the application code. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!