Log4net usage Summary-(2) use in ASP. net mvc, log4netmvc
Log4net configuration in ASP. net mvc, there is also a configuration method, that is, not in web. config, but to create a separate log4net. config under the root directory1. Reference log4net. dll 2. Add log4net. config under the root directory of the site
<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <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 % n record time: % date % n thread ID: [% thread] % n log level: %-5 level % n error class: % logger property: [% property {NDC}]-% n error Description: % message % newline % n "/> --> <conversionPattern value =" % newline % nDate: % date % nThread: [% thread] % nLevel: %-5 level % nClass: % logger [% property {NDC}] % nMessage: % message % newline "/> </layout> </appender> <root> <level value =" ALL "/> <! -- Log in the file format --> <appender-ref = "RollingLogFileAppender"/> </root> </log4net> </configuration>
3. Configure xmlConfigurationAdd [assembly: log4net. Config. XmlConfigurator (ConfigFile = "log4net. config", ConfigFileExtension = "config", Watch = true)] to AssemblyInfo under site properties.
Fourth, write test in the console as work
public class HomeController : Controller { log4net.ILog log = log4net.LogManager.GetLogger(typeof(HomeController)); public ActionResult Index() { log.Info("test...."); return View(); }}