介绍
Log4net是一个开源的组件.可以使.net程序记录日志并输出成各种格式.
使用代码
Log4net可以提供简便的方式去使用强大的日志功能.步骤如下:
1. 获取最新版本的Log4net组件并添加引用到程序
提供一个版本下载:http://files.cnblogs.com/xugang/log4net.rar
2. 增加以后行到你的AssemblyInfo.cs文件
[assembly: log4net.Config.XmlConfigurator(ConfigFile=
"
Web.config
",Watch=
true)]
//
For log4net 1.2.10.0
上面的语句提供了配置log4net参数的配置文件
3. 添加以下节点到web.config
<
configSections
>
< section name ="log4net" type ="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</ configSections >
< log4net debug ="true" >
< appender name ="RollingLogFileAppender" type ="log4net.Appender.RollingFileAppender" >
< file value ="C:\\TestProj\\TestLog.txt" />
< appendToFile value ="true" />
< rollingStyle value ="Size" />
< maxSizeRollBackups value ="10" />
< maximumFileSize value ="10MB" />
< staticLogFileName value ="true" />
< layout type ="log4net.Layout.PatternLayout" >
< conversionPattern value ="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</ layout >
</ appender >
< root >
< level value ="DEBUG" />
< appender-ref ref ="RollingLogFileAppender" />
</ root >
</ log4net >
< section name ="log4net" type ="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</ configSections >
< log4net debug ="true" >
< appender name ="RollingLogFileAppender" type ="log4net.Appender.RollingFileAppender" >
< file value ="C:\\TestProj\\TestLog.txt" />
< appendToFile value ="true" />
< rollingStyle value ="Size" />
< maxSizeRollBackups value ="10" />
< maximumFileSize value ="10MB" />
< staticLogFileName value ="true" />
< layout type ="log4net.Layout.PatternLayout" >
< conversionPattern value ="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</ layout >
</ appender >
< root >
< level value ="DEBUG" />
< appender-ref ref ="RollingLogFileAppender" />
</ root >
</ log4net >
上面的节点定义了配置记录日志的参数
RollingLogFileAppender描述记录日志的方式,这代表日志将被写在一个文件,该文件当满的话会自动增加.
这里有其他可用的途径来保存日志.
layout负责格式化日志请求,然而保存日志途径的定义和日志输出的格式有关.
上边layout输出的格式如下:
2006-07-14 20:26:04,033 [1736] ERROR Utility [PayStub] - Could not find a part of the path
"c:\inetpub\wwwroot\TestProj\Template\PayStub.xml"
"c:\inetpub\wwwroot\TestProj\Template\PayStub.xml"
4. 如果你想要log4net增加自己的诊断信息,须在web.config文件中加入以下代码
<
appSettings
>
< add key ="log4net.Internal.Debug" value ="true" />
</ appSettings >
在system.web节点下如以下代码:
< system.diagnostics >
< trace autoflush ="true" >
< listeners >
< add name ="textWriterTraceListener"
type ="System.Diagnostics.TextWriterTraceListener"
initializeData ="C:\\TestProj\\TestProjlog4net.txt" />
</ listeners >
</ trace >
</ system.diagnostics >
< add key ="log4net.Internal.Debug" value ="true" />
</ appSettings >
在system.web节点下如以下代码:
< system.diagnostics >
< trace autoflush ="true" >
< listeners >
< add name ="textWriterTraceListener"
type ="System.Diagnostics.TextWriterTraceListener"
initializeData ="C:\\TestProj\\TestProjlog4net.txt" />
</ listeners >
</ trace >
</ system.diagnostics >
5. 在代码页面进行以下步骤:
a. 添加名字空间
using log4net;
b. 添加有类定义的声明
private
static
readonly ILog log = LogManager.GetLogger(
typeof(TestPage1).Name);
或者
private
static
readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
6. 现在你可以用下面的语句记录日志了
if (log.IsErrorEnabled)
{
log.Error( " Page Load failed : " + ex.Message);
}
{
log.Error( " Page Load failed : " + ex.Message);
}
或者
if (log.IsDebugEnabled)
{
log.Debug( " Application loaded successfully. ");
}
{
log.Debug( " Application loaded successfully. ");
}
这里只是简单介绍了log4net的使用,但log4net还有很多特性可用.
来源: http://blog.csdn.net/jecray/archive/2007/04/06/1554673.aspx
相关文章:
Log4Net使用指南
日志记录组件Log4net详细介绍
本文转自钢钢博客园博客,原文链接:http://www.cnblogs.com/xugang/archive/2008/04/09/1145384.html,如需转载请自行联系原作者