I was reviewing a bunch of older questions asking about logging in .NET. A number of the posts referred to the use of logging facades to create a loose coupling between the application and the chosen logging framework. This seemed straightforward enough, until I started reviewing logging frameworks. I looked at Common.Logging, which has some interesting features, but it looks like development has stopped. And I looked at Castle Windsor, but th开发者_JS百科at seems overkill if I just want to abstract the logging interface.
Most of the questions I found were older, so I'm hoping that the landscape has changed somewhat. Is there a consensus on the "right" facade to use? Or should I just bite the bullet and implement the logging directly?
Thanks!
You should only consider using a logging facade if you're developing a library that will be used by others and you do not want to force a particular logging implementation upon them. I've used Commons.Logging without problems for this purpose in the past, although as you note development seems to have ceased. It didn't support my logger of choice (NLog 2.x) and I had to compile it from source to get it working (it's been a while so do not recall if I had to write any code, but I can look if you're interested).
That said, you'll be better off using a logger directly. You get to access the full API rather than some common (lowest-denominator) subset, and avoid a layer of indirection as well as an additional library dependence.
I'd strongly recommend using either NLog or log4net. If you don't mind the XML jungle, Enterprise Library also has decent a logging block. Avoid the built-in tracing API - it is cumbersome to use and configure while providing little flexibility compared to the others.
Logging is less trivial than one would expect from the surface so implementing it yourself might not be the path you'd want to take. A big question is, why the need for a facade? A good logging framework gives many hooks to which you can hook-in your own custom logging code too.
We use log4net: http://logging.apache.org/log4net/ in combination with Gibraltar Software (so we get reports from live installations) http://www.gibraltarsoftware.com (commercial, I am not affiliated with them, just a customer). Using log4net you can use many existing appenders or write custom appenders.
I use the Trace class in the .NET Framework for logging. Simple, flexible, no fuss, and it gives you the decoupling you're describing. You can also turn it off with a conditional symbol in the code.
精彩评论