Bill Greer
Bill Greer

Reputation: 3156

Second Log4net logger not writing to unique file

I have an application that is using Anotar.Log4Net for logging. I cannot strip out the Anotar.Log4Net as other team members are using it. I would like to add my own logging file. I added an additional file appender to my app.config called BGDEV. I reviewed the post here to see how other's are accomplishing the task of having two file appenders. The desired behavior below is for each logger to log to it's own file. Everything is logging to MainLog.txt. Is this because there is something wrong with the app.config settings or because I have the anotar library installed?

<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="MainLog.txt"/>
  <appendToFile value="true"/>
  <rollingStyle value="Size"/>
  <maxSizeRollBackups value="5"/>
  <maximumFileSize value="10MB"/>
  <staticLogFileName value="true"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
  </layout>
</appender>
<appender name="BGDEV" type="log4net.Appender.RollingFileAppender">
  <file value="BG_Log.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <datePattern value="yyyyMMdd" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true"/>
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="INFO" />
    <levelMax value="FATAL" />
  </filter>
</appender>
<root>
  <level value="INFO"/>
  <appender-ref ref="FileAppender"/>
</root>
<logger additivity="false" name="BGDev">
  <level value="INFO"/>
  <appender-ref ref="BGDev" />
</logger>

In code I create a logger and use it. Is there something wrong with my config file or is this becaise I am using

private static readonly ILog otherLog = LogManager.GetLogger("BGDEV");
otherLog.Info("TEST OTHER LOG");

Upvotes: 1

Views: 205

Answers (1)

Iridium
Iridium

Reputation: 23721

You have a couple of issues with naming in your code and config. Names are case sensitive - you're defining a logger named BGDev, but you call GetLogger("BGDEV"), since no logger named BGDEV exists, it falls back to the root logger.

Your logger named BGDev also references an appender named BGDev, however again, no such appender exists, since you are defining it with an all-uppercase name BGDEV.

Finally, your BGDev logger needs to have a <layout>...</layout> section, or it won't be able to format log messages.

If you fix these issues, it should work as you expect.

Upvotes: 3

Related Questions