tags:

views:

25

answers:

2

Using log4net, can I configure a logger so that it can override a single parameter of one of the appenders? Some examples:

<log4net>
  <appender name="A1" type="log4net.Appender.FileAppender">
    <file value="log4net.log">
  </appender>
  <root>
    <level value="ERROR" />
    <appender-ref ref="A1" />
  </root>
  <logger name="MyLogger">
    <level value="WARN" />
    <appender-ref ref="A1">
      <file value="MyLogger.Log"/>
    </appender-ref>
  </logger>
</log4net>

For example, I'd like to configure one of my sub-loggers to send output to a different file from the rest of my application. Am I going the wrong way? Should I configure it with a new appender? What about sending different levels to different appenders? e.g.

<log4net>
  <appender name="default" type="log4net.Appender.FileAppender">
    <file value="My.log">
  </appender>
  <appender name="errors" type="log4net.Appender.FileAppender">
    <file value="Errors.log">
  </appender>
  <root>
    <level value="DEBUG">
      <appender-ref ref="default" />
    </level>
    <level value="ERROR">
      <appender-ref ref="errors" />
    </level>
  </root>
</log4net>
A: 

I use something similar to your first xml file. (With Log4net for C#)

Don't think the second xml will work, because, as I understand it, it would interpret <level value="DEBUG"> as setting the logging level to DEBUG , whereas you are wanting it to interpret as

  if (Level == DEBUG) ...
sgmoore
How can it work? Is the syntax just like that? It doesn't seem to have any effect for me when I try it.
Eric Falsken
A: 

I have added this as a new answer as it is too long to fit in a comment.

Try something like


        <appendToFile value="true" />

        <layout type="log4net.Layout.PatternLayout">
            <header value="[Header]%date&#13;&#10;" />
            <footer value="[Footer]%date&#13;&#10;" />
            <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
        </layout>
    </appender>

    <appender name="MyLogger" type="log4net.Appender.FileAppender">
        <file type="log4net.Util.PatternString" value="MyLogger.Log" />

        <appendToFile value="true" />

        <layout type="log4net.Layout.PatternLayout">
            <header value="[Header]%date&#13;&#10;" />
            <footer value="[Footer]%date&#13;&#10;" />
            <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
        </layout>
    </appender>


    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
        <level value="INFO" />
        <appender-ref ref="A1" />
    </root>

    <logger name="SpecialMethod">
        <level value="DEBUG" />
        <appender-ref ref="MyLogger" />
    </logger>

</log4net
sgmoore
Yes, looks like that's the closest it gets, putting multiple appenders. But it still dosn't help to reproduce a case where debug/info messages go to one log and errors to another...like apache's http and error log.
Eric Falsken