Question

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!

Was it helpful?

Solution

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!

OTHER TIPS

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");
    }

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top