Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional logging dependency for C# library?

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?

like image 412
cdmckay Avatar asked Dec 12 '22 09:12

cdmckay


1 Answers

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.

like image 56
Mark Avatar answered Dec 14 '22 23:12

Mark