Detailed usage of. NET Log4 _ Practical Tips

Source: Internet
Author: User
Tags log4net

I began to contact. NET log, if there is a wrong place also please advise!

Llog4net is used to record the log, you can run the process of information output to some places (files, databases, eventlog, etc.), log is the program's black box, you can view the system through the log of the operation of the process, so as to find the system problems. The role of the log: Record the steps of the run process, the success failure, and record the critical data to analyze the system problem. Log4j.
L Look at the log files, such as Peng Station search. For the website, the exception information can not be displayed to the user, the exception information can only log, out of the problem of the log file to developers, you can know the problem.
L Configure Log4net Environment
• Create a new WebApplication, add an "application configuration file" (app.config)
• Add a reference to Log4net.dll
• Add configuration in web.config (or app.config), see note
• Initialize: Add log4net at the beginning of the program. Config.XmlConfigurator.Configure ();(is the beginning of the program in Global.asax Place Plus)
• Logmanager.getlogger (typeof) where you want to print the log. Debug ("Information");. Pass Logmanager.getlogger the log class name to be recorded to obtain the ilog of this class (so that the log is the output of the class in the log file), and then call the Debug method output message. Because there is more than one place within a class to print the log, Ilog is generally declared as a static field.
• Output error information with the Ilog.error method, the second parameter can pass the exception object. Log. Error ("* * * ERROR" +ex), log. Error ("* * * ERROR", ex)
L

Lappender: Can output the log to different places, different output target corresponding to different appender:rollingfileappender (scrolling file), Adonetappender (database), Smtpappender (mail) and so on.
Llevel (Level): An important level that identifies this log information. None>fatal>error>warn>debug>info>all, set a level, then the log below this level will not be written into the Appender.
Llog4net can also set a number of appender, can be achieved at the same time to log records to files, data, send mail, etc. can be set to different levels of appender, can achieve common level are recorded to file, error above the level of sending mail Can be implemented to different classes to set different Appender, but also can customize the Appender, so you can realize the error message text messages.
L (*) In addition to log4net, there are Enterprise Library logging Application block, Apache Commonlog and Nlog, and so on, are similar.
What is the advantage of using log4net? Do you write your own files differently? Flexible, you can change the code as long as the configuration can do arbitrary control.
L often wrong: the name of the project (essentially the name of the assembly) built Log4net
L
To change the project's "target framework" to not "Client profile" when the console project in VS2010 references log4net
1, log4net Configuration

Copy Code code as follows:

<configuration>
<!--configsections must be placed immediately behind 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 log stored in which file-->
<appendtofile value= "true"/> <!--whether to attach a file-->
<maxsizerollbackups value= "Ten"/> <!--rollback maximum number of capacity bars-->
<maximumfilesize value= "1024KB"/><!--when the 1024k is reached, automatic backup-->
<rollingstyle value= "Size"/>
<staticlogfilename value= "true"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%date [%thread]%-5level%logger-%message%newline"/>
</layout>
</appender>
<root>
<level value= "DEBUG"/><!--severity level score:debug,warn,error-->
<appender-ref ref= "Rollinglogfileappender"/>
</root>
</log4net>

<system.web>
<compilation debug= "false" targetframework= "4.0"/>
</system.web>

</configuration>


2,
Copy Code code 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 entered data errors:" + S, ex);
}

Console.readkey ();
Log. Debug ("program exit");
}

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.