Log4net configuration (use log4net in the web, put Web.config in a separate file)

Source: Internet
Author: User
Tags auth log4net
Success Example One:

VS2003 established asp.net project, log4net file in Web.config file

1. Add Log4net.dll reference (can be generated by the source itself, does not matter, download DLLs are all OK)

2. Web.config Add (note right side position, under <configuration> label)

<configSections>
<section name= "Log4net"
Type= "Log4net. config.log4netconfigurationsectionhandler,log4net-net-1.0 "
/>
</configSections>


<log4net>
<root>
<level value= "All"/>
<appender-ref 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]%-5p%c [%x] <%x{auth}>%n-%m%n"/>
</layout>
</appender>
</log4net>

3. In the Page_Load of WebForm1.aspx.cs

Log4net. ILog log = log4net. Logmanager.getlogger ("MyLogger");
Log. Debug ("Hello");

4. Read the configuration in the application code (there are 3 ways to do this, and one of the errors is noted)

Correct a: Added to the AssemblyInfo.cs file in the Web project (note right side location, outside the namespace)

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

Correct B: or add Global.asax.cs (location is also outside the namespace)

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

Correct c: or write directly in the WebForm1.aspx.cs of the paging file to log

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

Error: NOTE: Here, a lot of online said that the 4th step can be implemented in the Global.asax Application_Start code,

Error: ***************************

namely: Log4net. Config.XmlConfigurator.Configure (New System.IO.FileInfo ("Web.config"));

Error: ***************************

But I tried, no, I don't know if you can, please tell me, thank you.

5. In the WebForm1.aspx.cs Page_Load of Web projects

Log4net. ILog log = log4net. Logmanager.getlogger ("MyLogger");
Log. Debug ("Hello");

Success Example Two:

VS2005 established asp.net project, log4net file in Web.config file

1. Same as vs2003.

2. Same as vs2003.

3. Same as vs2003.

4. Read the configuration in the application code (there is no AssemblyInfo.cs file here vs2005, so there is a less method than vs2003, and global.asax the foreground page and the background page in the same file, if any location settings in the Global.asax are wrong, [Assembly:log4net. Config.domconfigurator (configfile= "Web.config", Watch=true)], there are only 1 ways, correct c)

5. In the WebForm1.aspx.cs Page_Load of Web projects

Log4net. ILog log = log4net. Logmanager.getlogger ("MyLogger");
Log. Debug ("Hello");

Success Example Three:

VS2005 the ASP.net project, log4net files are placed in a separate configuration file Log4net.config

1. Ditto (Add Reference)

2. Add in the web.config of a Web project (position or under <configuration> tab)

<configSections>
<section name= "log4net" type= "log4net". config.log4netconfigurationsectionhandler,log4net-net-1.0 "/>
</configSections>

3. Setting up a separate log4net.config configuration 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 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]%-5p%c [%x] <%x{auth}>%n-%m%n"/>
</layout>
</appender>
</log4net>
</configuration>
4. Read configuration in application code (ditto, only 1 correct methods, but here Web.config changed to Log4net.config)

Correct c: or write directly in the WebForm1.aspx.cs of the paging file to log

[Assembly:log4net. Config.domconfigurator (configfile= "Log4net.config", Watch=true)]

Here to think about, a project will certainly be in many pages to log, so in every page file to write this and inconvenient, it is not possible, so we use the next program, generally our project will be so designed


5. In the WebForm1.aspx.cs Page_Load of Web projects

Log4net. ILog log = log4net. Logmanager.getlogger ("MyLogger");
Log. Debug ("Hello");

Success Example four:

VS2005 established asp.net project, log4net files in a separate file Log4net.config, plus, put the log records in a separate public project, because often the project will design a public layer, implementation of logging, cache management, encryption, security, etc.

This also avoids the disadvantage of success example three, which can be read only in one place

We add a public project framework project here

That is, [assembly:log4net. Config.domconfigurator (configfile= "Log4net.config", Watch=true)]

1. Ditto (add Reference) now it's OK to add to the framework public project,

2. Add in the web.config of a Web project (position or under <configuration> tab)

<configSections>
<section name= "log4net" type= "log4net". config.log4netconfigurationsectionhandler,log4net-net-1.0 "/>
</configSections>

3. Create a separate log4net.config configuration file (preferably in a Web project for 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 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]%-5p%c [%x] <%x{auth}>%n-%m%n"/>
</layout>
</appender>
</log4net>
</configuration>


4. Reading configuration in application code

[Assembly:log4net. Config.domconfigurator (configfile= "Log4net.config", Watch=true)]

There is only one method, namely:

Add in the AssemblyInfo.cs of the public layer

[Assembly:log4net. Config.domconfigurator (configfile= "Log4net.config", Watch=true)]

5. Add in a separate write log class at the public level

public static void Test ()
{
Log4net. ILog log = log4net. Logmanager.getlogger ("MyLogger");
Log. Debug ("Hello");
}

6. In the WebFom1.aspx.cs of the Web project Page_Load

Framework. Class1.test ();

OK, it's done.

Specific log4net settings, I do not need to say here, a high man in the online translation of the Log4net article is very detailed

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.