第一步:config配置,独立文件的
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net> <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> <!-- Set root logger level to ERROR and its appenders --> <root> <level value="ALL"/> <!--<appender-ref ref="SysAppender"/>--> </root> <!-- Print only messages of level DEBUG or above in the packages --> <logger name="SysAppenderLogger"> <level value="ALL"/> <appender-ref ref="SysAppender"/> </logger> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logger/ttt/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> <param name="Header" value=" ----------------------header-------------------------- " /> <param name="Footer" value=" ----------------------footer-------------------------- " /> </layout> </appender> <logger name="WeiXinResultLogger"> <level value="ALL"/> <appender-ref ref="WeiXinResult"/> </logger> <appender name="WeiXinResult" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logger/weixinresult/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> <param name="Header" value=" ----------------------header-------------------------- " /> <param name="Footer" value=" ----------------------footer-------------------------- " /> </layout> </appender> </log4net> </configuration>
第二步:log4Helper配置
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; [assembly: log4net.Config.XmlConfigurator(ConfigFile = @"weixin\Log4.config", Watch = true)] namespace SERP3.Common { /// <summary> ///错误记录日志 ///注意:在需要记录log的类前加入[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"weixin\Log4.config", Watch = true)] /// </summary> public class Log4Helper { //private static readonly log4net.ILog log = log4net.LogManager.GetLogger("SysAppender123Logger"); //private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//动态获得名称 public Log4Helper() { } private static log4net.ILog log(string appenderName) { return log4net.LogManager.GetLogger(appenderName); } public static void LogInfo(string Message, string appenderName = "defaultLogger") { log4net.ILog logger = log(appenderName); if (logger.IsInfoEnabled) logger.Info(Message); } public static void LogInfo(string appenderName, string Message, Exception ex) { log4net.ILog logger = log(appenderName); if (logger.IsInfoEnabled) logger.Info(Message, ex); } public static void ErrorInfo(string appenderName, string Message) { log4net.ILog logger = log(appenderName); if (logger.IsInfoEnabled) logger.Error(Message); } public static void ErrorInfo(string appenderName, string Message, Exception ex) { log4net.ILog logger = log(appenderName); if (logger.IsInfoEnabled) logger.Error(Message, ex); } //public static void DebugInfo(string appenderName, string Message) //{ // if (!log(appenderName).IsInfoEnabled) // SetConfig(); // log(appenderName).Debug(Message); //} } }
第三步:调用
try { Result result = CreateResult(xmlStr); res = result.Save(); } catch (Exception ex) { SERP3.Common.Log4Helper.ErrorInfo("WeiXinResultLogger",ex.Message+xmlStr,ex); }
注意点:1.[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"weixin\Log4.config", Watch = true)]
namespace SERP3.Common {
时间: 2024-11-15 06:05:18