views:

49

answers:

2

Hi all:

For Log4Net used in ASP.NET, how do I specify specify different levels to separate appenders in web.config?

Thanks.

+1  A: 

Pure guesswork at play here.
Looked at the documentation & thought, this could be the way to do it.

Note: DEBUG level messages are logged to console & WARN level messages are logged to eventlog.

I hope it works :)

<log4net>
    <!-- A1 is set to be a ConsoleAppender -->
    <appender name="A1" type="log4net.Appender.ConsoleAppender">

        <!-- A1 uses PatternLayout -->
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
        </layout>
    </appender>

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

   <logger name="DebugLogger">
      <level value="DEBUG" />
      <appender-ref ref="A1" />
   </logger>

   <logger name="WarnLogger">
      <level value="WARN" />
      <appender-ref ref="EventLogAppender" />
   </logger>
</log4net>
shahkalpesh
+1  A: 

This works for SmtpAppender at least. Guess it will work for other appenders too, but I haven't tested it

<evaluator type="log4net.Core.LevelEvaluator">
   <threshold value="WARN"/>
simendsjo