tags:

views:

209

answers:

5
+5  Q: 

Logging in APIs

When I'm writing an API assembly for someone else to use, it would be useful to have some logging capability to help diagnose the clients' problems using it.

However, if I reference, for example, log4net in my assembly, this might clash with the version of log4net being used by the client's application.

I don't want to reinvent the wheel by writing my own logging framework.

What's the best way to solve my dilemma?

Edit: I suppose I could demand that the particular version of log4net I am using be installed into the GAC to avoid the version clash with the client, but that would make the API a fat thing that requires installation instead of a drop-in assembly.

+1  A: 

Use the Enterprise Library. It's flexible enough, and uses both built-in loggers and its own. It has been fairly verison-compatible as well. It's configurable enough that if there were a conflict with a particular component, you could easily swap it out, with just configuration changes.

John Saunders
A: 

This is where dependency injection is your friend--you probably don't need to use all of log4net but rather some small bits of it. So write an ILogger class in your app that exposes your functionality, then write an implementation for whatever logger(s) you want to use. For example, a Debug.Trace() logger is great for development or debugging work, while you might need a log4net driver for production. Then use dependency injection, such as StructureMap, to insert the instances of your logger in the production code.

Wyatt Barnett
+3  A: 
zvolkov
What if the client is using Common.Logging as well, or they are using NHibernate or Spring.Net. Couldn't that introduce a possibility of a clash in the Common.Logging.dll version?
Matt Howells
Note that I am actually already using Common.Logging.
Matt Howells
Normal apps should never use Common.Logging, only frameworks should. If they use NH or SF the conflict is possible although Common.Logging changes less frequently than log4net. Even then if you run into this issue you do some app.config magic to allow the app to load two different version of the same DLL. Or you can build your custom build of Common.Logging under a different name I suppose.
zvolkov
Actually, my bad, looks like NH still uses log4net directly, so your chances of clash have just lowered!
zvolkov
+1  A: 

use dependency injection (Unity, Castle Windsor, etc) to specify what the log4net dll to use that way if the client is using an older version, you can just plug in there version of log4net, if they don't have it then provide your own.

Bob The Janitor
A: 

Use System.Diagnostics.Trace. No chance of version clashes then!

Matt Howells