I've written a .NET DLL which is called from another application using COM interop. I'd like to use log4net, but I need to specify the location of the log file at runtime from the other application.
I've always used log4net in the simplest possible way for WinForms project, but since a DLL project doesn't have an app.config, and my calling application also doesn't have one (it's not even .NET), I've tried to read up a bit about how log4net works and attempted to set it without using a .config file at all.
I've got it to the stage where it compiles, and sort of conforms to my tiny understanding of log4net, but it doesn't write to the log file.
Here's what I have so far, can anyone point out my glaring errors/misconceptions, or else tell me a better way of logging from my dll?
private log4net.Core.ILogger _ilogger;
private ILog _logger;
public void Initialize(String LogFileName)
{
log4net.Repository.ILoggerRepository Repo = null;
try {
Repo = log4net.LogManager.GetRepository(Assembly.GetExecutingAssembly().FullName);
} catch (log4net.Core.LogException) {
//ignore, domain not yet created
}
if (Repo == null) {
Repo = log4net.LogManager.CreateRepository(Assembly.GetExecutingAssembly().FullName);
log4net.Appender.RollingFileAppender appender = new log4net.Appender.RollingFileAppender();
appender.Layout = new log4net.Layout.PatternLayout("%d - %m%n");
appender.File = LogFileName;
appender.MaxSizeRollBackups = 10;
appender.MaximumFileSize = "100MB";
appender.AppendToFile = true;
appender.Threshold = log4net.Core.Level.Debug; //NEW: set level to Debug
appender.ActivateOptions();
Repo.Threshold = log4net.Core.Level.Debug; //NEW: set level to Debug
log4net.Config.BasicConfigurator.Configure(Repo, appender);
}
// This doesn't seem to create the interface I need for logging
_ilogger = Repo.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.FullName);
// This doesn't seem to write to the log file for some reason.
_logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.FullName);
_logger.Debug("Application started");
}
Thanks!