4

My application uses several appenders -- a FileAppender, MemoryAppender, and AdoNetAppender. I have a command to reset the log so that a new file gets created on demand. This works great -- I simply call log4net.LogManager.GetRepository().Shutdown().

I also log simultaneously to the MemoryAppender, and I have a timer that pulls events out of the appender and displays them in a log window. When I use the aforementioned reset function, the log window stops displaying messages because no more events are being logged.

For the FileAppender, I was able to get it to start logging again by calling ActivateOptions after setting a proper filename. I have called ActivateOptions on the MemoryAppender, but that doesn't get it to start logging again. Before calling ActivateOptions, I have looked at the object to ensure that it is already configured. It actually preserves the configuration from the time I called the reset function, so that seems okay.

I have also searched online, and people have said that you just have to call log4net.Config.DOMConfigurator.Configure (deprecated -- now it is log4net.Config.XmlConfigurator.Configure`), but that didn't work, either.

Is there another method I can try to shutdown / restart logging, while supporting File, Memory, and Ado appenders?

Dave
  • 14,618
  • 13
  • 91
  • 145
  • When you say "reset the log" are you just wanting the file to be cleared to empty? – James Michael Hare Oct 25 '11 at 18:27
  • I want a new file to be created... but I have the FileAppender working just fine. It's the MemoryAppender that doesn't seem to log any events after I call Shutdown and try to restart with ActivateOptions. – Dave Oct 26 '11 at 18:28
  • I should also add that the AdoNetAppender works as well. To implement that, I call Shutdown, then rename the s3db file, then copy over a new "starting" s3db file with the original filename. The funny thing is that for the AdoNetAppender, I don't call ActivateOptions anywhere -- it just magically works somehow. – Dave Oct 26 '11 at 18:33

1 Answers1

2

I think you get what you want by not calling the Shutdown method. Simply set a new file name on your appender and call ActivateOptions. This will close the old log file and open the new one...

Stefan Egli
  • 17,398
  • 3
  • 54
  • 75
  • I keep changing my comment here... :) Ok, I will give that suggestion a try. Hopefully it will also work for the AdoNetAppender. – Dave Oct 26 '11 at 18:37
  • thanks! This totally did the trick. I no longer call Shutdown when resetting the logs, and the MemoryAppender works. I just change the filename and call ActivateOptions for the FileAppender. And for the AdoNetAppenders, I call Close() on them, move the old s3db file, and then call ActivateOptions and a new log database is generated. Perfect! – Dave Oct 27 '11 at 12:31