Log4net使用
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <logger name="LogInfo"> <level value="INFO" /> <appender-ref ref="InfoAppender" /> </logger> <logger name="LogError"> <level value="ERROR" /> <appender-ref ref="ErrorAppender" /> </logger> <!--定义输出到文件中--> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <!--定义文件存放位置--> <file value="Extends\\logs\\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyy\\yyyy-MM\\yyyy-MM-dd\\yyy-MM-dd'.txt'"/> <staticLogFileName value="false"/> <param name="MaxSizeRollBackups" value="100"/> <layout type="log4net.Layout.PatternLayout"> <!--每条日志末尾的文字说明--> <conversionPattern value="%newline%n记录时间:%date %n%message%newline%n"/> </layout> </appender> <!--定义输出到文件中--> <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"> <!--定义文件存放位置--> <file value="Extends\\logs\\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyy\\yyyy-MM\\yyyy-MM-dd\\yyy-MM-dd error'.txt'"/> <staticLogFileName value="false"/> <param name="MaxSizeRollBackups" value="100"/> <layout type="log4net.Layout.PatternLayout"> <!--每条日志末尾的文字说明--> <conversionPattern value="%newline%n记录时间:%date %n%message%newline%n"/> </layout> </appender> </log4net> </configuration>
using System; using System.IO; using log4net; namespace JADAY.Frame.Common { public class LogHelper { public static ILog LogInfo = LogManager.GetLogger("LogInfo"); public static ILog LogError = LogManager.GetLogger("LogError"); public static LogHelper Instance; public static LogHelper GetInstance() { return Instance ?? (Instance = new LogHelper()); } public LogHelper() { var file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\App_Data\\log4net.cfg.xml"); log4net.Config.XmlConfigurator.Configure(file); } public static void SetConfig(FileInfo configFile) { log4net.Config.XmlConfigurator.Configure(configFile); } public static void SetConfig() { var file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\App_Data\\log4net.cfg.xml"); log4net.Config.XmlConfigurator.Configure(file); } /// <summary> /// 写日志 /// </summary> /// <param name="info">信息</param> public void WriteInfo(string info) { if (LogInfo.IsInfoEnabled) { LogInfo.Info(info); } } /// <summary> /// 写日志 /// </summary> /// <param name="info">信息</param> public void WriteError(string info) { if (LogError.IsErrorEnabled) { LogError.Error(info); } } } }
作者:apegu
地址:http://www.cnblogs.com/jiangqw
如果,您认为阅读这篇博客让您有些收获,不妨点击一下推荐按钮。