Log4net Configuration and use

Source: Internet
Author: User
Tags log4net

Brief introduction

Almost all large applications will have their own API for tracking debugging. Because once the program is deployed, it is less likely to take advantage of specialized debugging tools. However, an administrator may need a powerful logging system to diagnose and fix problems with the configuration.

Experience has shown that logging is often an important part of the software development cycle. It has several advantages: it provides an accurate environment for the application runtime and allows developers to find bugs in the application as quickly as possible, and once the log output code is added to the program, the log information can be generated and output without human intervention during the program's operation. In addition, the log information can be exported to different places (consoles, files, etc.) for future research purposes.

Log4net is designed for such a purpose, for. NET development environment for logging packages.

Official website: http://logging.apache.org/log4net/

How to use

First step: Installation of Log4net

Install-package log4net

Step two: Configuration of the Log4net


Note: The Log4net.config property "copy to Output directory" is set to "Always copy"

<?xml version="1.0"encoding="Utf-8"?><configuration> <configSections> <section name="log4net"Type="log4net. Config.log4netconfigurationsectionhandler, Log4net"/> </configSections> <log4net> <appender name="Rollinglogfileappender"Type="log4net. Appender.rollingfileappender"> <!--log path--<param name="File"Value="D:\App_Log\ "/><!--whether to append the log to the file--<param name="Appendtofile"Value="true"/> <!--log retention days--<param name="maxsizerollbackups"Value="Ten"/> <!--The log file name is fixed--<param name="Staticlogfilename"Value="false"/> <!--log file name format is: -- ,- to.log--> <param name="Datepattern"Value="Yyyy-mm-dd&quot;. log&quot;"/> <!--logs by date-<param name="Rollingstyle"Value="Date"/> <layout type="log4net. Layout.patternlayout"> <param name="Conversionpattern"Value="%d [%t]%-5p%c-%m%n%loggername"/> </layout> </appender> <!--console display log--<appender name="Coloredconsoleappender"Type="log4net. Appender.coloredconsoleappender"> <mapping> <level value="ERROR"/> <forecolor value="Red, Highintensity"/> </mapping> <mapping> <level value="Info"/> <forecolor value="Green"/> </mapping> <layout type="log4net. Layout.patternlayout"> <conversionpattern value="%N%DATE{HH:MM:SS,FFF} [%-5level]%m"/> </layout> <filter type="log4net. Filter.levelrangefilter"> <param name="Levelmin"Value="Info"/> <param name="Levelmax"Value="Fatal"/> </filter> </appender> <root> <!--(h) OFF > FATAL > ERROR > WARN > I NFO > DEBUG > All (Low)-<level value=" All"/> <appender-ref ref="Coloredconsoleappender"/> <appender-ref ref="Rollinglogfileappender"/> </root> </log4net></configuration>

Step Three: Call

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;usinglog4net;usinglog4net. Config;namespacelog4net{classProgram {Static voidMain (string[] args)            {initlog4net (); varLogger = Logmanager.getlogger (typeof(program)); Logger. Info ("message"); Logger. Warn ("Warning"); Logger. Error ("Exception"); Logger. Fatal ("Error");        Console.ReadLine (); }        Private Static voidinitlog4net () {varLogcfg =NewFileInfo (AppDomain.CurrentDomain.BaseDirectory +"Log4net.config");        Xmlconfigurator.configureandwatch (LOGCFG); }    }}


Understanding the structure of log4net

Log4net has four main components, namely Logger (recorder), Repository (library), Appender (attachment), and layout.

External Blog Resources: http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html

Log4net Configuration and use

Related Article

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.