log4net 自定义日志级别记录多个日志
程序中原来只记录一个日志,现在我要写一个用户操作日志,需要与原来的日志分开,在config文件中一阵折腾无果(要么写不全,要么写重了,反正没办法完美分离,要么与现存代码没办法完美兼容),差点放弃准备自己直接写txt文件了。
在网上看到可以写自定义filter,可以and可以or,那还有什么事情是搞不定的呢? 代码比较简单,不解释。
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; } } } 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; } } }
<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>
public partial class Test : System.Web.UI.Page { static ILog log = log4net.LogManager.GetLogger(typeof(Test)); static ILog logForSubmitStudentCode = log4net.LogManager.GetLogger("SubmitStudentCode"); static ILog logForUserAction = log4net.LogManager.GetLogger("UserAction"); protected void Page_Load(object sender, EventArgs e) { log.Fatal("Fatal"); log.Error("Error"); log.Warn("Warn"); log.Info("info"); logForSubmitStudentCode.Logger.Log(typeof(Test), new log4net.Core.Level(50000, "SubmitStudentCode"), "SubmitStudentCode test", null); logForUserAction.Logger.Log(typeof(Test), new log4net.Core.Level(50001, "UserAction"), "UserAction test", null); } }
桂棹兮兰桨,击空明兮溯流光。