Use log4net in SDK

10,498

Solution 1

Consider using XmlConfigurator to configure a standalone config file location for log4net. You can use this technique to supply independent file-based configuration without having to touch app\web.config, or having to hard-code it. Example:

http://haacked.com/archive/2005/03/07/ConfiguringLog4NetForWebApplications.aspx

Update

Try the following bootstrapper to configure the standalone config file. It will construct a full path. Try logging out the path if it still appears to be having problems finding it.

public static class LogFactory
{
    public const string ConfigFileName = "log4net.config";

    public static void Configure()
    {
        Type type = typeof(LogFactory);
        FileInfo assemblyDirectory = AssemblyInfo.GetCodeBaseDirectory(type);
        string path = Path.Combine(assemblyDirectory.FullName, ConfigFileName);
        FileInfo configFile = new FileInfo(path);
        XmlConfigurator.ConfigureAndWatch(configFile);
        log4net.ILog log = LogManager.GetLogger(type);
        log.ToString();
    }
}

Call:

LogFactory.Configure();

Solution 2

you can put the configuration in the code, although not recommended.

better ship the dll with external file, or add a code- if the file exists, to use it, if not, use hard-coded configuration.

Share:
10,498
Icerman
Author by

Icerman

Updated on June 04, 2022

Comments

  • Icerman
    Icerman almost 2 years

    I am providing an SDK using C#. To enable field debugging, I want to include logging using log4net. How to enable configuration without using App.config since the assembly will be a dll?

    Thanks,