Log4net usage Summary-(1) Use in ASP. net mvc, log4netmvc
1. Download log4net. dll from the official website and add references to the website (you can also install it through nuget)Http://logging.apache.org/log4net/download_log4net.cgi2. Add configuration in Web. config (output by log file here)
<ConfigSections> <section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <! -- Define output to file --> <appender name = "RollingLogFileAppender" type = "log4net. Appender. RollingFileAppender"> <! -- Define the file storage location --> <file value = "log \"/> <appendToFile value = "true"/> <rollingStyle value = "Date"/> <datePattern value = "yyyy-MM-dd'.txt '"/> <staticLogFileName value = "false"/> <param name = "MaxSizeRollBackups" value = "100"/> <param name = "Encoding" value = "UTF-8 "/> <layout type =" log4net. layout. patternLayout "> <conversionPattern value =" % newline % nDate: % date % nThread: [% thread] % nLevel: %-5 level % nClass: % logge R [% property {NDC}] % nMessage: % message % newline "/> </layout> </appender> <root> <level value =" ALL "/> <! -- Log recorded as a file --> <appender-ref = "RollingLogFileAppender"/> </root> </log4net>
3. Test and use the controller.
public class HomeController : Controller { log4net.ILog log = log4net.LogManager.GetLogger(typeof(HomeController)); public ActionResult Index() { log.Info("test...."); return View(); }}
4. Pay attention to the configuration step.According to the above steps, the log does not work, because in asp.net MVC, your log4net configuration needs to be added to web. config to search for log4net configuration during startup. Find AssemblyInfo. cs in the Properties file of the site, and append [assembly: log4net. Config. XmlConfigurator (ConfigFile = "Web. config", Watch = true)]
5. Test Run resultsIn addition, the configuration file contains <param name = "Encoding" value = "UTF-8"/> to prevent Chinese garbled characters in some cases.