Practical techniques for ASP.net log4net

Source: Internet
Author: User

Just beginning to contact ASP.net, about how to log records less, so a simple record of log4net configuration and use, in case you forget later.

first introduced Log4net.dll, about the file of their own Baidu download.

Then configure the configuration and add the Web.config to the configuration node

 <configSections> <section name= log4net "type=" log4net. Config.log4netconfigurationsectionhandler, log4net "/> </configSections> <log4net> <appender name= "Rollingfile" type= "log4net. Appender.rollingfileappender,log4net "> <param name=" "File" value= "log/"/> "<param" name= "AppendToFile" Value= "true"/> <param name= "Rollingstyle" value= "Date"/> "<param" name= "Datepattern" value= "Yyyy-MM-dd". Log ""/> <param name= "Staticlogfilename" value= "false"/> <layout "type=". Layout.patternlayout "> <param name=" Conversionpattern "value="%n%r%p[%d%c]%m%n "/> </layout> </app ender> <root> <level value= "All"/> <appender-ref ref= "Rollingfile"/> </root> <logge R name= "Weblogger" > <level value= "All"/> <appender-ref ref= "Rollingfile"/> </logger> </log4n Et> 

then add in the Application_Start method of Global.asax

Log4net. Config.XmlConfigurator.Configure ();

Finally write a log output class for easy invocation

public class MyLog
{
 
 private static readonly log4net. ILog log = log4net. Logmanager.getlogger ("Weblogger");
 
 Public MyLog ()
 {
 
 }
 private static void Setconfig ()
 {
 object o = Configurationmanager.getsection (" Log4net ");
 Log4net. Config.XmlConfigurator.Configure (o as System.Xml.XmlElement);
 }
 
 public static void Loginfo (String message)
 {
 if (!log). isinfoenabled)
 setconfig ();
 Log. Info (message);
 }
 
 public static void Loginfo (String message, Exception Ex)
 {
 if (!log). isinfoenabled)
 setconfig ();
 Log. Info (message, ex);
 }
 public static void ErrorInfo (String message)
 {
 if (!log). isinfoenabled)
 setconfig ();
 Log. Error (message);
 }
 
 public static void Debuginfo (String message)
 {
 if (!log). isinfoenabled)
 setconfig ();
 Log. Debug (message);
 }

Then you can use the log output in individual code!

         about log4net configuration for the time being, I haven't looked at it carefully, so I'll continue to study it when we are free.

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.