Reputation: 32240
I've been working on a library in C# and would like to offer capability for it automatically log all exceptions. Ideally, I'd like it to use log4net, thus allowing the client to configure the log however they like (i.e. they can redirect it to the EventLog or a database or what have you).
However, I would like to avoid having the logging dependency if they chose not to use the logging feature. Is this doable?
In other words, is there a way I can optionally have a log4net dependency, depending on what the client sets in config file?
Upvotes: 4
Views: 981
Reputation: 124696
Personally I prefer the Log4Net API to that provided by System.Diagnostics.Trace
.
So I have my own abstraction layer that uses a provider-model design pattern, and exposes an API similar to log4net. I can then plug in a log4net provider, but am free to replace it with a different provider (e.g. System.Diagnostics.Trace; EntLib) in the future.
The implementation is very similar to this one.
By doing this, applications have no direct dependency on the underlying logging provider, and you can easily switch to a different provider if you (or your customers) have other preferences.
Upvotes: 0
Reputation: 498914
You can use the tracing sub-system that is already built into .NET - it is configuration controlled.
I wouldn't add a dependency myself - simply emit messages through the tracing API - the client can decide how to log them if they so wish.
Upvotes: 2
Reputation: 11740
The obvious answer is to use the System.Diagnostics.Trace
subsystem with a custom TraceSource
. That way you can set up, in the configuration file, any TraceListener
you'd like, including log4net, EventLog, text files, Console
, or XML files.
If you added a log4net TraceListener
then the dependency would be loaded at runtime, and not compiled in. The Trace
subsystem has become quite powerful since its inception, and I recommend you look into it.
Upvotes: 11