1.app.config
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<logger name="MyLogo4Net">
<level value="ALL" />
<appender-ref ref="InfoAppender"/>
<appender-ref ref="ErrorAppender" />
</logger>
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Log\LogError\" />
<!--日志记录的存在路径 Log\LogError\-->
<param name="AppendToFile" value="true" />
<!--为true就表示日志会附加到文件,为false,则会重新创建一个新文件-->
<!--<param name="MaxSizeRollBackups" value="100" />-->
<!--创建最大文件数-->
<!--<param name="MaxFileSize" value="10240" />-->
<!--文件大小-->
<param name="StaticLogFileName" value="false" />
<!--是否指定文件名-->
<param name="DatePattern" value="yyyyMMdd'.txt'" />
<!--文件格式-->
<param name="RollingStyle" value="Date" />
<!--创建新文件的方式,可选为Size(按文件大小),Date(按日期),Once(每启动一次创建一个文件),Composite(按日期及文件大小),默认为Composite-->
<layout type="log4net.Layout.PatternLayout">
<!--输出内容布局-->
<param name="ConversionPattern" value="%n异常时间:%d [%t] %n异常级别:%-5p %n异 常 类:%c [%x] %class %method %n%m %n " />
<!--method会影响性能-->
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Log\LogInfo\" />
<param name="AppendToFile" value="true" />
<!--<param name="MaxFileSize" value="10240" />
<param name="MaxSizeRollBackups" value="100" />-->
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd'.txt'" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%n日志时间:%d [%t] %n日志级别:%-5p %n日 志 类:%c [%x] %n%m %n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
</log4net>
2.日志类
/// <summary>
/// LogTools帮助类
/// </summary>
public class LogTools
{
public static readonly log4net.ILog Loginfo = log4net.LogManager.GetLogger("MyLogo4Net"); //选择<logger name="MyLogo4Net">的配置
//public static readonly log4net.ILog Logerror = log4net.LogManager.GetLogger("logerror"); //选择<logger name="logerror">的配置
public static void SetConfig()
{
log4net.Config.XmlConfigurator.Configure();
}
/// <summary>
/// 设置文件路径
/// </summary>
/// <param name="configFile"></param>
public static void SetConfig(FileInfo configFile)
{
log4net.Config.XmlConfigurator.Configure(configFile);
}
/// <summary>
/// 写系统信息日志
/// </summary>
/// <param name="info"></param>
public static void WriteLog(string info)
{
if (Loginfo.IsInfoEnabled)
{
Loginfo.Info(info);
}
}
/// <summary>
/// 写错误日志
/// </summary>
/// <param name="error"></param>
/// <param name="se"></param>
public static void WriteErrorLog(string error, Exception se)
{
if (Loginfo.IsErrorEnabled)
{
Loginfo.Error(error, se);
}
}
/// <summary>
/// 写错误日志
/// </summary>
/// <param name="error"></param>
/// <param name="se"></param>
public static void WriteErrorLog(string error)
{
if (Loginfo.IsErrorEnabled)
{
Loginfo.Error(error);
}
}
}
3.设置Log4Net
程序启动设置LogTools.SetConfig();