<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="LogTest2.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>
log类文件
[assembly: log4net.Config.XmlConfigurator(ConfigFile="App.config",Watch = true)]
namespace LogTest
{
public class Logger
{
/// <summary>
/// 输出日志到Log4Net
/// </summary>
/// <param name="t"></param>
/// <param name="ex"></param>
public static void WriteLog(Type t, Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(t);
log.Error("Error", ex);
}
/// <summary>
/// 输出日志到Log4Net
/// </summary>
/// <param name="t"></param>
/// <param name="msg"></param>
public static void WriteLog(Type t, string msg)
{
log4net.ILog log = log4net.LogManager.GetLogger(t);
log.Error(msg);
}
public static void WriterInfoLog(Type t, string msg)
{
log4net.ILog log = log4net.LogManager.GetLogger(t);
log.Info(msg);
}
}
}