Log4net Basic Learning

Source: Internet
Author: User

Log4net is a feature-famous open source logging component. The log4net makes it easy to log information to files, consoles, Windows event logs, and databases (including MS SQL Server, Access, Oracle9i,oracle8i,db2,sqlite). And we can also record the level of log control to be recorded, log categories include: FATAL (fatal error), error (general error), WARN (warning), info (general information), debug (debug information).

Log4net present: Http://logging.apache.org/log4net/download_log4net.cgi?Preferred=http%3A%2F%2Fmirror.bit.edu.cn%2Fapache%2F

After clicking the download package, copy the Log4net.dll file to the project reference. Other specific operations are described in Log4net-sdk-2.0.8.chm, the specific interface is as follows:.

Program Demo:

Config file modification

1<?xml version="1.0"encoding="Utf-8"?>2<configuration>3<configSections>4<section name="log4net"Type="System.Configuration.IgnoreSectionHandler"/>5</configSections>6<appSettings>7</appSettings>8<log4net>9<!--definition output to file--Ten<appender name="Logfileappender"Type="log4net. Appender.fileappender"> One<!--define where files are stored-- A<file value="f:\ My Blog \9.12\log4net\ program demo \log4net_test\log4net_test\ log file \log[%property{logfilename}].txt"/> -<appendtofile value="true"/> -<!--<rollingstyle value="Date"/> the<datepattern value="Yyyymmdd-hh:mm:ss"/>--> -        -<layout type="log4net. Layout.patternlayout"> -<!--text description at the end of each log-- +<footer value="by Ink Dye"/> -<!--output format-- +<!--examples: --Geneva- -  -: the: +,111[Ten] INFO Log4netdemo.mainclass [(NULL)]-info--> A<conversionpattern value="recording time:%date thread Id:[%thread] Log level:%-5level Error class:%logger PROPERTY:[%PROPERTY{NDC}]-error description:%message%newline"/> at</layout> -</appender> -<!--definition output to the console command line-- -<appender name="Consoleappender"Type="log4net. Appender.consoleappender"> -<layout type="log4net. Layout.patternlayout"> -<conversionpattern value="%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline"/> in</layout> -</appender> to<!--define output to Windows events-- +<appender name="Eventlogappender"Type="log4net. Appender.eventlogappender"> -<layout type="log4net. Layout.patternlayout"> the<conversionpattern value="%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline"/> *</layout> $</appender>Panax Notoginseng<!--definition output to the database, here is an example output to the Access database, the database is the C-drive log4net.mdb--> -<appender name="adonetappender_access"Type="log4net. Appender.adonetappender"> the<connectionstring value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:log4net.mdb"/> +<commandtext value="INSERT into Logdetails ([logdate],[thread],[level],[logger],[message]) VALUES (@logDate, @thread, @logLevel, @ Logger, @message)"/> A<!--define each parameter-- the<parameter> +<parametername value="@logDate"/> -<dbtype value="String"/> $<size value=" -"/> $<layout type="log4net. Layout.patternlayout"> -<conversionpattern value="%date"/> -</layout> the</parameter> -<parameter>Wuyi<parametername value="@thread"/> the<dbtype value="String"/> -<size value=" -"/> Wu<layout type="log4net. Layout.patternlayout"> -<conversionpattern value="%thread"/> About</layout> $</parameter> -<parameter> -<parametername value="@logLevel"/> -<dbtype value="String"/> A<size value=" -"/> +<layout type="log4net. Layout.patternlayout"> the<conversionpattern value="%level"/> -</layout> $</parameter> the<parameter> the<parametername value="@logger"/> the<dbtype value="String"/> the<size value=" -"/> -<layout type="log4net. Layout.patternlayout"> in<conversionpattern value="%logger"/> the</layout> the</parameter> About<parameter> the<parametername value="@message"/> the<dbtype value="String"/> the<size value=" -"/> +<layout type="log4net. Layout.patternlayout"> -<conversionpattern value="%message"/> the</layout>Bayi</parameter> the</appender> the<!--define the output medium for the log, the following defines the log output in four ways. You can also output the following by one type or other type. - -<root> -< log in as!--file- the<appender-ref ref="Logfileappender"/> the<!--console control display log-- the<appender-ref ref="Consoleappender"/> the<!--Windows Event log-- -<appender-ref ref="Eventlogappender"/> the<!--If you do not enable the appropriate logging, you can comment out this way the<appender-ref ref="adonetappender_access"/> the-94</root> the  the</log4net> the</configuration>
View Code

Main output code:

1 usinglog4net;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.Linq;5 usingSystem.Reflection;6 usingSystem.Text;7 usingSystem.Threading.Tasks;8 9 //Note Add the following statement to specify that log4net use the. config file to read the configuration informationTen[Assembly:log4net. Config.xmlconfigurator (Watch =true)] One namespacelog4net_test A { -     class Program -     { the         Static voidMain (string[] args) -         { -DateTime currenttime =DateTime.Now; -Log4net. globalcontext.properties["LogFileName"] =currenttime.tostring ("YYYYMMDD"); +ILog log =Logmanager.getlogger (Methodbase.getcurrentmethod (). DeclaringType); -Log. Info ("program has started"); +Log. Error ("Program Unexpected error"); ALog. Debug ("Debug"); atLog. Warn ("warn"); - Console.readkey (); -         } -     } -}
View Code

Output:

Log file:

Log4net Basic Learning

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.