Icerman
Icerman

Reputation: 1119

Use log4net in SDK

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,

Upvotes: 3

Views: 10979

Answers (4)

Tim Lloyd
Tim Lloyd

Reputation: 38494

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();

Upvotes: 8

wageoghe
wageoghe

Reputation: 27618

You might also consider using the Common.Logging for .Net abstraction.

http://netcommon.sourceforge.net/

Common.Logging supports log4net, NLog, and Enterprise Library out of the box. It is easy to write your own adapter to use with Common.Logging if you need to.

This way your SDK becomes logging platform agnostic. Your user would still need an entry in the app.config or web.config to indicate which platform to use, but the platform-specific configuration can still be stored in a separate file.

Upvotes: 0

devdigital
devdigital

Reputation: 34369

Why not make your SDK log platform agnostic by providing an abstraction of your logger? Caliburn.Micro does this, and this article describes how it works and how the client can configure the appropriate logger they wish to use - http://buksbaum.us/2010/08/08/how-to-do-logging-with-caliburn-micro/

Upvotes: 0

Dani
Dani

Reputation: 15081

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.

Upvotes: 1

Related Questions