Also talk about the use of log4net 1.2.9 beta (WEB Project)

Source: Internet
Author: User
Recently, many changes have been made in the project, and log4net has finally been used. Download the latest version 1.2.9, read other people's examples on the Internet, and try to do it. I did not expect that most of the online versions are earlier than 1.2.8, and it seems that from 1.2.9, the configuration and usage are different (dizzy, why am I so unlucky ). I saw someone studying 1.2.9 in the blog Park yesterday. However, his usage is too annoying. I carefully studied my own example. It turns out that it can be configured and operated as before.

First, the configuration file was previously placed in the web. config file, and then switched to a separate XML file. For example, for a project webapp, its configuration file is webapp. dll. log4net (the extension is configurable, as shown below)

<? XML version = "1.0" encoding = "UTF-8"?>
<Log4net DEBUG = "false">
<Appender name = "logfileappender" type = "log4net. appender. fileappender">
<File value = "webapp-log.txt"/>
<Appendtofile value = "true"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% date [% thread] %-5 level % logger [% NDC]-% message % newline"/>
</Layout>
</Appender>
<Appender name = "httptraceappender" type = "log4net. appender. aspnettraceappender">
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% date [% thread] %-5 level % logger [% NDC]-% message % newline"/>
</Layout>
</Appender>
<Appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender">
<File value = "log-data \ rolling-log.txt"/>
<Appendtofile value = "true"/>
<Maxsizerollbackups value = "10"/>
<Maximumfilesize value = "5 MB"/>
<Rollingstyle value = "size"/>
<Staticlogfilename value = "true"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% date [% thread] %-5 level % logger [% NDC]-% message % newline"/>
</Layout>
</Appender>
<Root>
<Level value = "debug"/>
<Appender-ref = "logfileappender"/>
<Appender-ref = "httptraceappender"/>
<! -- <Appender-ref = "rollinglogfileappender"/> -->
</Root>
</Log4net>

I think this is quite good. At least you do not need to configure the section in Web. config:

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

AddCode(The red part is the added code, and the blue part is the configuration before 1.2.8. You can compare it ):

Imports system. Web
Imports system. Web. sessionstate

<Assembly: log4net. config. xmlconfigurator (configfileextension: = "log4net", watch: = true)>

Public class global
Inherits system. Web. httpapplication

Sub application_start (byval sender as object, byval e as eventargs)
'In ApplicationProgramTriggered at startup
'Log4net. config. domconfigurator. Configure ()
End sub

End Class

The above code

<Assembly: log4net. config. xmlconfigurator (configfileextension: = "log4net", watch: = true)> log4net indicates the extension of webapp. dll. log4net in the preceding configuration file.

Finally, you can use it in the project.

.......

Imports log4net

Namespace webapp
Public class webform1
Inherits system. Web. UI. Page

Private shared readonly log as ilog = logmanager. getlogger (GetType (webform1 ))

First implement the ilog interface, and then use the following:

If log. isdebugenabled then log. debug ("txtadd1 = [" & txtadd1.text & "] txtadd2 = [" & txtadd2.text & "]")

It's easy.

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.