Configuration and use of the logging tool log4net

Source: Internet
Author: User
Tags log4net

The log component of the project is a must-have and is required in any project. This facilitates the early development test and facilitates project maintenance later in the project. A good log component of a C # project is log4net, and I'll take a rough look at how the program projects log4net in the configuration.

The first step is to install log4net in the project. Open the project, locate the "References" button in the project directory in "Solution Explorer " and click "Manage NuGet packages (N) ..." to pop up a management window, search to log4net click Install.

The second step is to configure the following in the Web. config (or app. config) file:

<name= "log4net"  type= "log4net. Config.log4netconfigurationsectionhandler, log4net "/>
<log4net>    <!--OFF, FATAL, ERROR, WARN, INFO, DEBUG, all -    <!--Set Root Logger level-to-ERROR and its appenders -    <Root>      < Levelvalue= "All" />      <Appender-refref= "Sysappender" />    </Root>    <!--Print only messages of level DEBUG or above in the packages -    <Loggername= "Weblogger">      < Levelvalue= "DEBUG" />    </Logger>    <Appendername= "Sysappender"type= "log4net." Appender.rollingfileappender,log4net ">      <paramname= "File"value= "app_data/" />      <paramname= "Appendtofile"value= "true" />      <paramname= "Rollingstyle"value= "Date" />      <paramname= "Datepattern"value= "&quot; Logs_&quot;yyyymmdd&quot;. txt&quot; " />      <paramname= "Staticlogfilename"value= "false" />      <Layouttype= "log4net." Layout.patternlayout,log4net ">        <paramname= "Conversionpattern"value= "%d [%t]%-5p%c-%m%n" />      </Layout>    </Appender>    <Appendername= "ConsoleApp"type= "log4net." Appender.consoleappender,log4net ">      <Layouttype= "log4net." Layout.patternlayout,log4net ">        <paramname= "Conversionpattern"value= "%d [%t]%-5p%c-%m%n" />      </Layout>    </Appender>  </log4net>

The third step is to add the following code to the Application_Start method in the Global.asax.cs file:

// Configure Log4log4net. Config.XmlConfigurator.Configure (new System.IO.FileInfo (Server.MapPath ("~/web.config ")));

Fourth step, add a line of code to the AssemblyInfo.cs in the project:

// log4net reading the configuration from the configuration file " Config " true)]

The final step is how the project code is applied, and the following is a simple use:

Log4net. ILog log = log4net.  Logmanager. GetLogger (GetType ()); log. info (" This is a log message record "); log. Error (" This is the wrong message ");

The following figure is the log generated in the project:

Keep Learning and growing. *** *****************************

Configuration and use of the logging tool log4net

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.