BriceTRockindale
BriceTRockindale

Reputation: 319

What's wrong with my log4net config file

I have a config file with two appenders, one file appender and one database appender. I want to log everything to the file appender, and only log exceptions to the database appender. When setting up both appenders in the section it logs fine but all log events are sent to both appenders, which is not what i want.

I changed the configuration but with this current configuration, exceptions get logged to the database, and nothing is getting written to the file appender. Can anyone tell me why I am not getting anything written to the file appender?

<log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\Log4net\Workflow\TestLog.txt" />
      <threshold value="All" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p {%logger} %d %5rms %-22.22c{1} %-18.18M - %m%n" />
      </layout>
    </appender>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <!-- Removed to keep this snippet simple-->
    </appender>
    <root>
      <level value="Error" />
      <appender-ref ref="AdoNetAppender" />
    </root>
    <logger name="AllLogs">
        <level value="ALL" />
        <appender-ref ref="RollingLogFileAppender" />      
    </logger>

  </log4net>

Upvotes: 0

Views: 262

Answers (1)

samy
samy

Reputation: 14972

What you have here is the following:

  • all logs events with the level Error or more will go to the AdoNetAppender
  • all logs events originating from a logger whose name is based on AllLogs will go to the RollingLogFileAppender

From what I understand you want all logs to default to the file, and only error ones to go also to the database. Then simply add both appenders to your root logger so that both get all events, and add filters to only let filters you're interested in pass through: a level range filter on your database appender would work

<log4net debug="true">

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <!-- rest of config snipped to save space -->
    </appender>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
        <filter type="log4net.Filter.LevelRangeFilter">
            <levelMin value="ERROR" />
            <levelMax value="FATAL" />
        </filter>
        <!-- rest of config snipped to save space -->
    </appender>

    <root>
        <appender-ref ref="AdoNetAppender" />
        <appender-ref ref="RollingLogFileAppender" />   
    </root>
</log4net>

Of course if you don't want any duplicate just filter errors and above in the rolling file appender

Upvotes: 1

Related Questions