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); } } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。