Log4Net, Cannot use multiple RollingFileAppenders at a time

781 Views Asked by At

I have a strange problem. I'm trying to create multiple rolling file appenders referencing everyone the same file, but using only one of theese appenders at a time.

The matter is, if i declare my appender like this:

<appender name="APP1" type="log4net.Appender.RollingFileAppender">
  <file value="C:\Log.txt" />
  <layout type="log4net.Layout.PatternLayout,log4net">
    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - message%newline" />
  </layout>
 </appender>
<logger name="APP1" additivity="false">
  <level value="DEBUG" />
  <appender-ref ref="APP1" />
</logger>
<appender name="APP2" type="log4net.Appender.RollingFileAppender">
  <file value="C:\Log.txt" />
  <layout type="log4net.Layout.PatternLayout,log4net">
    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - message%newline" />
  </layout>
 </appender>
<logger name="APP2" additivity="false">
  <level value="DEBUG" />
  <appender-ref ref="APP2" />
</logger>

Then my "APP1" logger writes to the file with no problem, but not the "APP2" logger, like if it's obtaining only the first logger. Obviously, if i make the two logger switch place, everything works for "APP2", but not for "APP1"

Also, when the appending fails i can see this in the debug window:

log4net: Opening file for writing [C:\Log.txt] append [True]
log4net: Opening file for writing [C:\Log.txt] append [True]

Which, i guess, it means it is appending, but strangely enough it doesn't do so. And in the working scenario I cannot see those logs. Any hint is greatly appreciated!! Thanks for your time

1

There are 1 best solutions below

1
On

Maybe you can write it like this:

<root>
    <level value="DEBUG" />
    <appender-ref ref="APP1" />
    <appender-ref ref="APP2" />
</root>

Here is a configuration file that uses multiple appenders. You can find it in http://logging.apache.org/log4net/release/manual/configuration.html

<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
        <!-- Pattern to output the caller's file name and line number -->
        <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
    </layout>
</appender>

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="example.log" />
    <appendToFile value="true" />
    <maximumFileSize value="100KB" />
    <maxSizeRollBackups value="2" />

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level %thread %logger - %message%newline" />
    </layout>
</appender>

<root>
    <level value="DEBUG" />
    <appender-ref ref="Console" />
    <appender-ref ref="RollingFile" />
</root>

It's possible that uses multiple appenders, but you can't use multiple FileAppenders(or RollingFileAppenders) to log in the same file. So if you change the APP2 appender to log in a different file, it works.

<appender name="APP2" type="log4net.Appender.RollingFileAppender">
  <file value="C:\Log2.txt" />
  <layout type="log4net.Layout.PatternLayout,log4net">
    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - message%newline" />
  </layout>
</appender>

The problem maybe log4net can't access same file at same time by different FileAppenders.