Log4net the fastest experience

Source: Internet
Author: User
Tags log4net

This article is for the practical driver to quickly get started log4net the most basic functions, a total of 4 steps, 3 minutes to fix.

One, add Log4net.dll reference, can use NuGet installation or direct reference file

Second, add the configuration

Add the following configuration in App. Config or Web. config. Note <configsections> must be placed at the top.

1 <Configuration>2   <configsections>3       < Sectionname= "Log4net"type= "log4net." Config.log4netconfigurationsectionhandler, log4net " />4   </configsections>5   <log4net>6     <!--defining the output to a file -7     <Appendername= "Rollinglogfileappender"type= "log4net." Appender.rollingfileappender ">8       <!--define where files are stored -9       <filevalue= "Logs\"/>Ten       <Appendtofilevalue= "true"/> One       <Rollingstylevalue= "Date"/> A       <Datepatternvalue= "' log-' yyyyMMdd '. txt '"/> -       <Staticlogfilenamevalue= "false"/> -       <Layouttype= "log4net." Layout.patternlayout "> the         <!--output Format - -        <Conversionpatternvalue= "%ndatetime:%d%nthread:%t%npriority:%p%nmessage:%m%n"/> -       </Layout> -     </Appender> +     <Root> -       <!--record log as file Form - +       <Appender-refref= "Rollinglogfileappender"/> A     </Root> at   </log4net> - </Configuration>

Iii. Project AssemblyInfo.cs Add the following configuration to initialize the log4net configuration file. This step must not be missed, do not miss, do not miss out.

[assembly:log4net. Config. xmlconfigurator (Watch = true)]

Iv. using the Log function

1 usinglog4net;2 3 namespaceSample4 {5     classlogtest6     {7         StaticILog logger = Logmanager.getlogger ("LogName");8 9          Public Static voidWriteinfo ()Ten         { OneLogger. Info ("Write log Test"); A         } -     } -}

Log4net the fastest experience

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.