1
配置文件中
<configuration>
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <log4net> <!--错误日志配置--> <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\LogError\\"/> <!--文件路径--> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="100"/> <param name="MaxFileSize" value="10240"/> <param name="StaticLogFileName" value="false"/> <param name="DatePattern" value="yyyyMMdd"/> <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>
</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"/> <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></appender>
<!--控制台--> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline"/> </layout> </appender> <!--log4net.LogManager.GetLogger("logerror")用这个来选择这种类型--> <logger name="logerror"> <level value="ERROR"/> <appender-ref ref="ErrorAppender"/> </logger> <!--log4net.LogManager.GetLogger("loginfo")用这个来选择这种类型--> <logger name="loginfo"> <level value="INFO"/> <appender-ref ref="InfoAppender"/> </logger> <!--<root> <level value="INFO"/> <appender-ref ref="InfoAppender"/> <appender-ref ref="ConsoleAppender"/></root>-->
</log4net>
2 添加一个类库
public class LogHelper
{ public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror"); //选择<logger name="logerror">的配置static LogHelper()
{ SetConfig(); }/// <summary>
/// 默认配置。按配置文件 /// </summary> public static void SetConfig() { log4net.Config.XmlConfigurator.Configure(); }/// <summary>
/// 写日志。出错时会写入 /// </summary> /// <param name="info"></param> /// <param name="se"></param> public static void WriteErrorLog(string info, Exception se) { if (logerror.IsErrorEnabled) { logerror.Error(info, se); } }public static void WriteErrorLog(string info)
{ if (logerror.IsErrorEnabled) { logerror.Error(info); } }/// <summary>
/// 记录信息日志 /// </summary> /// <param name="info"></param> public static void WriteInfoLog(string info) { if (logerror.IsErrorEnabled) { logerror.Info(info); } }/// <summary>
/// 记录警告日志 /// </summary> /// <param name="info"></param> public static void WriteWarnLog(string info) { if (logerror.IsErrorEnabled) { logerror.Warn(info); } } }3 实咧调用
protected void Page_Load(object sender, EventArgs e)
{ LogHelper.WriteErrorLog("异常信息测试"); }