Using memory appender to test logger

I found a bug in IglooCoder.Commons, the Logger doesn’t log the correct class. The fix is easy, but I want to create a test before fixing it, in case the next release the problem came back.

How to test the logger without logging at the log files generated? This is the perfect situation we should use memory appender.

    public class LogTests
        private MemoryAppender _appender;
        private LoggingEvent _event;

        public void Init()
            Log.Initialize(new AglcLog4NetLogger(SystemInfo.ApplicationBaseDirectory + "\\Log4Net.config"));
            _appender = new MemoryAppender();

            _appender.Name = "Unit Testing Appender";

            _appender.Layout =
                new PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");

            _appender.Threshold = Level.All;


            Logger root = ((Hierarchy) LogManager.GetRepository()).Root;


            root.Repository.Configured = true;

        public void should_get_the_correct_class_name_to_log()

            _event = _appender.GetEvents()[0];

            var data = _event.GetLoggingEventData();

            Assert.AreEqual(this.GetType().ToString(), data.LoggerName);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s