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.
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.
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.
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.
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!");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With