VS2010 asp.net Web site project using log4net

Source: Internet
Author: User
Tags log4net

A preface


The log4net configuration found on the Internet is mostly for Web application projects and WinForm projects, too little or no description of Web site projects. The difference between Web application project and Web site project please refer to http://www.cnblogs.com/ceachy/articles/2149075.html

This article describes the Log4net configuration under the Web site project.



Two Solutions

1. Add the Log4net.config configuration file to the Web site project, and the configuration file reads as follows

<?xml version= "1.0" encoding= "Utf-8"?> <configuration> <configSections> <!--type comma followed by log4n ET's DLL name--> <section name= "log4net" type= "log4net". Config.log4netconfigurationsectionhandler,log4net "/> </configSections> <system.web> <compi 
    Lation debug= "false" targetframework= "4.0"/> </system.web> <log4net> <!--definition output to file--> <appender name= "Logfileappender" type= "log4net".  Appender.fileappender "> <!--define file location--> <file value=" App_data/lqz.log "/> <appendtofile Value= "true"/> <rollingstyle value= "Date"/> <datepattern value= "Yyyymmdd-hh:mm:ss" ; Layout type= "log4net. Layout.patternlayout "> <!--text description at the end of each log--> <footer value=" by saibo3s "/> <!--output format --> <!--sample: 2008-03-26 13:42:32,111 [ten] INFO log4netdemo.mainclass [(null)]-info--> <conver Sionpattern value= "%date thread Id:[%thread] level:%-5level%n%c [%p]%n-Error description:%m%n"/> </layout> </appender> <!--define output to the console command line--> <appender name= "Consoleappender type=" log4net. Appender.consoleappender "> <layout type=" log4net.
        Layout.patternlayout "> <param name =" Conversionpattern "value="%d [%t]%-5level%c [%p]-%m%n "/> <!--<conversionpattern value= "%d [%t]%-5level%c [%p]-%m%n"/>--> </layout> &LT;/APPENDER&G
   T The <!--defines the output media for the log, and the following defines the log output in four different ways. You can also export by one type or other type below. --> <root> <level value= "DEBUG"/> <!--file Form log--> <appender-ref ref= "LogFile Appender "/> <!--console controls the display log, disabling--> <!--<appender-ref ref=" Consoleappender "in the Web project/>--> & Lt;/root> <!--Specifies the log level--> <!--<logger name = "Frstrightfactory" additivity = False for a child log object (the class name that requires logging) "> <level value =" DEBUG "></level> </loGger>--> </log4net> </configuration>
 


2. Modify Global.asax

[Assembly:log4net. Config.xmlconfigurator (watch=true)]

void Application_Start (Object Sender,eventargs e)
{
 //Add content Start
  string s = Server.MapPath ("Log4net.config");
  System.IO.FileInfo info = new System.IO.FileInfo (s);

 Log4net. Config.XmlConfigurator.Configure (info);
  Add content End

}

3. Use Log4net.

To define a static member variable log in a class

private static readonly ILog log = Logmanager.getlogger (typeof (My_class). Name);

Use the log method in the class's method, as follows

if (log. isdebugenabled)
{
Log. Debug ("Sfsag");
}

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.