1. Quoting Log4net.dll
2. Add log4.config files under the project root directory
<?xml version="1.0"?><configuration> <configSections> <!--log4net configuration-<section name="log4net"Type="log4net. Config.log4netconfigurationsectionhandler,log4net"/> </configSections> <log4net debug="true"> <appender name="Rollinglogfileappender"Type="log4net. Appender.rollingfileappender"> <file value="Test.txt"/> <!--file You can specify a specific path Eg:d:\\test.log. If not specified, log is generated in the project's Bin/debug or Bin/release directory (the Web project is generated by default in the root directory)--<appendtofile value="true"/> <rollingstyle value="Size"/> <maxsizerollbackups value="Ten"/> <!--backup log files up to 10--<maximumfilesize value="2MB"/> <!--a maximum of 2M per log file, if more than 2M will recreate a new log file and back up the original log file. -<staticlogfilename value="true"/> <layout type="log4net. Layout.patternlayout"> <!--Specify log format--<conversionpattern value="[%date]%thread--%-5level--%logger [%M]--%message%newline"/> </layout> </appender> <root> <level value="DEBUG"/> <!--Specifies that logs of this level and above be printed to the log file--<appender-ref ref="Rollinglogfileappender"/> </root> </log4net></configuration>
3. Under AssemblyInfo.cs, add the following:
@" Log4net.config " true)]
4. Add the following code under Application_Start ():
Log4net. Config.XmlConfigurator.Configure (new FileInfo (Server.MapPath ("~/log4net.config" ))); // Read the log4net configuration file
5. Call directly in the project:
ILog log = Logmanager.getlogger ("errormessage"); Log. Error (Exception. ToString ());
MVC log4net Configuration