ROBERT RICHARDSON
ROBERT RICHARDSON

Reputation: 2299

Why am I logging to the wrong file?

I have a program that will simulate the behavior of three cranes. I want each crane to have its own log file. For this question, we'll only worry about one of them. The program will also have a separate, general-purpose log file. I am planning to use the root logger as the general-purpose file. The cranes will have named loggers, "Crane1" in this case.

The Crane1 log configuration uses additivity=false to ensure that it does not inherit anything from the root. I want to be able to configure the root and the crane log files independently from each other.

My program gets the logger named "Crane1" and writes messages. However, the messages are going to the RollingFile appender and are appearing in the wrong file. They are not getting handled by the TimedRollingAppender appender. The TimedRollingAppender's file, PlantGenieTimed.log, is getting created, but it is empty.

Why is Crane1 not using the appender I expect it to?

(I used the silly name for the RollingFile appender's file to make sure that my program was reading the correct configuration file.)

Thanks very much for your help.

Configuration file:

<?xml version="1.0" encoding="utf-8"?>
<log4net>
  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="PlantGenie_ThisIsTheWrongFile.log" />
    <appendToFile value="true" />
    <maxSizeRollBackups value="90" />
    <rollingStyle value="Size" />

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

  <appender name="TimedRollingAppender" type="log4net.Appender.RollingFileAppender">
    <file value="PlantGenieTimed.log" />
    <appendToFile value="true" />
    <maxSizeRollBackups value="90" />
    <rollingStyle value="Size" />

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

  <logger Name="Crane1" additivity="false">
    <appender-ref ref="TimedRollingAppender" />
    <level value="DEBUG"/>
  </logger>

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

</log4net>

Upvotes: 0

Views: 445

Answers (1)

ROBERT RICHARDSON
ROBERT RICHARDSON

Reputation: 2299

Dang. As usual, I see the problem the instant I post the question. I used the Name property instead of the name property for the Crane1 logger. The capitalized property does not exist. I replaced it with lower case and it worked as expected.

Upvotes: 1

Related Questions