Use of Log4Net

Source: Internet
Author: User

The use of log4net is not complex. You can do it in three steps:

1. Reference log4net. dll;

2. Create a configuration file log4net. config;

3. Use log4net;

The attachment is a dll file and a config File sample. For more information, see.

In actual use, a class is usually written to initialize log4net and return the log4net instance:

Public static class Log4Net {// <summary> // Log4Net initialization /// </summary> public static void Log4NetInit () {log4net. config. xmlConfigurator. configureAndWatch (new System. IO. fileInfo (System. web. httpContext. current. server. mapPath (".. /Log4Net. config ");} // <summary> // return the log object // </summary> public static log4net. ILog Log () {return log4net. logManager. getLogger ("KangarooLog ");}}

In Global. asax, initialize the Log4Net Configuration:

Void Application_Start (object sender, EventArgs e) {// code that runs when the application is started // initialize Log4Net to configure Log4Net. Log4NetInit ();}

Use on the page:

Log4Net.Log().Error("Error");Log4Net.Log().Warn("Warn");Log4Net.Log().Info("Info");
 
Content of the Log4net. config configuration file:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Log4net>
<Logger name = "KangarooLog">
<! -- Control log level: ALL | DEBUG | INFO | WARN | ERROR | FATAL | OFF -->
<! -- If no level value is defined, the default value is DEBUG -->
<Level value = "ALL"/>
<! -- <Appender-ref = "SmtpAppenderKangaroo"> </appender-ref> -->
<Appender-ref = "FileAppenderKangaroo"> </appender-ref>
</Logger>
<Appender name = "SmtpAppenderKangaroo" type = "log4net. Appender. SmtpAppender">
<To value = "Kangaroo@800best.com"> </to>
<From value = "Kangaroo@800best.com"/>
<Subject value = "Kangaroo Log Message"/>
<SmtpHost value = "smtp.800best.com"/>
<Username value = "Kangaroo"/>
<Password value = "Kangaroo"/>
<BufferSize value = "2048" type = "codeph" text = "/codeph"/>
<! -- Whether to discard the extra-long part -->
<Lossy value = "false"/>
<! -- When evaluator is used, it is good or bad. If it is unreliable, it is still true to use filter. -->
<! -- <Evaluator type = "log4net. Core. LevelEvaluator">
<Threshold value = "ERROR"/>
</Evaluator> -->
<! -- <Evaluator type = "log4net. Core. LevelEvaluator, log4net">
<Threshold value = "WARN"/>
</Evaluator> -->
<! -- Logs cannot be written. -->
<! -- <Filter type = "log4net. Filter. DenyAllFilter"/> -->
<! -- Logs with output levels between definitions -->
<Filter type = "log4net. Filter. LevelRangeFilter">
<Param name = "LevelMin" value = "ALL"/>
<Param name = "LevelMax" value = "OFF"/>
</Filter>
<Layout type = "log4net. Layout. PatternLayout">
<ConversionPattern value = "% newline % date [% thread] %-5 level % logger [% property {NDC}]: % newline % message % newline"/>
</Layout>
</Appender>
 
<Appender name = "FileAppenderKangaroo" type = "log4net. Appender. RollingFileAppender">
<! -- Absolute path -->
<! -- <File value = "D: \ KangarooLog.txt"> </file> -->
<Param name = "File" value = "./Log/KangarooLog. log"/>
<Param name = "AppendToFile" value = "true"/>
<! -- Relative path, under the root directory of the project -->
<! -- Take the last path as the standard, so no logs are written in the absolute path above -->
<! -- <File value = "./Log/Kangaroo.txt"> </file> -->
<! -- Prevent writing logs when multithreading occurs. The official saying is that the thread is not safe -->
<! -- In actual use, the local test is normal and logs cannot be written after deployment -->
<LockingModel type = "log4net. Appender. FileAppender + MinimalLock"/>
<AppendToFile value = "true"/>
<! -- Can be: Once | Size | Date | Composite -->
<! -- Composite is a combination of Size and Date -->
<RollingStyle value = "composite"/>
<! -- The maximum number of logs is the latest -->
<! -- When the rollingStyle node is Date, this node does not work -->
<! -- When the rollingStyle node is set to Size, only values are allowed -->
<! -- When the rollingStyle node is Composite, there is a value log every day -->
<MaxSizeRollBackups value = "10"/>
<! -- When backing up a file, add a suffix to the file name -->
<! -- When the suffix is *. txt, for example, AX.txt _ 2008-07-24.PxP should be a bug in the program -->
<! -- If the suffix is *. TXT, for example, AX.txt _ 2008-07-25.TXT -->
<DatePattern value = "_ yyyy-MM-dd.TXT"/>
<! -- Available unit: KB | MB | GB -->
<! -- Do not use decimal places; otherwise, the current log is always written. -->
<MaximumFileSize value = "2 MB"/>
<! -- Set to true. The current latest log file name is always the name in the file section -->
<StaticLogFileName value = "true"/>
<! -- Logs with the output level between INFO and ERROR -->
<Filter type = "log4net. Filter. LevelRangeFilter">
<Param name = "LevelMin" value = "INFO"/>
<Param name = "LevelMax" value = "ERROR"/>
</Filter>
<! -- Must be used in combination. The first one only filters out the WARN, and the second one rejects other log outputs. -->
<Filter type = "log4net. Filter. LevelMatchFilter">
<Param name = "LevelToMatch" value = "WARN"/>
</Filter>
<Filter type = "log4net. Filter. DenyAllFilter"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
    </layout>
  </appender>
</log4net>
<! -- ====================================== Layout node configuration instructions ========================================= -->
<! -- Made By AX -->
<! -- % M (message): the output Log message, such as ILog. Debug (...) A message output -->
<! -- % N (new line): line feed -->
<! -- % D (datetime): time when the current statement is output -->
<! -- % R (run time): number of milliseconds consumed by the output program from running to executing to the current statement -->
<! -- % T (thread id): ID of the thread where the current statement is located -->
<! -- % P (priority): current priority of the log, that is, DEBUG, INFO, WARN... And so on -->
<! -- % C (class): name of the current log object, for example, -->
<! -- Mode string: %-10c-% m % n -->
<! -- Code: -->
<! -- ILog log = LogManager. GetLogger ("Exam. Log"); -->
<! -- Log. Debug ("Hello"); -->
<! -- The output is in the following format: -->
<! -- Exam. Log-Hello -->
<! -- % 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, if the conversion mode is % r [% t] %-5 p % c-% m % n, PatternLayout generates output similar to the following: -->
<! -- 176 [main] INFO org. foo. Bar-Located nearest gas station. -->

Source: http://www.cnblogs.com/qiangzi/archive/2009/08/07/1541023.html


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.