Reputation: 27575
I am facing the challenge of logging implementation in my C# class library, which is a domain model. My main goal is to keep logging functionality as canonical and decoupled as possible. Usually logging messages will be written to files, and perhaps output window while debugging.
Currently I use log4net, and I have written a static Logger
class which implements the logging methods themselves. So, when methods in the domain objects are executed, they call static Logger.Log()
methods.
While this wrapping is enough to sooth my purity urges, I wonder if it would be a good idea to implement all this logging calls via events, and remove logging implementation details from the class library altogether. Then, client code would optionally listen to them and extract useful information from some LogEventArgs
.
So the question is:
Event-based logging in general, and specifically in class libraries, is a good idea or not? And why?
Thanks for reading.
Upvotes: 3
Views: 1484
Reputation: 942
Using events for logging also introduces a lot of memory leak possibilities!
I would use Dependency Injection for cross cutting concerns like logging.
With DI you have the possibility to either inject it, or use interception.
Upvotes: 3