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.
Look at how SpringFramework solves this problem. It uses Common.Logging which can then be mapped to log4net or to any other custom logging framework via the config file. You can find more details on the Common.Logging website but basically you do the following:
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
</common>
<log4net>
.... normal log4net configuration goes here...
</log4net>
If the client is also using SpringFramework or the Common.Logging directly, the conflict is still possible but its odds are greatly decreased for the following reasons:
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.
Use System.Diagnostics.Trace
. No chance of version clashes then!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With