log4Net | 使用步骤
根据网上资料,测试log4net.仅以下步骤成功:
(曾记得log4j可以记录输出到控制台的信息,4net只能调用Ilog的方法记录信息吗?)
(web.config中配置的方式失败 需进一步研究...)
1.添加引用log4net.dll
2. 在文件AssemblyInfo.cs下面加入
[assembly:log4net.Config.XmlConfigurator(ConfigFile="log4net.config",Watch=true)]
其中是ConfigFile="log4net.config"的web.config是你的配置文件名称,
3.在文件Global.asax中
protected void Application_Start(Objectsender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
4.创建log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.0" />
</configSections>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log/huggies.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<encoding value="unicodeFFFE" />
<lockingModel type="log4net.Appender.RollingFileAppender+MinimalLock" />
<!--
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
5. 代码文件
log4net.ILog log =log4net.LogManager.GetLogger("AA"); //log.Logger.Name
log.Debug("Log4Net");