Log4net configuration (four different cases)

Source: Internet
Author: User
Tags log4net
Technorati label: log4net configuration, independent log4net. config, non-embedded

Example 1:

Vs2003The created Asp.net project, the log4net file is placed in the web. config file

1. Add a reference to log4net. dll (download DLL)

2. Add in Web. config (Note the location of the peer, under the <configuration> label)

<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net-net-1.0"/>
</Configsections>

<Log4net>
<Root>
<Level value = "all"/>
<Appender-ref = "logfileappender"/>
</Root>

<Appender name = "logfileappender" type = "log4net. appender. fileappender">
<Param name = "file" value = "log-file.txt"/>
<Param name = "appendtofile" value = "true"/>
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C [% x] & lt; % x {auth} & gt; % N-% m % N "/>
</Layout>
</Appender>
</Log4net>

3. Page_load in webform1.aspx. CS

Log4net. ilog logger = log4net. logmanager. getlogger (system. reflection. methodbase. getcurrentmethod (). declaringtype );
Logger. debug ("hello ");

4. In the ApplicationProgramCodeRead Configuration

Correct A: add it to the assemblyinfo. CS file in the WEB Project (note that the correct location is placed outside the namespace)

[Assembly: log4net. config. domconfigurator (configfile = "Web. config", watch = true)]

Correct B: or add it to global. asax. CS (the location is outside the namespace)

[Assembly: log4net. config. domconfigurator (configfile = "Web. config", watch = true)]

Correct C: Or write it directly in the webform1.aspx. CS page file to record the log

[Assembly: log4net. config. domconfigurator (configfile = "Web. config", watch = true)]

Correct D: it can be implemented using code in application_start in global. asax, but it cannot be implemented if it is an independent log4net. config file.

That is, log4net. config. xmlconfigurator. Configure ();

5. In webform1.aspx. CS page_load of the web Project

Log4net. ilog logger = log4net. logmanager. getlogger (system. reflection. methodbase. getcurrentmethod (). declaringtype );
Logger. debug ("hello ");

Example 2:

In the Asp.net project created in vs2005, The log4net file is stored in the web. config file.

1. Like vs2003, add log4net. dll reference (download DLL)

2. Like vs2003, add in Web. config (note the correct location under the <configuration> label)

3. Same as vs2003

4. Read the configuration in the application code (vs2005 does not have the assemblyinfo. CS file,

You can use the code in application_start in global. asax, that is, log4net. config. xmlconfigurator. Configure ();

Or write it directly in the webform1.aspx. CS page file to record logs.

[Assembly: log4net. config. domconfigurator (configfile = "Web. config", watch = true)]

5. In webform1.aspx. CS page_load of the web Project

Log4net. ilog logger = log4net. logmanager. getlogger (system. reflection. methodbase. getcurrentmethod (). declaringtype );
Logger. debug ("hello ");

 

 

Example 3:

The Asp.net project created in vs2005, where the log4net file is storedLog4net. config in a separate configuration file

1. Same as above (add reference)

2. In the WEB ProjectWeb. config(The location is still under the <configuration> label)

<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net-net-1.0"/>
</Configsections>

3. Create a separateLog4net. configConfiguration File

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net-net-1.0"/>
</Configsections>

<Log4net>
<Root>
<Level value = "all"/>
<Appender-ref = "logfileappender"/>
</Root>

<Appender name = "logfileappender" type = "log4net. appender. fileappender">
<Param name = "file" value = "log-file.txt"/>
<Param name = "appendtofile" value = "true"/>
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C [% x] & lt; % x {auth} & gt; % N-% m % N "/>
</Layout>
</Appender>
</Log4net>
</Configuration>
4. Read the configuration in the application code (same as above, there is only one correct method, but here web. config is changed to log4net. config)

Correct: Write it directly in the webform1.aspx. CS file on the page where logs are to be recorded (note that the correct location is placed outside the namespace)

[Assembly: log4net. config. domconfigurator (configfile = "log4net. config", watch = true)]

5. In webform1.aspx. CS page_load of the web Project

Log4net. ilog logger = log4net. logmanager. getlogger (system. reflection. methodbase. getcurrentmethod (). declaringtype );
Logger. debug ("hello ");

 

Example 4:

The Asp.net project created in vs2005, where the log4net file is storedLog4net. config in a separate file, Plus,Place log records in a separate public projectBecause projects often design a public layer to implement logging, Cache Management, encryption, security, and so on.

1. Add a public class library classlib project and add it to the assemblyinfo. CS file under properties:

That is, [Assembly: log4net. config. xmlconfigurator (configfile = "log4net. config", watch = true)]

2. Same as above (add reference). In this case, it is OK to add it to the public class library classlib project,

3. Create a separateLog4net. configConfiguration file (preferablyIn a WEB ProjectFor later modification)

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net-net-1.0"/>
</Configsections>

<Log4net>
<Root>
<Level value = "all"/>
<Appender-ref = "logfileappender"/>
</Root>

<Appender name = "logfileappender" type = "log4net. appender. fileappender">
<Param name = "file" value = "log-file.txt"/>
<Param name = "appendtofile" value = "true"/>
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C [% x] & lt; % x {auth} & gt; % N-% m % N "/>
</Layout>
</Appender>
</Log4net>
</Configuration>

4. Add application_start (Object sender, eventargs E) to the Global. asax code:

Log4net. config. xmlconfigurator. Configure (new system. Io. fileinfo (server. mappath ("~ ") + @" \ Log4net. config "));

5. Add the log class mylog. CS to the public class library classlib project.

Using system;
Using system. Collections. Generic;
Using system. text;
Using log4net;

Namespace classlib
{
Public class mylog
{

Public static void test ()
{
Log4net. ilog log = log4net. logmanager. getlogger ("mylogger ");
Log. debug ("hello ");
}
}

}

6. In page_load of webfom1.aspx. cs of the web Project

Classlib. mylog. Test ();

OK.

The log4net. config configuration for logging to the database is as follows:

<? XML version = "1.0"?>

<Configuration>
<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>
</Configsections>

<Log4net DEBUG = "false">
<Root>
<Level value = "all"/>
<Appender-ref = "adonetappender"/>
<! -- <Appender-ref = "rootfile"/> -->
</Root>
<! -- Define the Loger name and the appender used -->
<Logger name = "*">
<Level value = "all"/>
<Appender-ref = "adonetappender"/>
<! -- <Appender-ref = "rollingfile"/> -->
</Logger>
<Appender name = "adonetappender" type = "log4net. appender. adonetappender">
<! -- Define several items to be written to the database only when two items are stored in the database. No two items are called cache -->
<Buffersize value = "0"/>
<Connectiontype value = "system. Data. sqlclient. sqlconnection, system. Data, version = 1.0.3300.0, culture = neutral, publickeytoken = b77a5c561934e089"/>
<Connectionstring value = "database = database name; server = (local); User ID = sa; Password = 123456"/>
<Commandtext value = "insert into log ([date], [thread], [level], [logger], [Message], [Exception]) values (@ log_date, @ thread, @ log_level, @ logger, @ message, @ exception) "/>
<Parameter>
<Parametername value = "@ log_date"/>
<Dbtype value = "datetime"/>
<Layout type = "log4net. layout. rawtimestamplayout"/>
</Parameter>
<Parameter>
<Parametername value = "@ thread"/>
<Dbtype value = "string"/>
<Size value = "255"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% thread"/>
</Layout>
</Parameter>
<Parameter>
<Parametername value = "@ log_level"/>
<Dbtype value = "string"/>
<Size value = "50"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% level"/>
</Layout>
</Parameter>
<Parameter>
<Parametername value = "@ logger"/>
<Dbtype value = "string"/>
<Size value = "255"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% logger"/>
</Layout>
</Parameter>
<Parameter>
<Parametername value = "@ message"/>
<Dbtype value = "string"/>
<Size value = "4000"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% message"/>
</Layout>
</Parameter>
<Parameter>
<Parametername value = "@ exception"/>
<Dbtype value = "string"/>
<Size value = "2000"/>
<Layout type = "log4net. layout. exceptionlayout"/>
</Parameter>
</Appender>
</Log4net>
</Configuration>

 

The database structure is as follows:

Create Table [DBO]. [log] (
[ID] [int] identity (1, 1) not null,
[Date] [datetime] not null,
[Thread] [varchar] (255) Collate chinese_prc_ci_as not null,
[Level] [varchar] (50) Collate chinese_prc_ci_as not null,
[Logger] [varchar] (255) Collate chinese_prc_ci_as not null,
[Message] [varchar] (4000) Collate chinese_prc_ci_as not null,
[Exception] [varchar] (2000) Collate chinese_prc_ci_as null
) On [primary]

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.