Ivan G.
Ivan G.

Reputation: 5215

Logging without a logging library

Here is the "problem". Every single library I develop logs something, however I don't want to require a library user to know that and to install log4net or similar logging implementation I would use. How are smart people creating standalone assemblies?

Logging output is not important for a library user unless he or she really wants to see the debug/info output. In this case he would install log4net and tell the library to use it somehow via the configuration file. Otherwise I'd like to ship my library without any irrelevant dependencies. Is this possible? What are the options?

In C++ I would simply create a couple of dll/so (with and without logging) statically linking them with a required library set, and if someone needs a library with logging enabled, he would simply overwrite it with another version.

Upvotes: 5

Views: 1065

Answers (5)

johnwbyrd
johnwbyrd

Reputation: 3758

You might consider including a logging library with a sufficiently broad redistribution license that permits you to distribute it along with your source code. Most BSD-based licenses will permit you to do this.

I'm biased, but http://www.logog.org may be redistributed under some pretty broad license terms, and may fit your requirements.

Upvotes: 0

Arne Claassen
Arne Claassen

Reputation: 14404

I just ran into the same problem. My library was only 78KB, but required a 238KB log4net dependency. So i wrote an abstraction class to dynamically load log4net only when it exists like this:

static Logger() {
        try {
            Assembly.Load("log4net");
            _loggingIsOff = false;
        } catch {}
    }

    public static ILog CreateLog() {
        var frame = new System.Diagnostics.StackFrame(1, false);
        var type = frame.GetMethod().DeclaringType;
        return _loggingIsOff ? (ILog)new NoLog() : new Log4NetLogger(type);
    }
    ...
}

Link to the full code is here

Upvotes: 1

Joseph Tanenbaum
Joseph Tanenbaum

Reputation: 2241

Excellent idea; things like these are the details that make life easier for both the writer and consumer of a library!

DotNetOpenAuth does this

I know the DotNetOpenAuth library behaves the way you describe. It uses log4net, but only if it's present and configured by the consuming assembly. From its README.Bin.html:

log4net.dll If present and v1.2.10, will allow for recording log messages this library emits.

There's also an informative page on how to configure DotNetOpenAuth logging in your own app.

[EDIT] I've inspected their source code, and they dynamically check for the log4net assembly and load+wire it if present (in Log4NetLogger.cs & Logger.cs). As mentioned in your comment below, they need log4net during compilation because it is referenced in several places. The clever part is that because all log4net-specific code is skipped when the check fails, there will be no runtime problems when the log4net assembly is missing.

DotNetOpenAuth is an open source project, so you should be able to mimic their approach from browsing through their initialization code.

Upvotes: 4

Adrian K
Adrian K

Reputation: 10215

The only option I can see is to completely encapsulate something with your application; two options:

  • Take an open source logging framework and bake its source code into your app (so when someone installs your app they also inadvertently install the logger). The assumption here is that your installation process would cover anything needed, or, you bake it in in such a way that there's nothing (significant) exposed to them.
  • Take a logging framework and add it to your project but only as a clean dependency - not "baked in" at the code level so much. For example you can do this with the MS EntLibs, and by providing a default configuration (and including the EntLib DLLs in your intstall process) you can successfully shield users from having to do anything.

The bottom line though (as far as I can see) is that logging (config) is usually something that people will need to mess with at some point as it's often environment specific so I doubt you can shield all users from having to know anything about it all the time.

Upvotes: 1

topspin
topspin

Reputation: 401

Here's a good intro to tracing, which is what I prefer to use, and consumers of your assembly can use it as well.

http://www.devx.com/codemag/Article/22040

Upvotes: 3

Related Questions