Reputation: 5215
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
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
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
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
Reputation: 10215
The only option I can see is to completely encapsulate something with your application; two options:
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
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