log4net 测试实例
log4net配置可以分为以下几个部分:
1、配置App.config文件,在<configuration>节点下加入
另外在<configuration>节点下还要加入一个节点
2、将log4net.dll 文件引用到工程中,并在AssemblyInfo.cs文件中加入配置项
[assembly: log4net.Config.XmlConfiguratorAttribute(Watch=true)]
3、应用log4net,在操作类中使用log4net
log4net测试实例.rar
1、配置App.config文件,在<configuration>节点下加入
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
另外在<configuration>节点下还要加入一个节点
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="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>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="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>
2、将log4net.dll 文件引用到工程中,并在AssemblyInfo.cs文件中加入配置项
[assembly: log4net.Config.XmlConfiguratorAttribute(Watch=true)]
3、应用log4net,在操作类中使用log4net
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);
}
}
}
}
log4net测试实例.rar