Log4net integrated into the MVC+EF framework

Source: Internet
Author: User
Tags log4net

This article is transferred from: log4net integrated into MVC+EF framework Asp.net.net-itnose

In more detail: The detailed installation steps for log4net in MVC Asp.net.net-itnose

Prerequisite Reference Log4Net.dll File

1,

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

The above code is written to the AssemblyInfo.cs file

2. In the Global.asax file, the initialization code for the log4net is added to the Application_Start method as follows:

String l4net = Server.MapPath ("~/web.config");
Log4net. Config.XmlConfigurator.ConfigureAndWatch (New System.IO.FileInfo (l4net));

3, the configuration Web.config file, the contents are as follows, some configuration instructions, see note



     <!--This configuration must be placed on the first node of configuration--> <configSections> <section name= "log4net" log 4net.
      Config.log4netconfigurationsectionhandler,log4net "/> </configSections> <log4net debug=" true "> <appender name= "Errorappender" type= "log4net". Appender.rollingfileappender "> <filter type=" log4net. Filter.levelmatchfilter "> <leveltomatch value=" ERROR "/> </filter> <filter type = "Log4net." Filter.denyallfilter "/> <!--log file name opening--> <file value=". \\Logs\\Error\\ "/> <!--append to file--> <appendtofile value=" true "/> <!--mixed date and file size transform days Log filename--> <rollingstyle value= "composite"/> <!--maximum number of transformations--> <maxsizerollbackups "Ten"/> <!--maximum file size--> <maximumfilesize value= "500KB"/> <!--date format Staticlogfilename
   If true, the file name of the current log file (in relation to the history log file) takes only the file parameter.    If False, the filename is file+datepattern. --> <staticlogfilename value= "false"/> <!--The following format log can be archived by month--> <datepattern value= " Yyyy-mm/yyyy-mm-dd. ' Log ', '/> <layout type= ' log4net. Layout.patternlayout "> <conversionpattern value="%date%logger:%message%newline "/> </layou t> </appender> <appender name= "Infoappender" type= "log4net". Appender.rollingfileappender "> <filter type=" log4net. Filter.levelmatchfilter "> <leveltomatch value=" info "/> </filter> <!--the day that does not conform to the info condition Zhi, do not do output--> <filter type= "log4net. Filter.denyallfilter "/> <!--log file name opening--> <file value=". \\Logs\\Info\\ "/> <!--append to file--> <appendtofile value=" true "/> <!--Mix date and file size transform logs FileName--> <rollingstyle value= "composite"/> <!--maximum number of transformations--> <maxsizerollbackups " Ten "/> &Lt;! --Maximum file size--> <maximumfilesize value= "500KB"/> <!--date format--> <staticlogfilename "False"/> <datepattern value= "yyyy-mm-dd/> <layout type=" log4net. Layout.patternlayout "> <conversionpattern value="%date%logger:%message%newline "/> </layou t> </appender> <root> <level value= "All"/> <appender-ref ref= "errorappend Er "/> <appender-ref ref=" Infoappender "/> </root> </log4net>

4, add LogHelper.cs file

   public class Loghelper {public static void debug (String message) {log4net. ILog log = log4net.
              Logmanager.getlogger ("LogOut"); if (log. isdebugenabled) {log.
              Debug (message);
         log = null; public static void error (String message) {log4net. ILog log = log4net.
             Logmanager.getlogger ("LogOut"); if (log. iserrorenabled) {log.
             Error (message);
         log = null; public static void Fatal (String message) {log4net. ILog log = log4net.
             Logmanager.getlogger ("LogOut"); if (log. isfatalenabled) {log.
             Fatal (message);
         log = null; public static void info (String message) {log4net. ILog log = log4net.
     Logmanager.getlogger ("LogOut");        if (log. isinfoenabled) {log.
             Info (message);
         log = null; public static void Warn (String message) {log4net. ILog log = log4net.
             Logmanager.getlogger ("LogOut"); if (log. iswarnenabled) {log.
             Warn (message);
         log = null; }
     }

To this point, you can start your log4net trip

.

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.