views:

538

answers:

3

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!

+2  A: 

Edit- Update. Reread the question. It looks like you just need to call the overloaded version of this (passing in details of a config file)- you basically just want to configure the logging from within the COM DLL?

Jokingly I google'd log4com and found this. I've not used it but it might be worth a look!

RichardOD
What a terrifying thought! :) My calling application does have its own logging, and this DLL is not just for logging, my thinking behind not passing detailed information back to the parent app for logging was a saving in COM traffic, which I had read was slow, and a bit of resiliance in case there was some kind of failure and the parent app could not log it. I'm kinda new to the world of COM... this may not be nessecary I guess. If I can't get this working then I will get my DLL to report absolutely everything back to the calling app and then log it there.
Colin Pickard
I reread your question, and I think I get what you are asking now.
RichardOD
Is there a way to change the destination of the log file with this method? I might be confused.
Colin Pickard
I found my final answer by looking at the different overloads of XmlConfigurator.Configure and BasicConfigurator.Configure so I'll give you the accept for this one, since you steered me in the right direction. Thanks!
Colin Pickard
Good you got it sorted- it looks like you fixed this before I saw your previous comment.
RichardOD
+1  A: 

Try explicitly setting the level to DEBUG on both your Logger and Appender.

Vinay Sajip
Hmm... It still doesn't seem to write anything to the log file
Colin Pickard
+2  A: 

I think the answer was I didn't need the new repository at all. I was just making matters too complicated. This decimated version seems to work fine:

public void Initialize(String LogFileName)
    {
        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;
        appender.ActivateOptions();
        log4net.Config.BasicConfigurator.Configure(appender);

        _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.FullName);
        _logger.Debug("Application started");
    }
Colin Pickard
Thanks for this - just what I needed...
Matt