有时候为了实现自己想要的多个日志文件记录不同的内容,可能需要自定义log4net过滤器,比如我这里需要记录三个文件,这三个文件的内容又不能重复,多次尝试未果。
为了不更改任何现有日志代码的情况下,于是自定了过滤器。
<log4net> <root name="Default"> <level value="All" /> <appender-ref ref="FileAppender"/> </root> <logger name="SubmitStudentCode"> <level value="SubmitStudentCode" /> <appender-ref ref="FileAppender.SubmitStudentCode" /> </logger> <logger name="UserAction"> <level value="UserAction" /> <appender-ref ref="FileAppender.UserAction" /> </logger> <appender name="FileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log/"/> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="10"/> <param name="StaticLogFileName" value="false"/> <param name="DatePattern" value="yyyy-MM-dd".log""/> <param name="RollingStyle" value="Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="Header" value="----------------------------------------[Header]-------------------------------------- "/> <param name="Footer" value="----------------------------------------[Footer]-------------------------------------- "/> <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n%n"/> </layout> <filter type="FirsteElite.Model.Model.Log.OrFilter"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="INFO" /> </filter> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="WARN" /> <levelMax value="FATAL" /> </filter> </filter> <filter type="log4net.Filter.DenyAllFilter" /> </appender> <appender name="FileAppender.SubmitStudentCode" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log/" /> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="10"/> <param name="StaticLogFileName" value="false"/> <param name="DatePattern" value="yyyy-MM-dd"_SubmitStudentCode.log""/> <param name="RollingStyle" value="Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d %m%n"/> </layout> <filter type="log4net.Filter.LoggerMatchFilter"> <param name="LoggerToMatch" value="SubmitStudentCode" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> </appender> <appender name="FileAppender.UserAction" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log/"/> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="10"/> <param name="StaticLogFileName" value="false"/> <param name="DatePattern" value="yyyy-MM-dd"_UserAction.log""/> <param name="RollingStyle" value="Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d %m%n"/> </layout> <filter type="log4net.Filter.LoggerMatchFilter"> <param name="LoggerToMatch" value="UserAction" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> </appender> </log4net>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using log4net.Filter; using log4net.Core; namespace FirsteElite.Model.Model.Log { public class AndFilter : FilterSkeleton { private bool acceptOnMatch; private readonly IList<IFilter> filters = new List<IFilter>(); public override FilterDecision Decide(LoggingEvent loggingEvent) { if (loggingEvent == null) throw new ArgumentNullException("loggingEvent"); foreach (IFilter filter in filters) { if (filter.Decide(loggingEvent) != FilterDecision.Accept) return FilterDecision.Neutral; // one of the filter has failed } return FilterDecision.Accept; } public IFilter Filter { set { filters.Add(value); } } public bool AcceptOnMatch { get { return acceptOnMatch; } set { acceptOnMatch = value; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using log4net.Filter; using log4net.Core; namespace FirsteElite.Model.Model.Log { public class OrFilter : FilterSkeleton { private bool acceptOnMatch; private readonly IList<IFilter> filters = new List<IFilter>(); public override FilterDecision Decide(LoggingEvent loggingEvent) { if (loggingEvent == null) throw new ArgumentNullException("loggingEvent"); foreach (IFilter filter in filters) { if (filter.Decide(loggingEvent) == FilterDecision.Accept) return FilterDecision.Accept; } return FilterDecision.Deny; } public IFilter Filter { set { filters.Add(value); } } public bool AcceptOnMatch { get { return acceptOnMatch; } set { acceptOnMatch = value; } } } }
public static class UserActionLogger { static ILog userActionLogger = log4net.LogManager.GetLogger("UserAction"); public static void log(string msg) { userActionLogger.Logger.Log(null, new log4net.Core.Level(50001, "UserAction"), msg, null); } }
时间: 2024-10-17 21:22:06