Simple use and configuration of log4net (log4net 1.2.9)

Source: Internet
Author: User
Tags log4net

Log4net

Http://cvs.apache.org/dist/incubator/log4net/1.2.9/incubating-log4net-1.2.9-beta.zip

 

There are a lot of related content on the Internet, and I have explained the same things. I just put them in my personal test step, because although there are many codes on the Internet, there are also many that cannot be successfully implemented.

It may be because many people have not tried it and directly reprinted it, resulting in "deterioration" after the content is reposted several times.

 

Steps:

1. Add the application DLL first. In the net/1.1/release file of the binfile, there is a DLL, which is referenced

2. Create a separate configuration file, such as the file name log4net. config,

The file content is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Log4net DEBUG = "false">
<Logger name = "penavicoxmlogger">
<Level value = "all"/>
<Appender-ref = "rollinglogfileappender"/>
</Logger>
<Root>
<Level value = "all"/>
</Root>
<Appender name = "logfileappender" type = "log4net. appender. fileappender">
<Param name = "file" value = "penavicoxm.log.txt"/>
<Param name = "datepattern" value = "mm-dd hh: mm"/>
<Param name = "appendtofile" value = "true"/>
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C-% m % N"/>
</Layout>
</Appender>
<Appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender">
<File value = "log/penavicoxm"/>
<Appendtofile value = "true"/>
<Rollingstyle value = "date"/>
<Datepattern value = ".yyyymmdd'.txt '"/>
<Staticlogfilename value = "false"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% d [% T] %-5 p % C-% m % N"/>
</Layout>
</Appender>
</Log4net>

 

3. Load the configuration file log4net. config at the place where the program starts to load

Most of the methods on the Internet are

Web:
<1> Assembly info:
[Assembly:
Log4net. config. xmlconfigurator (configfile = "log4net. config", watch = true)]

<2> copy log4net. config to the web directory.
<3> In gloable. asax
Application_start event
Log4net. config. xmlconfigurator. Configure (New
Fileinfo ("log4net. config "));
<4> what logs do you want to record?

You can use applicationlog. writeinfo () writeerror () to record
Winform:

<1> Assembly info:
[Assembly:
Log4net. config. xmlconfigurator (configfile = "log4net. config ",
Watch = true)]
<2> entry functions
[Stathread]
Private Static
Void main ()
{
Log4net. config. xmlconfigurator. Configure (New
Fileinfo ("log4net. config "));
}

<4> copy log4net. config to the debug/release directory of bin.

Or modify the properties of the target project.
Project properties-general properties-Pre-generated event command line
Enter copy
$ (Projectdir) log4net. CONFIG $ (targetdir) log4net. config

 

After you try it, you can do it as follows. Note: If you search for the path on the internet, you will find that many people use the relative path. It may be feasible in most cases, but I have tried it for a long time today, after finding the path for a long time, I found that the absolute path is used, but it is not written to death. Server. mappath ()

System. Io. fileinfo fin = new system. Io. fileinfo (server. mappath (".") + "/log4net. config ");
Log4net. config. xmlconfigurator. Configure (FIN );

 

After the above method is successful, you can write simple code for testing.

Public class Loger <br/>{< br/> static ilog = NULL; </P> <p> Public static void createloger () <br/>{< br/> If (ilog = NULL) <br/> ilog = log4net. logmanager. exists ("penavicoxmlogger"); </P> <p> // If (ilog. isdebugenabled) <br/> // ilog. debug ("test", new exception ("test1ssss22 ")); <br/>}< br/> /// <summary> <br/> // bug log output <br/> /// </Summary> <br/> /// <Param name = "titlname"> title </param> <br/> /// <Param name = "exceptionmsg"> exception information </param> <br /> Public static void debug (string titlname, string exceptionmsg) {<br/> createloger (); <br/> If (ilog. isdebugenabled) <br/> ilog. debug (titlname, new exception (exceptionmsg )); <br/>}< br/> /// <summary> <br/> // bug log output <br/> /// </Summary> <br/> /// <Param name = "titlname"> Information </param> <br/> Public static void debug (string titlname) <br/>{< br/> createloger (); <br/> If (ilog. isdebugenabled) <br/> ilog. debug (titlname); <br/>}< br/> // call where an exception may occur and record the log <br/> Loger. debug ("2222", "exception"); <br/>

 

If the test is successful, a penavicoxm.xxxxxx.txt log file is under the logfile of the project target.

 

 

Transfer Record exception case column:

The configuration of the global. asax. CS file is as follows:
Protected void application_start (Object sender, eventargs E)
{
Systemlog. setconfig ();
}
Protected void application_error (Object sender, eventargs E)
{
Exception objexp = httpcontext. Current. server. getlasterror ();
Loghelper. writelog ("/R/n Client IP:" + request. userhostaddress + "/R/n error address:" + request. URL + "/R/n exception information:" + server. getlasterror (). message, objexp );
}
Protected void application_start (Object sender, eventargs E)
{
Systemlog. setconfig ();
}
Protected void application_error (Object sender, eventargs E)
{
Exception objexp = httpcontext. Current. server. getlasterror ();
Loghelper. writelog ("/R/n Client IP:" + request. userhostaddress + "/R/n error address:" + request. URL + "/R/n exception information:" + server. getlasterror (). message, objexp );
}

Eg:
Try
{}
Catch (exception ex)
{
Loghelper. writelog ("errorinfo" EX );
}

 

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.