public class Log4 { public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo"); public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror"); //public static void SetConfig() //{ // log4net.Config.XmlConfigurator.Configure(); //} //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="info"></param> /// <param name="se"></param> public static void WriteLog(string info, Exception se) { if (logerror.IsErrorEnabled) { logerror.Error(info, se); } } }
方法一:
AssemblyInfo.cs 添加
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]
方法二:
Global.asax 添加
protected void Application_Start(object sender, EventArgs e)
{
try
{
Common.Log4.SetConfig(new FileInfo(Server.MapPath("log4net.config")));
}
catch { }
}
log4net.config 配置
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> </configSections> <appSettings> </appSettings> <log4net> <!--错误日志类--> <logger name="logerror"> <level value="ALL" /> <appender-ref ref="ErrorAppender" /> </logger> <!--信息日志类--> <logger name="loginfo"> <level value="ALL" /> <appender-ref ref="InfoAppender" /> </logger> <!--错误日志附加介质--> <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".txt"" /> <param name="RollingStyle" value="Date" /> <!--布局--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%-5p %d [%c] %m%n" /> </layout> </appender> <!--信息日志附加介质--> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\LogInfo\" /> <param name="AppendToFile" value="false" /> <param name="MaxFileSize" value="1" /> <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="<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" /> </layout> </appender> </log4net> </configuration>