Log4net Tips for use

Source: Internet
Author: User
Tags apache log log4net

Log4net Tips for use1.log4net IntroductionLog4net Library is the Apache log4j framework at Microsoft. NET platform is a tool that helps programmers to output log information to various targets (consoles, files, databases, etc.). Log4net is part of the Apache Software Foundation Apache Logging Services Project. The Apache Log service project is dedicated to providing cross-language logging services for program debugging and auditing.
today, I lead you to log logs using Log4net on ASP.
2.log4net Download:http://download.csdn.net/detail/abc456456456456/7653857
3. Steps to use3.1create a new ASP. NET Web application, creating three layers of code, adding the required class libraries, (1):

Common: Global common class library file;wordfilter: Display layer code base;WordFilter.BLL:BLL Code Layer Library
WordFilter.DAL:DAL Code Class Library
WordFilter.Model:Model Code Class Library

3.2 Configure the log4net in the Webconfig file;The code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?><!--For more information about how to configure an ASP. NET application, go to http://go.microsoft.com/fwlink/? linkid=169433--><configuration> <configSections> <section name= "log4net" type= "log4net". Config.log4netconfigurationsectionhandler, log4net "/> </configSections> <log4net> <!--define the log record type to be rolled Dynamic file Type--<appender name= "Rollinglogfileappender" type= "log4net. Appender.rollingfileappender "> <!--define the location of the log file, which is the Moumoulog.txt file under the log folder under the Web site root and <file value=" log/       Moumoulog.txt "/> <!--log appended in the log file--<appendtofile value=" true "/> <!--maximum number of logs are up-to-date <maxsizerollbackups value= "Ten"/> <!--maximum capacity per log file, which automatically creates a new log file, more than that capacity-<maximumfilesize value = "1024KB"/> <rollingstyle value= "Size"/> <!--true, the current log file name is always the name in the file section----&LT;STATICLOGFI Lename value= "true"/> <layout type= "log4net. Layout.patternlayout "> <Conversionpattern value= "%date [%thread]%-5level%logger-%message%newline"/> </layout> &LT;/APPENDER&G    T <root> <!--logging level, default is debug--> <level value= "DEBUG"/> <appender-ref ref= "Rollinglogfile Appender "/> </root> </log4net> <appSettings> <!--The connection string is encrypted--<add key=" Constri Ngencrypt "value=" false "/> <!--database connection string, (if encrypted, the above one is set to true; the encryption tool can be downloaded in the official download, if clear text is used in this way Server=127.0.0.1;databa Se=..., set to false above. --<add key= "ConnectionString" value= "Server=.\sqlexpress;database=wordfilter;uid=sa;pwd=hello"/> </ appsettings> <system.web> <compilation debug= "true" targetframework= "4.0"/> 
3.3 Common Class Library add a reference to Log4net:

3.4 Common class Library Add LogHelper.cs class file

Using system;using system.collections.generic;using system.linq;using system.text;using System.IO;namespace moumou.enterprise.common{public    class Loghelper    {        //log4net log dedicated public        static readonly log4net. ILog loginfo = log4net. Logmanager.getlogger ("Loginfo");        public static readonly log4net. ILog logerror = log4net. Logmanager.getlogger ("Logerror"); <span style= "White-space:pre" ></span>
<span style= "White-space:pre" ></span>//settings log4net get configuration information, get public static void Setconfig from configuration file () { Log4net.        Config.XmlConfigurator.Configure (); }<span style= "White-space:pre" ></span>//<span style= "font-family:arial, Helvetica, Sans-serif;" > Get </span> public static void Setconfig (FileInfo configfile) {log4net from file.        Config.XmlConfigurator.Configure (ConfigFile); }///<summary>///Normal file Log///</summary>//<param name= "info" ></par am> public static void Writelog (string info) {if (loginfo. isinfoenabled) {Loginfo.            info (info); }}///<summary>//error Log///</summary>//<param name= "info" ><        /param>//<param name= "se" ></param> public static void Writelog (string info, Exception se)         {   if (logerror. iserrorenabled) {Logerror.            Error (info, SE); }        }    }}

3.5 in Global.asax fileApplication_Start method Start call Log4net

protected void Application_Start (object sender, EventArgs e) {loghelper.setconfig (); Loghelper.writelog ("System Boot");}

3.6 Logging error logs in Application_Error
protected void Application_Error (object sender, EventArgs e) {//code to run in case of unhandled error exception Objexp = HttpContext.Current.Server.GetLastError (); Loghelper.writelog ("Exception:" + Server.GetLastError (). Message, objexp);}

The error log is logged as follows:


Log4net tips for using

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.