Use log4net in ASP. net2.0

Source: Internet
Author: User
Method 1: Write the configuration file in Web. config

Log4net homepage: http://logging.apache.org/log4net/, download log4net
Log4net consists of four components: logger, appender, filter, and layout. Generally, the last three components are configured in the configuration file.
Logger
Generates log messages. Code Calling
Appender
Append logs to the storage media, which is generally configured in the configuration file. There are many appender types based on the storage media, such as adonetappender, eventlogappender, rollingfileappender, etc. See: http://logging.apache.org/log4net/release/config-examples.html.
Filter
Filters logs, which are generally used together with appender and configured in the configuration file.
Layout
It is responsible for the log message format, which is generally used together with appender and configured in the configuration file.
Procedure
1. Add reference log4net. dll to the web Project
2. Add (online) Article Put log4net in front of configsections, but I cannot compile it)

<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>
</Configsections>
<Log4net>
<Root> </root>
<Logger name = "test">
<Level value = "debug"/>
<Appender-ref = "rollingfile"/>
</Logger>
<Appender name = "rollingfile" type = "log4net. appender. rollingfileappender, log4net">
<Param name = "file" type = "" value = "log/"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "rollingstyle" value = "date"/>
<Param name = "datepattern" value = "yyyymmdd"/>
<Param name = "staticlogfilename" value = "false"/>
<Layout type = "log4net. layout. patternlayout, log4net">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C-% m % N"/>
<Param name = "Header" value = "---------------------- header --------------------------"/>
<Param name = "footer" value = "---------------------- footer --------------------------"/>
</Layout>
</Appender>
</Log4net>



The Level Attribute of logger can be set to the following values: Off, fatal, error, warn, info, debug, and all. logs can be written higher than the set value method. off all write methods are not written into logs, and all is the opposite. For example, when we set it to info, logger. debug will be ignored without writing files, but fatal, error, warn, info will be written because their levels are higher than Info;
3. Add

Void application_start (Object sender, eventargs E)
{
// Code that runs on application startup
Log4net. config. xmlconfigurator. Configure ();

}


4. Configure the content in the <log4net> label



<Log4net>
<Root>
<! --
<Level value = "all"/>
<Appender-ref = "rootfile"/>
-->
</Root>
<Logger name = "test">
<Level value = "debug"/>
<Appender-ref = "rollingfile"/>
</Logger>
<Appender name = "rollingfile" type = "log4net. appender. rollingfileappender, log4net">
<Param name = "file" type = "" value = "log/"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "rollingstyle" value = "date"/>
<Param name = "datepattern" value = "yyyymmdd"/>
<Param name = "staticlogfilename" value = "false"/>
<Layout type = "log4net. layout. patternlayout, log4net">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C-% m % N"/>
<Param name = "Header" value = "---------------------- header --------------------------"/>
<Param name = "footer" value = "---------------------- footer --------------------------"/>
</Layout>
</Appender>
</Log4net>



The framework of the log4net label is as follows. The label contains labels such as root, logger, and appender.
Root tag
All logger are inherited from the root, and the root itself is also a logger
Logger tag
Each logger tag represents a logger, and appender-ref indicates the appender to which the log messages generated by the logger are sent. A logger can pass the same messages to multiple appender records.
Appender label
Each appender indicates the storage location of a log. The name cannot be the same as the type.

Type: rollingfileappender
<Appender name = "rollingfile" type = "log4net. appender. rollingfileappender, log4net">
<Param name = "file" value = "log"/> the file name starts with log.
<Param name = "appendtofile" value = "true"/> whether to append logs to the file
<Param name = "rollingstyle" value = "date"/> log scrolling by date
<Param name = "datepattern" value = "yyyymmdd"/> log file name format: log20071120
<Param name = "staticlogfilename" value = "false"/> whether the log file name is fixed

The format of the log message, indicating line feed.
<Layout type = "log4net. layout. patternlayout, log4net">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C-% m % N"/>
<Param name = "Header" value = "---------------------- header --------------------------"/>
<Param name = "footer" value = "---------------------- footer --------------------------"/>
</Layout>
</Appender>

Message Mode
% M (Message): the output Log message, such as ilog. debug (...) Output Message
% N (New Line): line feed
% D (datetime): time when the current statement is output
% R (Run time): Output Program Number of milliseconds consumed from running to execution to the current statement
% T (thread ID): ID of the thread where the current statement is located
% P (priority): Current Log priority level, namely debug, info, warn... And so on
% C (class): name of the current log object
% L: the row number of the output statement
% F: File Name of the output statement
%-Number: indicates the minimum length of the item. If not, fill it with spaces.
5. Call logger in the code


Using system;
Using system. configuration;
Using system. Data;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using log4net;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Ilog logger = logmanager. getlogger ("test ");
Logger. debug ("test information ");
}
}




Finally, you can find the log file in the root directory.
Source code of this example: Download the source code of this Article
Note: For more information about this article, see the blog. I use vs2008, but the class library is. Net framework2.0.
Note: It takes 10 minutes for an instance to complete. I have arranged such a short article for several hours, and it is barely satisfactory at the end, I really don't know how people who have written so many articles stick to it.

(2) Use a separate configuration file instead of in Web. config.
1. Create a website.
2. Add a class library project named lognet

3. Reference log4net. dll for the lognet Project
4. Double-click to open assemblyinfo. CS and add
[Assembly: log4net. config. xmlconfigurator (configfile = "log4net. config ", watch = true)] 5. add a logbll for the lognet project. CS file. We encapsulate the log call to this layer.


Using system;
Using system. Collections. Generic;
Using system. text;
Namespace lognet
{
Public class logbll
{
Public static void debug (string message)
{
Log4net. ilog log = log4net. logmanager. getlogger ("test ");
If (log. isdebugenabled)
{
Log. debug (Message );
}
Log = NULL;
}
Public static void error (string message)
{
Log4net. ilog log = log4net. logmanager. getlogger ("test ");
If (log. iserrorenabled)
{
Log. Error (Message );
}
Log = NULL;
}
Public static void fatal (string message)
{

Log4net. ilog log = log4net. logmanager. getlogger ("test ");
If (log. isfatalenabled)
{
Log. Fatal (Message );
}
Log = NULL;
}
Public static void Info (string message)
{
Log4net. ilog log = log4net. logmanager. getlogger ("test ");
If (log. isinfoenabled)
{
Log. Info (Message );
}
Log = NULL;
}

Public static void warn (string message)
{
Log4net. ilog log = log4net. logmanager. getlogger ("test ");
If (log. iswarnenabled)
{
Log. Warn (Message );
}
Log = NULL;
}
}
}


6. Add a log4net. config file under the root directory of the web site. The content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>
</Configsections>
<Log4net>
<Root>
<! --
<Level value = "all"/>
<Appender-ref = "rootfile"/>
-->
</Root>
<Logger name = "test">
<Level value = "debug"/>
<Appender-ref = "rollingfile"/>
</Logger>
<Appender name = "rollingfile" type = "log4net. appender. rollingfileappender, log4net">
<Param name = "file" type = "" value = "log/"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "rollingstyle" value = "date"/>
<Param name = "datepattern" value = "yyyymmdd"/>
<Param name = "staticlogfilename" value = "false"/>
<Layout type = "log4net. layout. patternlayout, log4net">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C-% m % N"/>
<Param name = "Header" value = "---------------------- header --------------"/>
<Param name = "footer" value = "---------------------- footer ------------"/>
</Layout>
</Appender>
</Log4net>
</Configuration>
7. Compile the lognet Project
8. Introduce lognet. dll under the bin directory of the lognet project to the Web
9. Write the following code in default. aspx. CS:
Using system;
Using system. configuration;
Using system. Data;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using lognet; // reference an assembly
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Logbll. debug ("test information ");
}
}
}

10. Run the command to view the log file in the log directory under the web directory.
---------------------- Header --------------------------
12:16:24, 171 [4] Debug livebookings-test information
12:16:46, 312 [4] Debug livebookings-test information
---------------------- Footer --------------------------
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.