Log4Net Logging of two different levels to two different appenders for the same logger

后端 未结 4 1043
遥遥无期
遥遥无期 2020-11-30 02:24

We have two different asp.net applications with Log4net logging enabled. They both have the same version of Log4Net, 1.2.10.0.

We have added the log4net.Append

相关标签:
4条回答
  • 2020-11-30 02:39

    You could specify a LevelRangeFilter for each of the appenders, and define each appender in the root to log at different levels.

    <appender name="filelogAppender" type="log4net.Appender.RollingFileAppender" >
        <!--File Details/Layout Options-->
        <filter type="log4net.Filter.LevelRangeFilter">
            <levelMin value="INFO"/>
        </filter>
    </appender>
    <appender name="dbLogAppender" type="log4net.Appender.AdoNetAppender">
        <!--SQL Options-->      
        <filter type="log4net.Filter.LevelRangeFilter">
            <levelMin value="ERROR"/>
        </filter>
    </appender>
    <root>
        <level value="INFO"/>
        <appender-ref ref="filelogAppender" /> <!--Prints info, warn, error, or fatal logs. -->
        <appender-ref ref="dbLogAppender" />   <!--Prints only error or fatal logs. -->
    </root>
    
    0 讨论(0)
  • 2020-11-30 02:42

    Simply use Evaluator, you do not need to give threshold in all the appenders.

    <appender name="filelogAppender" type="log4net.Appender.RollingFileAppender">
            <evaluator type="log4net.Core.LevelEvaluator">
              <threshold value="ERROR" />
      </evaluator>
    </appender>
    
    0 讨论(0)
  • 2020-11-30 02:44

    You should be able to set the threshold property of each appender separately and include them in the same root.

    <appender name="filelogAppender" type="log4net.Appender.RollingFileAppender">
      <threshold value="Error" />
    </appender>
    <appender name="dblogAppender" type="log4net.Appender.AdoNetAppender">
      <threshold value="Info" />
    </appender>
    <root>
      <appender-ref ref="filelogAppender" />
      <appender-ref ref="dblogAppender" />
    </root>
    

    reference

    0 讨论(0)
  • 2020-11-30 03:00

    Two <root> elements are not allowed.

    From documentation:

    root     Optional element, maximum of one allowed. Defines the configuration of the root logger.
    

    You can however have a root logger like you do, and then have a separate logger specified by a namespace.

    <root>
        <level value="Error"/>
        <appender-ref ref="filelogAppender"/>
    </root>
    <logger name="MyCompany.MyApp.Namespace">
        <level value="Info"/>
        <appender-ref ref="dbLogAppender"/>
    </logger>
    
    0 讨论(0)
提交回复
热议问题