You actually asked 4 questions in 1.
First, you need to understand that you have used a NuGet package manager and it is a good practice to include available packages using NuGet but not manually as it automatically places package in a proper place, lets you add this package easily to other projects of solution, provides versioning etc.
Read more about NuGet. For example, here.
Now about your questions:
First: No, log4net
package contains not only a log4net.dll
file. It also has log4net.xml
file. Yes, you can add these two files manually without using NuGet package manager.
Second: It is a list of NuGet packages in your project. You can only remove it if you are not going to use NuGet. However, it is not a good idea - let it be there, it is a single light-weight XML-file.
Third: It's a question of OOP. Create a singleton global logger, for example:
public static class LoggingFactory
{
private static ILog _logger;
private static ILog CreateLogger()
{
// Some log4net initialization
return LogManager.GetLogger("Logger");
}
public static ILog GetLogger()
{
return _logger ?? (_logger = CreateLogger());
}
}
public class AnyClassOfYourWholeSolution
{
LoggingFactory.GetLogger().DebugFormat("{0} {1}", a, b);
}
Fourth: It is a question of OOP again. Create an adapter and use it:
public interface ILogger
{
void Debug(string ip, string message);
void Info(string ip, string message);
void Error(string ip, string message);
}
public class Log4NetLogger : ILogger
{
private ILog _logger;
public Log4NetLogger()
{
// Some log4net initialization
_logger = LogManager.GetLogger("Logger");
}
public void Debug(string ip, string message)
{
// ...
}
public void Info(string ip, string message)
{
// ...
}
public void Error(string ip, string message)
{
// ...
}
}
public static class LoggingFactory
{
private static ILogger _loggerAdapter;
private static Initialize(ILogger adapter)
{
_loggerAdapter = adapter;
}
public static GetLogger()
{
return _logger;
}
}
public class BeginningOfYourProject
{
// Main() or Global.asax or Program.cs etc.
LoggingFactory.Initialize(new Log4NetLogger());
}
public class AnyClassOfYourWholeProject
{
LoggingFactory.GetLogger().Debug(ip, message);
}
Or you can extract ip to the properties of log4net:
// During initialization
log4net.ThreadContext.Properties["ip"] = ip;
// log4net Config
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%-5p %d %t IP: %property{ip} %m" />
</layout>
It is all up to your fantasy.