Configure RollingFileAppender Date rolling programmatically

log4net

I'm working on configuring a RollingFileAppender in log4net programmatically. I have something like this currently:

RollingFileAppender fa = new RollingFileAppender();
fa.AppendToFile = true;
fa.RollingStyle = RollingFileAppender.RollingMode.Date;
fa.DatePattern = "yyyyMMdd";
fa.StaticLogFileName = true;
// Set more properties and add the appender to the root repository

This refuses to roll the log based on date. If I switch the RollingStyle to Size or Composite, it will then roll, but only size based, not date based. I've tried removing the StaticLogFileName and playing around with the DatePattern to no avail.

I have to configure this programmatically, as I don't know the number of appenders at runtime, so configuring it using XML, which I've been able to get working in the past, is a no-go. Anybody got any ideas? Am I missing something?

Best Answer

This behaviour is because of the fact that RollingFileAppender (and many other appenders as well as filters) implements the IOptionHandler interface which "allows an object to defer activation of its options until all options have been set. This is required for components which have related options that remain ambiguous until all are set".

And further: "The ActivateOptions method must be called on this object after the configuration properties have been set. Until ActivateOptions is called this object is in an undefined state and must not be used."

When configuring adapters via xml files you won't have to think about activation since it is done by the log4net configurator.

Related Topic