How to make log4net.dll optional?

I am using log4net to implement registration in my .NET application. However, I do not want to distribute 300kb log4net.dll for each user, but instead send this DLL to the user if he has problems, and I want him to provide the logs.

So, is it possible to run my application regardless of whether the dll exists there? Of course, dll is required to register, but if you do not need to keep a log, the application should work without a DLL.

+5
source share
1 answer

Yes it is possible.

First create interfase with all your log methods:

public interface ILogger
{
    void Write(string message);
    // And much more methods.
}

, ( DummyLogger) , Log4Net (Log4NetLogger).

, factory:

static public class LogFactory
{
     static public ILogger CreateLogger()
     {
          if (/*Check if Log4Net is available*/)
               return new Log4NetLogger();
          else
               return new DummyLogger();
     }
}

, Log4Net, , bin. - :

File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Log4Net.dll")

, , , GAC - .

factory :

ILogger logger = LoggerFactory.CreateLogger();
logger.Write("I am logging something!");
+4

All Articles