Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make log4net.dll optional?

Tags:

c#

log4net

I use log4net to implement logging in my .NET app. However I do not want to distribute the 300kb log4net.dll for every user but instead send this dll to the user if he has problems and I want him to provide logs.

So, is it possible to make my app run whether or not the dll is there? Of course for logging the dll would be needed, but if no logging is needed, the app should run without the dll.

like image 400
Oliver Kötter Avatar asked Apr 29 '13 12:04

Oliver Kötter


People also ask

Where can I find log4net dll?

You can download the desired Log4net. dll from this path: http://logging.apache.org/log4net/download.html, Whilst I've also attached the required Log4net.

What is the use of log4net dll?

The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. log4net is a port of the excellent Apache log4j™ framework to the Microsoft® . NET runtime.

Could not load file or assembly log4net or one of its dependencies?

Solution 1 Just remove it from the dll from your project and then try to build the project. Since you don't need that one. In case you want to include that dll, you should check that, the application pool you are using is compatible with the assembly.


1 Answers

Yes, it is possible.

First create an interfase with all your log methods:

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

Now create two instances, a dummy instance (lets call it DummyLogger), and a instance which will send its messages to Log4Net (Log4NetLogger).

To finish, create a factory class:

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

You could check if Log4Net is available by simply checking if the file is in your bin-folder. Something like:

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

But I can imagine that you want to do other checks, like if it exists in the GAC or whatever.

Now you can use your factory to create your logger and "write" messages to the log:

ILogger logger = LoggerFactory.CreateLogger();
logger.Write("I am logging something!");
like image 128
Martin Mulder Avatar answered Sep 23 '22 18:09

Martin Mulder