filter to not log logging entries of particular class

794 views Asked by At

I have a adonet appender and rolling file appender configure for my application. What i want is a filter on my adonetappender via which i can omit logging on particular class. That class has multiple level of devugging which I needed for my file appender. so i can't change the level and have to omit via filter. My logging config is given below

<?xml version="1.0" encoding="utf-8" ?>
    <log4net>
       <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender" >
        <file value="..\\Logs\\Logfile.txt" />
        <appendToFile value="true" />
        <datePattern value="yyyyMMdd" />
        <rollingStyle value="Date" />
        <filter type="log4net.Filter.LevelRangeFilter">
          <acceptOnMatch value="true" />
          <levelMin value="DEBUG" />
          <levelMax value="FATAL" />
        </filter>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.fff} %10p %-5property{_App} %-5property{_Node} [%2t] %5c{1}.%M - %m%n" />
        </layout>
      </appender>   
      <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
        <immediateFlush value="true" />
        <bufferSize value="0" />
        <connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" />
        <connectionString value="data source=localhost;initial catalog=logging_db;User ID=root;Password=" />
        <commandText value="INSERT INTO system_log(appname, action, level)
                            VALUES(@appname, @action,@level);" />
        <parameter>
          <parameterName value="appname" />
          <dbType value="String" />
          <size value="32" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="My Web Service" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="action" />
          <dbType value="String" />
          <size value="150" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%c{1}" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="level" />
          <dbType value="String" />
          <size value="10" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%p" />
          </layout>
        </parameter>   
        <filter type="log4net.Filter.LevelRangeFilter">
          <acceptOnMatch value="true" />
          <levelMin value="DEBUG" />
          <levelMax value="FATAL" />
        </filter>
      </appender>
      <root>
        <level value="ALL" />
        <appender-ref ref="RollingLogFileAppender" />       
        <appender-ref ref="AdoNetAppender" />
      </root>    
    </log4net>

My class name is MyNameSpace.Business.ConditionCompare And in my class log4net object is declared as

private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
1

There are 1 answers

9
stuartd On BEST ANSWER

You don't show your file appender in the config, but what you need to do is define a logger which overrides the appenders defined in root by specifying additivity="false": this allows the logger to choose which appenders to use. As you say you want to stop logging to the database altogether, you can tell the logger just to use the file appender:

Edit: add this just under the root element:

<logger name="MyNameSpace.Business.ConditionCompare" additivity="false">
   <level value="ALL" />
   <appender-ref ref="RollingLogFileAppender" />
</logger>