Detailed usage of. net log4

Source: Internet
Author: User
Tags log4net

I started to access. net logs. Please give me more advice if there is anything wrong!

LLog4Net is used to record logs. It can output information during the program running to some places (files, databases, EventLog, etc.). logs are the program's black box, you can view the running process of the system through Logs to discover system problems. Log function: records the running steps and successes and failures, and records key data to analyze system problems. Log4J.
L look at the log files searched in the Peng station. For websites, the exception information cannot be displayed to users. The exception information can only be recorded in logs. If a problem occurs, send the log file to the developer to know the problem.
L configure the Log4Net Environment
• Create a New WebApplication and add an "Application configuration file" (App. config)
• Add a reference to log4net. dll
• Add configuration in Web. Config (or App. Config). For more information, see the remarks.
• Initialization: Add log4net. Config. XmlConfigurator. Configure () at the beginning of the program)
• LogManager. GetLogger (typeof (Program). Debug ("info ");. Use LogManager. getLogger transmits the log class name to be recorded to obtain the ILog of this class (so that the log file can see which class is output), and then calls the Debug Method to output the message. Because logs must be printed in more than one place within a class, ILog is generally declared as a static field.
• The ILog. Error method is used to output Error messages. The second parameter can pass Exception objects. Log. Error ("*** Error" + ex), log. Error ("*** Error", ex)
L

LAppender: logs can be output to different places. Different output targets correspond to different Appender: RollingFileAppender (rolling file), AdoNetAppender (database), SmtpAppender (mail), and so on.
Llevel: An Important level that identifies the log information. None> Fatal> ERROR> WARN> DEBUG> INFO> ALL. If a Level is set, logs lower than this Level will not be written to the Appender.
LLog4Net can also set multiple Appender to simultaneously record logs to files, Data, send emails, and so on; it can set different levels for different Appender, you can record files at the common level, send emails at the Error level or above, set different Appender for different classes, and customize Appender, in this way, you can send text messages about Error messages.
L (*) in addition to Log4Net, there are also Logging Application Block in the Enterprise Library, CommonLog of Apache, and NLog.
L what are the advantages of using Log4Net? Are you sure you want to write files by yourself? Flexible, you can do any control without changing the code as long as you change the configuration.
L common error: build the project name (essentially the Assembly name) into log4net
L
When the console project in VS2010 references Log4Net, change the "target framework" of the project to a non-"Client Profile"
1. Log4Net Configuration
Copy codeThe Code is as follows:
<Configuration>
<! -- Configsections must be followed by configuration. -->
<ConfigSections>
<Section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/>
</ConfigSections>
<Log4net>
<! -- Define some output appenders -->
<Appender name = "RollingLogFileAppender" type = "log4net. Appender. RollingFileAppender">
<File value = "test.txt"/> <! -- This is the file where logs are stored -->
<AppendToFile value = "true"/> <! -- Whether to attach a file -->
<MaxSizeRollBackups value = "10"/> <! -- Maximum number of rollback capacity -->
<MaximumFileSize value = "1024KB"/> <! -- When 1024 KB is reached, automatic backup is performed. -->
<RollingStyle value = "Size"/>
<StaticLogFileName value = "true"/>
<Layout type = "log4net. Layout. PatternLayout">
<ConversionPattern value = "% date [% thread] %-5 level % logger-% message % newline"/>
</Layout>
</Appender>
<Root>
<Level value = "DEBUG"/> <! -- Severity Score: debug, warn, error -->
<Appender-ref = "RollingLogFileAppender"/>
</Root>
</Log4net>

<System. web>
<Compilation debug = "false" targetFramework = "4.0"/>
</System. web>

</Configuration>

2,
Copy codeThe Code is as follows:
Class Program
{
Private static ILog log = LogManager. GetLogger (typeof (Program ));

Static void Main (string [] args)
{
Log4net. Config. XmlConfigurator. Configure ();

Log. Debug ("start running ");
String s = Console. ReadLine ();
Log. Debug ("user input:" + s );
Try
{
Int I = Convert. ToInt32 (s );
}
Catch (Exception ex)
{
Log. Error ("user input data Error:" + s, ex );
}

Console. ReadKey ();
Log. Debug ("Exit program ");
}

Related Article

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.