Log4net Usage Note

Source: Internet
Author: User
  1. Download log4net Apache log4net-download Apache log4net
  2. Modify assemblyinfo. CS
    Add [Assembly: log4net. config. domconfigurator (configfile = "log. log4net", configfileextension = "log4net", watch = true)]
  3. Add your own logger. Log component single-piece mode in the project.
    Public class Logger
    {
    Private Static log4net. ilog logger = log4net. logmanager. getlogger ("all ");
    Public static void log (string message)
    {
    Logger. debug (Message );
    }
    // Add the appropriate log reload for the project here, such as public static void log (string message, exception ex)
    }
  4. The log. log4net file mentioned above first posts my configuration in the project:
    <Log4net>
    <Root>
    <Level value = "all"/>
    <Appender-ref = "rollingfile"/>
    </Root>

     

    <Appender name = "rollingfile" type = "log4net. appender. rollingfileappender, log4net">
    <Param name = "file" type = "" value = "log"/>
    <Param name = "appendtofile" value = "true"/>
    <Param name = "rollingstyle" value = "date"/>
    <Param name = "datepattern" value = "yyyy-mm-DD & quot;. log & quot;"/>
    <Param name = "maxsizetorollbackups" value = "10"/>
    <Param name = "maximumfilesize" value = "5 MB"/>
    <! -- Static and Dynamic log file name -->
    <Param name = "staticlogfilename" value = "false"/>
    <Layout type = "log4net. layout. patternlayout, log4net">
    <Param name = "conversionpattern" value = "% d [% T] %-5 p % C-% m % N"/>
    <Param name = "Header" value = "& #13; & #10; ------------------------ header -------------------------- & #13; & #10;"/>
    <Param name = "footer" value = "& #13; & #10; ------------------------ footer -------------------------- & #13; & #10;"/>
    </Layout>
    </Appender>
    <Appender name = "leleapp" type = "log4net. appender. leleappender, log4net">
    <Layout type = "log4net. layout. patternlayout, log4net">
    <Param name = "conversionpattern" value = "% d [% T] %-5 p % C-% m % N"/>
    </Layout>
    </Appender>
    <Logger name = "log4nettest. logtest">
    <Level value = "debug"/>
    <Appender-ref = "rollingfile"/>
    <Appender-ref = "coloredconsoleapp"/>
    <Appender-ref = "systemevent"/>
    </Logger>
    </Log4net>

I have been using log4net in development projects, but I have never been used to writing notes, what I have done, what I have learned, and what I have found. I will share with you here.

Log4net details the conversion mode (conversionpattern) in log4net. layout. patternlayout)

% M (Message): the output Log message, such as ilog. debug (...) Output Message

% N (New Line): line feed

% D (datetime): time when the current statement is output

% R (Run time): OutputProgramNumber of milliseconds consumed from running to execution to the current statement

% T (thread ID): ID of the thread where the current statement is located

% P (priority): Current Log priority level, namely debug, info, warn... And so on

% C (class): name of the current log object

% L: the row number of the output statement

% F: File Name of the output statement

%-Number: indicates the minimum length of the item. If not, fill it with spaces.

For example, execute logger in patternlayout in the conversion mode of % d [% T] %-5 p % C-% m % N. log ("Hello"); generates outputs similar to the following:

09:55:11, 421 [2720] Debug all-hello.

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.