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?