The first search was this:
Http://www.codeproject.com/Articles/823247/How-to-use-Apache-log-net-library-with-ASP-NET-MVC
Baidu Word, everywhere is the translation of this article, reproduced to reprint to, really meaningless
But the configuration described here is too cumbersome, and the actual project is more likely to be a directory like this:
It should be more reasonable to add a log4net reference to the tool, and then encapsulate a helper class for out of the box.
Try it out and do it:
1. Adding references
You don't have to explain this.
2. Add a configuration file:
The contents are as follows:
<log4net> <Loggername= "Logerror"> < Levelvalue= "All" /> <Appender-refref= "Errorappender" /> </Logger> <Loggername= "Loginfo"> < Levelvalue= "All" /> <Appender-refref= "Infoappender" /> </Logger> <Appendername= "Errorappender"type= "log4net." Appender.rollingfileappender "> <paramname= "File"value= "Log\\logerror\\" /> <paramname= "Appendtofile"value= "true" /> <paramname= "Maxsizerollbackups"value= "+" /> <paramname= "MaxFileSize"value= "10240" /> <paramname= "Staticlogfilename"value= "false" /> <paramname= "Datepattern"value= "Yyyymmdd"". log" " /> <paramname= "Rollingstyle"value= "Date" /> <Layouttype= "log4net." Layout.patternlayout "> <Conversionpatternvalue= "%date [%thread]%-5level%logger [%NDC]-%message%newline" /> <!--<param name= "Conversionpattern" value= "<hr color=red>%n exception Time:%d [%t] <br>%n exception level:% -5p <br>%n Exception class:%c [%x] <br>%n%m <br>%n "/> - </Layout> </Appender> <Appendername= "Infoappender"type= "log4net." Appender.rollingfileappender "> <paramname= "File"value= "log\\loginfo\\" /> <paramname= "Appendtofile"value= "true" /> <paramname= "MaxFileSize"value= "10240" /> <paramname= "Maxsizerollbackups"value= "+" /> <paramname= "Staticlogfilename"value= "false" /> <paramname= "Datepattern"value= "Yyyymmdd"". log" " /> <paramname= "Rollingstyle"value= "Date" /> <Layouttype= "log4net." Layout.patternlayout "> <Conversionpatternvalue= "%date [%thread]%-5level%logger [%NDC]-%message%newline" /> <!--<param name= "Conversionpattern" value= "<hr color=gray>%n Log Time:%d [%t] <br>% N Log level:%-5p <br>%n Log class:%c [%x] <br>%n%m <br>%n "/> - </Layout> </Appender></log4net>
Conversionpattern is the control output content, if you want the log output HTML file,
Uncomment the Conversionpattern, and then comment out one of the conversionpattern above
Then change the. Log in Datepattern to. htm
3. Create a Help class
The code is as follows:
Public Static classLoghelper {Private Staticlog4net. ILog _loginfo; Private Staticlog4net. ILog _logerror; Private Static BOOL_config =false; Public Static voidConfig () {if(_config)return; varTMP =System.Reflection.Assembly.GetExecutingAssembly (). Location; varCodeBase =System.Reflection.Assembly.GetExecutingAssembly (). CodeBase; CodeBase= Codebase.substring (8, Codebase.lastindexof ("/") -8+1); stringFile = CodeBase +"Log4net.config"; using(varFS =File.Open (File, FileMode.Open)) log4net. Config.XmlConfigurator.Configure (FS); _loginfo= Log4net. Logmanager.getlogger ("Loginfo"); _logerror= Log4net. Logmanager.getlogger ("Logerror"); _config=true; } Public Static voidDebug (Objectinfo) {_loginfo. Debug (info); } Public Static voidInfo (Objectinfo) {_loginfo. info (info); } Public Static voidError (Exception ex) {_logerror. Error (ex. Message, ex); } }
4.WEB Configuration
Add a line of code to global.asax:
5. Results
Run the code again and you will see that the log directory is generated at the root of the Web site.
Using Log4net in MVC5