0

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?

jimconstable
  • 2,246
  • 17
  • 28

3 Answers3

1

You should merge the 2 dlls

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

I think

linkerro
  • 5,034
  • 2
  • 22
  • 28
1

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.

Sumit
  • 2,670
  • 6
  • 28
  • 53
  • How does the config work in this case? Do you still reference log4net.Appender.ConsoleAppender in the config or does it become customdll.Appender.ConsoleAppender? – jimconstable Feb 04 '12 at 00:31
  • Config works same as it did for log4net. I still refer log4net.Appender.ConsoleAppender, there will be no change in configuration section. – Sumit Feb 04 '12 at 04:56
0

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)

Community
  • 1
  • 1
Stefan Egli
  • 17,000
  • 3
  • 50
  • 72
  • I can use the appender but it is going to be useful in all project internally requiring logging so it seems logical to have a single dll I can add to projects. – jimconstable Feb 03 '12 at 21:57
  • I agree on putting all your logging functionality in one assembly, but I would never consider to merge log4net with my DLL or write a wrapper without any justification. – Stefan Egli Feb 04 '12 at 14:43