• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#自定义log4net过滤器

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

有时候为了实现自己想要的多个日志文件记录不同的内容,可能需要自定义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&quot;.log&quot;"/>
            <param name="RollingStyle" value="Date"/>
            <layout type="log4net.Layout.PatternLayout">
                <param name="Header" value="----------------------------------------[Header]--------------------------------------&#xA;&#13;&#10;&#13;&#10;"/>
                <param name="Footer" value="----------------------------------------[Footer]--------------------------------------&#xA;&#13;&#10;&#13;&#10;"/>
                <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&quot;_SubmitStudentCode.log&quot;"/>
      <param name="RollingStyle" value="Date"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d&#9;%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&quot;_UserAction.log&quot;"/>
      <param name="RollingStyle" value="Date"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d&#9;%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);
            }
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
createaC#contextmenufromcode发布时间:2022-07-14
下一篇:
C#Enum,Int,String,之间及bool与int之间的转换发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap