Asp. NET, use Apache Log4net

Source: Internet
Author: User
Tags config configuration settings log reference log4net
Apache|asp.net

Log4net is the Apache log4j. NET version, is a set of XML configuration log engine, just recently a program needs a log system, in order to facilitate the integration, decided to study. Thank Wei Wei for your recommendation.

The log4net system divides 7 log levels, if set to "WARN", then Debug,info is not recorded. As follows:

1. All

2. DEBUG

3. INFO

4. WARN

5. ERROR

6. FATAL

7. Off

Creating the Sample Application

Step 1: Download log4net

Go to Logging.apache.org/log4net, to get their latest version, in the example I used is incubating-log4net-1.2.9-beta.zip, look forward to the official version.

Step 2: Create a asp.net Web application

The creation of this is not much to say, in Log4net-1.2.0-beta8\bin\net as pictured. Locate the corresponding DLL and add it to the reference. Some versions do not compile a good DLL, then do it yourself, with the version of the. NET V1.0 converted to 1.1 or 2.0. I think this is not a work error, it is intentional. Many of the people they used to play Java were like this.

Configuring the Sample Application

Step 3: Add assembly information

Open the AssemblyInfo.cs file and add:

[Assembly:log4net. Config.domconfigurator ()]
The log4net system automatically looks for the configuration file Web.config to obtain and load the configuration information. If you want to log4net the configuration file to reload at any time
A little bit of trouble to write this:


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

Step 4: Add configuration information

To edit the Web.config file, it's a surprise to see it every time. Find the configuration label, configsections must write next to it under it (not at all for others to consider, to be in the same as his overbearing how to do? So just write a log profile of it! I reckon that's what they think about Apache). Let's take a look at the example:


<?xml version= "1.0" encoding= "Utf-8"?>

<configuration>

<configSections>

<section name= "Log4net"

Type= "Log4net. Config.log4netconfigurationsectionhandler, log4net-net-1.0 "

/>

</configSections>

<!--This section contains the Log4net configuration settings-->

<log4net>

<!--Define some output appenders-->

<appender name= "Consoleappender" type= "log4net". Appender.consoleappender ">

<layout type= "log4net. Layout.patternlayout ">

<param name= "Conversionpattern" value= "%d [%t]%-5p%c [%x] <%X{auth}>-%m%n"/>

</layout>

</appender>

<!--Rollingfileappender looks after rolling over files by size or date-->

<appender name= "Rollingfileappender" type= "log4net". Appender.rollingfileappender ">

<param name= "File" value= "C:\\log\\rollingloghelloworld.log"/>

<param name= "Appendtofile" value= "true"/>

<param name= "maxsizerollbackups" value= "ten"/>

<param name= "maximumFileSize" value= "1000"/>

<param name= "Rollingstyle" value= "Size"/>

<param name= "Staticlogfilename" value= "true"/>

<layout type= "log4net. Layout.patternlayout ">

<param name= "Conversionpattern" value= "%d [%t]%-5p%-45c [%x]-%m%n"/>

</layout>

</appender>

<!--Fileappender appends to a log and it are manually managed or size-->

<appender name= "Fileappender" type= "log4net". Appender.fileappender ">

<param name= "File" value= "LogHelloWorld.log"/>

<!--Example using environment variables in params-->

<!--<param name= "File" value= "${tmp}\\applicationkit.log"/>-->

<param name= "Appendtofile" value= "true"/>

<layout type= "log4net. Layout.patternlayout ">

<param name= "Conversionpattern" value= "%d [%t]%-5p%c [%x]-%m%n"/>

</layout>

</appender>

<!--Setup The root category, add the Appenders and set the default level-->

<root>

<level value= "INFO"/>

<appender-ref ref= "Consoleappender"/>

</root>

<!--applicationkit category-the presentation UI-->

<logger name= "WebForm1" >

<level value= "INFO"/>

<appender-ref ref= "Fileappender"/>

</logger>

</log4net>

</configuration>

Note that the Name property of the logger node is set to WebForm1, which refers to the class name. To add one such node to each class that needs to add log. Think of the osworkflow work flow, flexible and convenient and practical often can not be both. The level here is info, which means that the info below will not be recorded.

Running the Sample application

Step 5: Add a Log class

The first is to add a reference

Using Log4net;using log4net. Config; Then it's instantiated. If the configuration file is carefully studied, then this declarative approach is well understood: private static readonly ILog log = Logmanager.getlogger ("WebForm1");


Step 6: Using the Log Object

Now that all the configuration work has been done, the next task is to test it and connect him to the application layer. Just add a button in response to the event button1_click events handler.


Log. Info ("Hello World, I am a logger");


Step 7:run The Application

Now it can be run, it should be a:

So every time you click the button, it appears in the day to the file.

2006-02-13 10:12:30,671 [1228] INFO WebForm1 []-Hello world, I am a logger such a record.


Summarize:

Simple and easy to use, after a while to study the SQL log, DB only Su Wingdao, hehe.

For more information see http://logging.apache.org/log4net/







Related Article

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.