Use log4net to record server logs

Source: Internet
Author: User

Some time ago, the project needed to record various operation logs and errors during runtime, and its own implementation was also possible. However, based on time considerations, the open-source log4net was used. Log4net is a sister of log4j and is a very powerful log record assembly.

The usage is as follows:

1. Add log4net DLL reference

2. Add the following content to the <configuration> node in Web. config:

<Configuration>
<Configsections>
<! -- Add log4net reference to configuration options -->
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>
</Configsections>

<Log4net>
<Appender name = "logfile" type = "log4net. appender. rollingfileappender, log4net">
<! -- <Param name = "file" value = "C: \ srvlog.txt"/> define a file to save logs. The generated file log.txt is in the same directory as the EXE file generated by the application. -->
<Param name = "file" value = "serverlog \ srvlog-"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "rollingstyle" value = "date"/>
<Param name = "datepattern" value = "yyyy-mm-DD & quot;. log & quot;"/>
<Param name = "staticlogfilename" value = "false"/>
<Layout type = "log4net. layout. patternlayout, log4net">
<! -- Define the output style -->
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C-% m % N"/>
<Param name = "Header" value = "& #13; & #10; ------------------------ serverlog -------------------------- & #13; & #10;"/>
<Param name = "footer" value = "& #13; & #10; ---------------------------------------------------------- & #13; & #10;"/>
</Layout>
</Appender>
<Appender>
<! -- A configuration file can have many appender, and an appender section is equivalent to a log output media or method. -->
</Appender>
<Logger name = "logapp">
<! -- Define the logger object name as logapp to facilitate use in code. <logger>
You can choose not to configure configuration items -->
<Level value = "all"/> <! -- Define the output information level to all levels, including fatal. Error. Warn. info. debug -->
</Logger>
<Root>
<! -- Define the log output mode and level -->
<Level value = "info"/>
<Appender-ref = "logfile"/> <! -- Select the file output. Note the appender name corresponding to the bold part. -->
</Root>
</Log4net>
</Configuration>

3. Read related configurations

Private Static ilog log = NULL;

Log4net. config. xmlconfigurator. Configure (New fileinfo (appdomain. currentdomain. basedirectory + "Web. config "));
Log = logmanager. getlogger (system. reflection. methodbase. getcurrentmethod (). declaringtype); // reflection is used here

4. Call related function log

Log. Info (content );

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.