Source: Http://www.codeproject.com/Articles/823247/How-to-use-Apache-log-net-library-with-ASP-NET-MVC
Tracking and monitoring logs is a good way to record a process when running a software program.
Brief introduction:
Tracking and monitoring logs is a good way to record a process when running a software program. Especially when an application goes wrong, the log is the file we need most. Whether it's in Web,winform,windowsphone.
Here we will walk you through how to use the Apache log4net Framework to log logs in an ASP. NET MVC5 application.
I'm using Visual Studio Express 2013 and. NET Framework 4.5.
The first step:
Open visual Studio 2013 Create a new ASP. NET application Select the MVC template.
Step Two:
- In VS solution Explorer, right-click Reference and select Manage NuGet Packages.
- Look for ' log4net ' and install.
If the installation succeeds, we will be able to see below the reference segment such as:
Step Three:
Next we will configure our application to use the log4net. Add the following line of code to the solutionStartup.csfile.
1 " Web. config " true)]
Fourth Step:
Add the following section to the Web. config file:
1 <configsections>2 <!--Add log4net config section -3 < Sectionname= "Log4net"type= "log4net." Config.log4netconfigurationsectionhandler, log4net " />4 </configsections>5 6 <log4netDebug= "true">7 <Appendername= "Rollinglogfileappender"type= "log4net." Appender.rollingfileappender ">8 <filevalue= "Logs\log.txt" />9 <Appendtofilevalue= "true" />Ten <Rollingstylevalue= "Size" /> One <maxsizerollbackupsvalue= "Ten" /> A <maximumFileSizevalue= "10MB" /> - <Staticlogfilenamevalue= "true" /> - <Layouttype= "log4net." Layout.patternlayout "> the <Conversionpatternvalue= "%-5p%d%5rms%-22.22c{1}%-18.18m-%m%n" /> - </Layout> - </Appender> - + <Root> - < Levelvalue= "DEBUG" /> + <Appender-refref= "Rollinglogfileappender" /> A </Root> at </log4net>
Fifth Step:
ModifyGlobal.asax.csThe file adds the following code to theApplication_Start()
Function Inside:
Log4net. Config.XmlConfigurator.Configure (New FileInfo (Server.MapPath ("~/web.config"));
To this, we have completed the configuration.
Sixth step:
In the class where we want to add the log, add the following log declaration:
ReadOnly log4net. ILog logger = log4net. Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);
Seventh Step:
Use logger. The Error () method to add a log to the place where we want the log to be added.
Run the program and we can see the application root directory (the location of the Webconfig file configuration) logs folder is .
That's all thank you!
How to use the Apache Log4net class library to log logs in ASP. MVC5