Using Log4net in MVC5

Source: Internet
Author: User

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&quot;". log&quot; " />    <paramname= "Rollingstyle"value= "Date" />    <Layouttype= "log4net." Layout.patternlayout ">      <Conversionpatternvalue= "%date [%thread]%-5level%logger [%NDC]-%message%newline" />      <!--<param name= "Conversionpattern" value= "&lt;hr color=red&gt;%n exception Time:%d [%t] &lt;br&gt;%n exception level:% -5p &lt;br&gt;%n Exception class:%c [%x] &lt;br&gt;%n%m &lt;br&gt;%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&quot;". log&quot; " />    <paramname= "Rollingstyle"value= "Date" />    <Layouttype= "log4net." Layout.patternlayout ">      <Conversionpatternvalue= "%date [%thread]%-5level%logger [%NDC]-%message%newline" />      <!--<param name= "Conversionpattern" value= "&lt;hr color=gray&gt;%n Log Time:%d [%t] &lt;br&gt;% N Log level:%-5p &lt;br&gt;%n Log class:%c [%x] &lt;br&gt;%n%m &lt;br&gt;%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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.