The. NET Log tool log4net uses

Source: Internet
Author: User

1.NuGet installation log4net.

2. Modify the configuration file

<?xml version= "1.0"?><configuration> <configSections> <section name= "log4net" type= "log4net".  Config.log4netconfigurationsectionhandler,log4net "/> </configSections> <log4net> <!--OFF, FATAL,      Error, WARN, INFO, DEBUG, All-<!--Set root logger level-to-error and its appenders-<root> <level value= "All"/> <appender-ref ref= "Sysappender"/> </root> <!--Print only message S of level DEBUG or above in the packages-<logger name= "Weblogger" > <level value= "All"/> &lt ;/logger> <appender name= "Sysappender" type= "log4net". Appender.rollingfileappender,log4net "> <param name=" File "value=" logger/"/> <param name=" AppendToF Ile "value=" true "/> <param name=" Rollingstyle "value=" Date "/> <param name=" Datepattern "value=" "Lo     Gs_ "YyyyMMdd". txt ""/> <param name= "Staticlogfilename" value= "false"/> <layout type= "log4net. Layout.patternlayout,log4net "> <param name=" Conversionpattern "value="%d [%t]%-5p%c-%m%n "/> &L T;param name= "header" value= "----------------------header--------------------------"/> <param name= "Footer" Value= "----------------------footer--------------------------"/> </layout> </appender> </log4n Et></configuration>

Adding code to the 3.assemblyinfo.cs

Log Component Configuration

[Assembly:log4net. Config.xmlconfigurator (configfile = "Web. config", configfileextension = "config", Watch = True)]

4. Public class Encapsulation method call Log4net

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Configuration;4 usingSystem.Linq;5 usingsystem.web;6 7 namespacetestlog4net8 {9      Public classMylog4netinfoTen     { One         Private Static ReadOnlyLog4net. ILog log = log4net. Logmanager.getlogger ("Weblogger"); A  -          PublicMylog4netinfo () -         { the         } -  -         Private Static voidSetconfig () -         { +             Objecto = configurationmanager.getsection ("log4net"); -Log4net. Config.XmlConfigurator.Configure (o asSystem.Xml.XmlElement); +         } A  at          Public Static voidLoginfo (stringMessage) -         { -             if(!log. isinfoenabled) - setconfig (); - log. Info (Message); -         } in  -          Public Static voidLoginfo (stringmessage,exception ex) to         { +             if(!log. isinfoenabled) - setconfig (); the log. Info (Message, ex); *         } $ Panax Notoginseng          Public Static voidErrorInfo (stringMessage) -         { the             if(!log. isinfoenabled) + setconfig (); A log. Error (Message); the         } +  -          Public Static voidDebuginfo (stringMessage) $         { $             if(!log. isinfoenabled) - setconfig (); - log. Debug (Message); the         } -     }Wuyi}
View Code

5. Test Page log4nettest.aspx

1<%@ Page language="C #"autoeventwireup="true"Codebehind="Log4NetTest.aspx.cs"inherits="testlog4net.log4nettest"%>2 3<! DOCTYPE html>4 5"http://www.w3.org/1999/xhtml">6"Server">7<meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/>8<title></title>9Ten<body> One<form id="Form1"runat="Server"> A<asp:button id="button1"runat="Server"text="Button"onclick="Button1_Click"/> -</form> -</body> theView Code

Log4NetTest.aspx.cs

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Web.UI;6 usingSystem.Web.UI.WebControls;7 8 namespacetestlog4net9 {Ten      Public Partial classLog4NetTest:System.Web.UI.Page One     { A         protected voidPage_Load (Objectsender, EventArgs e) -         { -  the         } -  -         protected voidButton1_Click (Objectsender, EventArgs e) -         { +Mylog4netinfo.loginfo ("error Log Test"); -Mylog4netinfo.loginfo ("error Log Test"); +Mylog4netinfo.loginfo ("error Log Test"); A         } at     } -}
View Code

The. NET Log tool log4net uses

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.