1. 使用
a) 下载地址: http://logging.apache.org/log4net/downloads.html
下载下来的解决方案编译后的DLL文件添加引用到自己项目中
b) 在项目AssemblyInfo.cs文件中添加如下内容
其中ConfigFile="Web.config" 表示log4net的配置信息在web.config文件当中
[assembly: log4net.Config.XmlConfigurator(ConfigFile="Web.config",Watch=true)]
c) 在web.config文件当中添加如下内容
代码
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="c:\Log\Log.txt" />
<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="Log\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>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="c:\Log\Log.txt" />
<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="Log\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>
d) 使用示例代码如下
代码
using System;
using System.Configuration;
using System.IO;
using log4net;
using log4net.Config;
namespace Log4NetTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class MainTest
{
private static ILog logger = LogManager.GetLogger(typeof(MainTest));
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
logger.Info("Start application ");
logger.Debug("Debug Ilog");
int a = 0;
int b = 10 / a;
}
catch (Exception e)
{
logger.Info (e.StackTrace, e);
}
}
}
using System.Configuration;
using System.IO;
using log4net;
using log4net.Config;
namespace Log4NetTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class MainTest
{
private static ILog logger = LogManager.GetLogger(typeof(MainTest));
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
logger.Info("Start application ");
logger.Debug("Debug Ilog");
int a = 0;
int b = 10 / a;
}
catch (Exception e)
{
logger.Info (e.StackTrace, e);
}
}
}
以上代码的原文地址是 http://www.cnblogs.com/adam/archive/2007/07/31/837978.html
2 注意事项
a) Log4net支持多种级别的日志。优先级从高到低依次排列如下:
FATAL > ERROR > WARN > INFO > DEBUG
其中 的config文件的
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
中配置了 debug级别,由于debug是最低级别所以在项目当中所有日志都会被记录下来,若是WARN 则log.info(“”),log.debug(“”)信息则不会被输出
b) Log4ne支持4种输出形式,如上配置是将以文件形式输出到c:\log\log.txt文件当中
俱体的配置参照如上配置的 <appender >配置结