Log4net does not store the location of the configuration file used to
configure it. Depending on how log4net is configured it may not know
where the configuration data came from.

If you use the DOMConfigurator to specify where the configuration file
is then you can do the same thing that log4net does to lookup where the
file should be. Something like this:

public static string LocateConfigFile(System.Reflection.Assembly
assembly)
{
        object[] repositoryAttributes =
Attribute.GetCustomAttributes(assembly,
typeof(log4net.Config.DOMConfiguratorAttribute), false);

        if (repositoryAttributes != null && repositoryAttributes.Length
> 0 && repositoryAttributes[0] != null && repositoryAttributes[0] is
log4net.Config.DOMConfiguratorAttribute)
        {
                log4net.Config.DOMConfiguratorAttribute attr =
(log4net.Config.DOMConfiguratorAttribute)repositoryAttributes[0];

                // Work out the full path to the config file
                string fullPath2ConfigFile = null;
        
                // Select the config file
                if (attr.ConfigFile == null || attr.ConfigFile.Length ==
0)
                {
                        if (attr.ConfigFileExtension == null ||
attr.ConfigFileExtension.Length == 0)
                        {
                                // Use the default .config file for the
AppDomain
                                fullPath2ConfigFile =
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                        }
                        else
                        {
                                string ext = attr.ConfigFileExtension;

                                // Force the extension to start with a
'.'
                                if (ext[0] != '.')
                                {
                                        ext = "." + ext;
                                }

                                fullPath2ConfigFile =
System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
System.IO.Path.GetFileName(assembly.Location) + ext);
                        }
                }
                else
                {
                        // Just the base dir + the config file
                        fullPath2ConfigFile =
System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
attr.ConfigFile);
                }

                return fullPath2ConfigFile;
        }
        return null;
}

You need to pass the assembly which has the DOMConfiguratorAttribute
specified on it. Probably
System.Reflection.Assembly.GetExecutingAssembly() will do the right
thing.

Cheers,
Nicko



> -----Original Message-----
> From: Simon Wallis [mailto:[EMAIL PROTECTED] 
> Sent: 18 November 2004 21:01
> To: [email protected]
> Subject: dynamically get config filename
> 
> I would like to dynamically determine the log4net config 
> filename. I tried 
> log4net.helpers.SystemInfo.ConfigurationFileLocation (seen in 
> DOMConfigurator.cs) but it gives me the wrong info. It gives 
> me "web.config", but I'm using "<assemblyname>.dll.log4net" 
> as the config filename.
> 
> I have this code in my Global.asax.cs:
> [assembly: 
> log4net.Config.DOMConfigurator(ConfigFileExtension="log4net", 
> Watch=true)]
> 
> I need to determine the config filename because I'm writing a 
> page to sit on the production servers that will allow us to 
> update the config file, should we need to change the log level.
> 
> Thanks,
> Simon.
> 

Reply via email to