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.
Each instance of a DLL has to create a separate log file. Therefore I do all the Log4Net configuration programatically.
I've used some help from here.
Here is my 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);
}
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.
A link from here explains how to shutdown appenders. But the problem is that in my destructor a call to log.Logger.Repository.GetAppenders(); returns an empty array.
How should I solve it?
Just a note: the process that loads my DLL is from 3rd party and I don't know the internals of it.