几乎所有的项目都需要以 log 的形式保留一些系统纪录,实现这类功能的API又以Log4J最为出名。对于.NET平台上,值得庆幸的是,Apache组织已经把Log4J迁移到.NET平台上,从而诞生了:og4j的姐妹-Log4net.下面的文字仅仅作为快速入门的参考,更详细的应用可以访问apache的站点。
1. 首先你应该下载log4net.dll并引入到你的项目References中.
2. 需要修改你的global.asa.cs. 配置application对象启动的时候加载log4net配置. 这一步是不可以缺少的.
protected void Application_Start(Object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
对于Application程式,则在main入口点加载配置。
3. Log4net 的配置应设置于web.config或App.config中,以Web.Config为例在根节点 configuration 中加入如下section:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
4. 该config section 声明了名为 log4net的一个Section,其必须位于web.config根节点 configuration 下。
以下是一个sample:
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="XxxxApplication.log.txt" />
<param name="datePattern" value="MM-dd HH:mm" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="HttpTraceAppender" type="log4net.Appender.ASPNetTraceAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="_LogData\Log.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
5. 上面的配置定义了多个appender. 简单来说, 每一个 appender 都是一个输出管道。
6. root节点指定了选用的 appender. 本例选用了LogFileAppender. (文本文件输出).
在Appender定义中定义了输出的格式. 和目标文本文件所在位置. (起始位置是应用程序根目录. (web.config所在目录).
7. 到目前位置就配置好了log4net. 可以在我们的应用中直接使用了.
8. 以下说明应用方法:
要输出日志, 必须首先得到带有一个别名的logger. 使用以下命令:
log4net.ILog Logger logger = log4net.LogManager.GetLogger(typeof(XXXX));
之后调用
logger.Info(string message);
logger.Error(string message);
logger.Debug(string message);
...
即可输出日志.
分享到:
- 2006-11-06 15:07
- 浏览 5025
- 评论(0)
- 论坛回复 / 浏览 (0 / 4299)
- 查看更多
相关推荐
'1' (number one) key repeatedly during booting and the Radmin driver will not load. ---------------------------------------------------------------------- Limitations ------------------------------...
immediate and easy use by a wide class of users. With this goal, it seems natural to provide the ability to use any remote HOST as if it had been dialed up from a TTY (teletype) terminal. ...
2) output the progress into a logfile 3) fast converting to text files (7 seconds for 18M, 3hours10minutes for 24.5G) 4) output the converted text file into split files. 100 records per file. Easy to ...
EurekaLog 7.3 Hotfix 1 (7.3.1.0), 2-October-2015 1)....Fixed: Long startup time on terminal services servers EurekaLog 7.3 (7.3.0.0), 24-September-2015 1)....Added: RAD Studio 10 Seattle support 2).....
### Big Data Made Easy – A Comprehensive Guide to the Hadoop Ecosystem #### Introduction to Big Data and Hadoop In today's digital era, the volume and variety of data generated by businesses and ...
2. Some of the Diskeeper installation dialogs do not display correctly on computers using the High Contrast Black display setting under the Windows Accessibility Options. Note, however, the ...
3. **Utilize Configuration Files:** Configure Log4j using external configuration files rather than hardcoding settings within the application. This approach provides greater flexibility and makes it ...
#### 1. Unit Testing (Simon Willison) **Introduction:** Unit testing is a crucial aspect of software development that ensures the reliability of individual components or functions within an ...
repository that is zipped each night for easy download. The download page above also includes the OpenSSL binaries needed to support SSL. ICS V8 supports Delphi 64-bit and Mac OS-X projects. Note that...
Page.Request.SaveAs("D:\\SCMwwwroot\\HCBN\\errorLog\\Requests\\" + str_time + ".txt", true); // 关闭文件流 sw1.Close(); sw2.Close(); // 调用基类的OnError方法 base.OnError(e); } ``` 在这段代码...
Typically, the process address space is evenly divided into two 2-GB regions. Each process has access to 2 GB of the 4 GB address space. The upper 2 GB of address space is reserved for the system. ...
This makes it easy to use the tools on machines with limited memory (but enough disk space) as well as computer clusters (from a terminal). In addition, the interactive tools can optionally write the...
1. About jclazz is a crossplatform powerful Java bytecode viewer and decompiler. It supports latest Java versions (from 1.4 till 1.6). It can be used both as command line tool and user application ...
v2.0 Beta 2 (November-1999) : - Main window can now be resized to full screen. - Many bugs killed. Where are the next ?... v2.0 Beta 1 (September-1999) : - Full program conversion into C ...
in Chapter 1 but aren’t used in Chapters 2, 3, and 4. It also introduces another new technique: keeping track of the previous row you processed and the row you’re currently processing, in order to ...
The Simple Tags can be installed in 3 easy steps: 1. Unzip "Simple tags" archive and put all files into a folder like "/wp-content/plugins/simple-tags/" 2. Activate the plugin through the 'Plugins' ...
What is this?...If you have previously installed it as system-wide and now you want it current user only (or vice-versa), it won't remove it, you must do it yourself. (at least for now)
Just as the organization of a telephone directory makes it easy for a person to search, SQL Server quickly searches a table with a clustered index. Because a clustered index determines the sequence ...
Pages in a b+tree are usually implemented as a list or array of child pointers and so while finding and inserting a value is a O(log k) operation the process actually has to move children around in ...
Chapter 3: Scratching the Surface of Zend Framework 2 Chapter 4: The First Request/Response – Building the User Wall Chapter 5: Handling Text Content – Posting Text Chapter 6: Working with Images –...