简体   繁体   中英

Implement logging functionality using events in C# class library - is it good practice?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM