Use the log4net component to record system logs

Source: Internet
Author: User
Log4net is a framework component developed for recording system logs under. net. It is powerful and easy to use.

Usage:

1. Add a reference to log4net. dll in your project.

2. Set the configuration file app. config (you can also set a separate configuration file for log4net. Refer to carhui's log recording using log4net in. Net Program (example ))

Example:
<? XML version = "1.0" encoding = "UTF-8"?>

<Configuration>

<! -- If app. config is not used as the configuration file, the configsections section is not required. -->

<Configsections>

<! -- The complete format of the "type" attribute is: configuration section processor class name, assembly name, version = assembly version number, culture = region information, publickeytoken = Public Key -->

<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>

</Configsections>

<Log4net>

<! -- Logger, which can have multiple -->

<Logger name = "applogger">

<Level value = "info"/>

<Appender-ref = "logfileappender"/>

<Appender-ref = "leleappender"/>

</Logger>

<Logger name = "form1">

<Level value = "debug"/>

<Appender-ref = "logfileappender"/>

</Logger>

<! -- All logger bases and root settings work in all logger.

When you repeatedly set the same appender in the root and logger fields, you will find that the same log information will be recorded twice. -->

<! -- <Root>

<Level value = "Warn"/>

<Appender-ref = "logfileappender"/>

<Appender-ref = "leleappender"/>

</Root> -->

<! -- An appender can be shared by multiple logger. Of course, a logger can specify multiple appender. -->

<Appender name = "logfileappender" type = "log4net. appender. fileappender">

<Param name = "file" value = "app. log"/>

<Param name = "appendtofile" value = "true"/>

<Layout type = "log4net. layout. patternlayout">

<Param name = "Header" value = "[header] \ r \ n"/>

<Param name = "footer" value = "[footer] \ r \ n"/>

<Param name = "conversionpattern" value = "% d [% T] %-5 p % C [% x] % x {auth}-% m % N"/>

</Layout>

<Filter type = "log4net. Filter. levelrangefilter">

<Param name = "levelmin" value = "debug"/>

<Param name = "levelmax" value = "Fatal"/>

</Filter>

</Appender>

<Appender name = "leleappender" type = "log4net. appender. consoleappender">

<Layout type = "log4net. layout. patternlayout">

<Param name = "conversionpattern" value = "% d [% T] %-5 p % C [% x] % x {auth}-% m % N"/>

</Layout>

</Appender>

</Log4net>

</Configuration>

3. Use logger in the code.
// Configure logger according to the configuration file.

Object o = configurationsettings. getconfig ("log4net ");

Log4net. config. domconfigurator. Configure (O as system. xml. xmlelement );


Log4net. ilog logger = log4net. logmanager. getlogger ("applogger ");


// Call logger to record the log information as needed.

Logger. Info ("Start application ");

Some references are packaged into CHM files. Click here to download them.

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.