Reputation: 6348
I have a project that includes 10 dlls, each dll uses log4net to log. The project is not started by me, I got it like this and I need to continue it. Because it's first time for me working with log4net, I'd like to know how to get, at runtime, the configuration file (xml) and the log file (the file where the logger writes the logs). I stop the application execution using a breakpoint and I'd like to be able, using quick watch window, to get those parameters.
Thanks for help.
Upvotes: 1
Views: 4138
Reputation: 16077
Personally, I think you might be going the wrong way about this as you would be better to try to see how log4net is configured and from that determine where it logs the information.
The usual starting place is the app.config file, which may contain the log4net configuration itself, or a reference to another external file, but configuation can also be done entirely at runtime, so there is no guarantee that there even is a config file, but you could try looking at
log4net.Config.XmlConfigurator.m_repositoryName2ConfigAndWatchHandler["log4net-default-repository"]
which may show you where the config file is located.
The filename is tricker, because
Upvotes: 2
Reputation: 2243
Here is a sample of something similar:
public static void ChangeRemoteAddress(System.Net.IPAddress remoteAddressIp)
{
var hier = log4net.LogManager.GetRepository() as Hierarchy;
if (hier == null)
{
Console.WriteLine("Unable to change Syslog filename, null hierarchy");
return;
}
var sysLogAppender =
(SyslogAppender)hier.GetAppenders().
First(appender => appender.Name.Equals("UdpAppender",
StringComparison.InvariantCultureIgnoreCase));
if (null == sysLogAppender)
{
Console.WriteLine("Unable to change Syslog filename, appender not found");
return;
}
sysLogAppender.RemoteAddress = remoteAddressIp;
sysLogAppender.ActivateOptions();
}
I get the 'hierarchy' and can then get the appenders and check for the one I wanted by its name. In my case, I want to be able to programmatically change the remote address of my UDP appender, but you could handle any predefined 'setting' this way. It is a bit more trouble if you are using a custom 'setting', but this should get you going.
So, this will get you access to the XML config file (as an object). The filename of the log file for your rolling file appender, for example, would be accessed in a similar way.
Upvotes: 1
Reputation: 3261
As for me dll's must return only exceptions. Application can use log4net or other loger and log exceptions from dlls. Path to log.config i am usually store in app.config appSettings.
You can try to make inside dll something like
const string RELATIVE_LOGGER_CONFIG_FILE_PATH="log.config";
string directory=System.IO.Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
return System.IO.Path.Combine(directory, RELATIVE_LOGGER_CONFIG_FILE_PATH)
, but i think is incorrect. Simply return exceptions from dll's.
PS This code set configuration file for log4net
log4net.Config.XmlConfigurator.Configure(new FileInfo(configFilePath));
Upvotes: 0