You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-dev@logging.apache.org by "Mark (JIRA)" <ji...@apache.org> on 2015/09/17 15:53:05 UTC

[jira] [Created] (LOG4NET-475) Cannot add appender in Log4net at runtime

Mark created LOG4NET-475:
----------------------------

             Summary: Cannot add appender in Log4net at runtime
                 Key: LOG4NET-475
                 URL: https://issues.apache.org/jira/browse/LOG4NET-475
             Project: Log4net
          Issue Type: Bug
          Components: Appenders
    Affects Versions: 1.2.13
         Environment: Windows, C#, Visual Studio unit testing
            Reporter: Mark
            Priority: Blocker


I am trying to write a custom appender that rolls over daily, as well as on a size limit, and cleans up after itself in the process. I wrote the implementation using the AppenderSkeleton as a base class, then attempted to write a unit test to test the appender functionality. I do the following:

            DailyRollingFileAppender appender = new DailyRollingFileAppender();
            appender.RollOverOnSize = true;
            appender.RollOverDaily = true; //dont think I can test this
            appender.MaxFileSizeBytes = 1024;
            appender.RolloverDayCount = 1;
            appender.RollOverOnSize = true;
            appender.LoggingDirectory = Path.Combine(Directory.GetCurrentDirectory(), "TestLogFolder");
            appender.LogFileName = "TestLogFileName";
            appender.DateTimeFormatString = "YYYY-MM-DD-hh-mm-ss";
            appender.Cleanup = true;
            appender.Layout = new PatternLayout();
            appender.MaxNumberOfFiles = 10;
            appender.Name = "testAppender";

            //lets write some crappy log messages
            byte[] bytes = new byte[1050];

            Random rnd = new Random();
            rnd.NextBytes(bytes);

            ILog log = LogManager.GetLogger("LoggerName");
            Logger logger = (Logger)log.Logger;
            logger.AddAppender(appender);

            logger.Log(Level.Info, Encoding.UTF8.GetString(bytes), new Exception());
            logger.Log(Level.Info, Encoding.UTF8.GetString(bytes), null);

The code is based on the only examples I can find of people adding appenders to loggers at runtime, which you would want to do for a unit test. The issue is that the Logger instance I get from the call to ILog.Logger in the code above as a collection of appenders of type ReadOnlyAppenderCollection. The code silently consumes the exception it throws internally and never actually adds the appender to the collection. Can/How do I add an appender to a logger at runtime in the manner described above, so I can appropriately test that it functions. 



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)