Note that there are some explanatory texts on larger screens.

plurals
  1. POLog4Net doesn't release resources
    text
    copied!<p>I have a DLL file which does Log4Net logging to a a file. There is a process which loads the DLL and can create multiple instances of the DLL.</p> <p>Each instance of a DLL has to create a separate log file. Therefore I do all the Log4Net configuration programatically.</p> <p>I've used some help from <a href="https://stackoverflow.com/questions/1519728/programmatically-adding-and-removing-log-appenders-in-log4net">here.</a></p> <p>Here is my code:</p> <pre><code>public class LogHelper { private PatternLayout _layout = new PatternLayout(); private const string LOG_PATTERN = "%date %-5level - %message%newline"; private String Configuration; public static string DefaultPattern { get { return LOG_PATTERN; } } public ILog log = null; public LogHelper(String configuration) { Configuration = configuration; InitialiseLogger(); _layout.ConversionPattern = DefaultPattern; _layout.ActivateOptions(); Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); hierarchy.Configured = true; hierarchy.LevelMap.Add(log4net.Core.Level.Debug); hierarchy.LevelMap.Add(log4net.Core.Level.Critical); hierarchy.LevelMap.Add(log4net.Core.Level.Info); hierarchy.LevelMap.Add(log4net.Core.Level.Warn); hierarchy.LevelMap.Add(log4net.Core.Level.Error); hierarchy.LevelMap.Add(log4net.Core.Level.Fatal); } ~LogHelper() { log.Debug("Closing myself down"); IAppender[] appenders = log.Logger.Repository.GetAppenders(); //appenders are empty log.Logger.Repository.Shutdown(); } public void InitialiseLogger() { Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); Logger newLogger = hierarchy.GetLogger(Configuration) as Logger; PatternLayout patternLayout = new PatternLayout(); patternLayout.ConversionPattern = LOG_PATTERN; patternLayout.ActivateOptions(); RollingFileAppender roller = new RollingFileAppender(); roller.Layout = patternLayout; roller.AppendToFile = true; roller.RollingStyle = RollingFileAppender.RollingMode.Size; roller.MaxSizeRollBackups = 4; roller.MaximumFileSize = "10MB"; String name = String.Format("-{0:yyyy-MM-dd_HH-mm-ss}", DateTime.Now); roller.File = "C:\\Logs\\" + Configuration + name + ".log"; roller.ImmediateFlush = true; roller.ActivateOptions(); newLogger.AddAppender(roller); log = LogManager.GetLogger(Configuration); } </code></pre> <p>The problem is that log.Debug("Closing myself down"); is not logged to a log file; I know it is being called. And the log files never get released, unless I stop the process that loads my DLL0, and I do not want to stop it.</p> <p>A link from <a href="https://stackoverflow.com/questions/5892916/proper-way-to-shutdown-a-logger-instance-in-log4net">here</a> explains how to shutdown appenders. But the problem is that in my destructor a call to log.Logger.Repository.GetAppenders(); returns an empty array.</p> <p>How should I solve it?</p> <p>Just a note: the process that loads my DLL is from 3rd party and I don't know the internals of it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload