1

I have an issue to diagnose, and to get an understanding of it, I wrote two unit tests - one that gets a Logger that exists in my config, and another that gets logger that doesn't exist.

The first surprise was that even if the logger name doesn't exist, I'm still getting back something (instead of NULL, as I was expecting). And it even has a ConsoleAppender attached to it.... okay.... but where does that come from?

Log4net config:

<log4net>
   <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\tmp\log" />
      ... (some other settings) ....
   </appender>
   <root>
      <level value="FATAL"/>  
      <appender-ref ref="RollingFileAppender" />
   </root>
   <logger name="DefinedLogger" additivity="false">
      <level value="DEBUG" />
      <appender-ref ref="MyCustomAppender" />
   </logger>
   <appender name="MyCustomAppender" type="MyAssembly.CustomAppender, MyAssembly">
      .....
   </appender>
</log4net>

Next, I was trying to understand how I can check what appenders are configured in each logger. My first attempt using the .Repository kept giving back all three appenders that are configured in my config file - OK, makes sense, since it's all the appenders that are in the repository.... but how can I check for an individual entry what appenders are attached?

[Test]
public void TestGetLoggerWithValidNameGetsLogger() {
    // Arrange
    string loggerName = "DefinedLogger";

    // Act
    ILog myCustomLogger = LogManager.GetLogger(loggerName);

    // this returns *ALL* appenders in the config - not those attached to this logger....
    var appenders = myLogger.Logger.Repository.GetAppenders();  

    // Assert            
    Assert.IsNotNull(myCustomLogger, "MyCustomLogger is NULL");
}

Any ideas? How can I check to make sure the proper appender(s) have been attached to my logger here?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does your `MyCustomAppender` inherit from a log4net concrete appender or from `AppenderSkeleton`? – Philip Pittle Jul 24 '14 at 10:09
  • 1
    [Related](http://stackoverflow.com/questions/382479/how-do-i-disable-log4net-status-messages-to-the-console), and [this](http://stackoverflow.com/questions/126939/log4net-c-disable-default-logging) also for `ConsoleAppender` part – Sriram Sakthivel Jul 24 '14 at 10:10
  • @PhilipPittle: it inherits directly from `AppenderSkeleton` – marc_s Jul 24 '14 at 10:36
  • Marc_s can you show your code where you call `.Configure`? – Philip Pittle Jul 24 '14 at 10:37
  • @PhilipPittle: I had a call to `BasicConfigurator.Configure();` in my test fixture's `[SetUp]` method. That seems to have caused the `ConsoleAppender` to be added to all loggers. I've removed this, everything works fine - but I still don't know how to check the attached appenders on a per-logger basis.... – marc_s Jul 24 '14 at 11:16

0 Answers0