jimconstable
jimconstable

Reputation: 2388

How do I make log4net available from a class library dll?

I think I am missing something. I have a written a custom log4net appender in a class library dll so I figure I should expose log4net through this as well. That way others just add the single logging class library which is carrying log4net and they get the custom appender.

Do I need to wrap log4net to do this or is there some other way of exposing it?

Upvotes: 0

Views: 1202

Answers (3)

Stefan Egli
Stefan Egli

Reputation: 17018

If you wrote an appender you need to do two things in order to use it: Configure it (like any other appender) and make sure that your assembly is deployed.

In order to configure it you need to have somewhere in your log4net configuration a block for your appender:

<appender name="YourCustomAppenderName" type="YourAssemblyNameSpace.YourAppenderType">
    ....
</appender>

In my experience it works best when you fully qualify your assembly but it is not strictly necessary. The second part can be done by an installer, but a maybe somewhat crude but quite effective way is to just create a reference to the appender assembly in your application. This way the assembly will always be copied to the output directory.

Writing a wrapper is not necessary for this. There may be reasons to write a wrapper, but this should be done if you want to be able to have additional functionality (e.g. methods that accept additional parameters). Another reason for a wrapper might be that you can replace log4net with something else (though there would still be some work with updating all configuration files)

Upvotes: 0

Sumit
Sumit

Reputation: 3068

I prefer to wrap log4net and provide single point of entry for all logging needs. eg.

My interface will be like this

public interface ICustomLogger : ILog
    {

    }

My logger class will be something like this.

    public  class CustomLogger : ICustomLogger
        {
            private ILog log4netLogger;
            public TMALogger(ILog logger)
            {
                log4netLogger = logger;
            }
//Plus all the methods that you would need to implement for ICustomLogger
    }

LogManager will be like this.

public class LogManager
    {
        public static ICustomLogger GetLogger(Type type)
        {
            ICustomLogger logger = null;
            try
            {
                ILog log4netLogger = log4net.LogManager.GetLogger(type);
                CustomLogger customLogger = new CustomLogger(log4netLogger);
                logger = customLogger;
            }
            catch (Exception ex)
            {
                logger = null;

            }
            return logger;
        }
    }

And finally logger will be used in class like

private readonly ICustomLogger _logger = LogManager.GetLogger(typeof(ClassName));

I hope this helps.

Upvotes: 1

linkerro
linkerro

Reputation: 5458

You should merge the 2 dlls

http://www.codeproject.com/Articles/9364/Merging-NET-assemblies-using-ILMerge

I think

Upvotes: 1

Related Questions