Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

.net - When should I use Tracing vs Logger.NET, Enterprise Library, log4net or Ukadc.Diagnostics?

How do I choose between standard tracing, Logger.NET, Enterprise Library, log4net or Ukadc.Diagnostics?

Is there a situation where one is more appropriate than the other? ... what would that be? (ASP.NET, console app, Azure Cloud, SOHO, Enterprise...)

What are the benefits or drawbacks?

Did I miss any other major logging frameworks?

question from:https://stackoverflow.com/questions/4775194/when-should-i-use-tracing-vs-logger-net-enterprise-library-log4net-or-ukadc-di

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are many similar questions here on SO:

You missed several commonly used logging frameworks. Here is a list of commonly used frameworks, some of which you listed:

Logging abstractions:

System.Diagnostics addons:

Other

A couple of other logging frameworks from codeplex (that I have seen mentioned here on SO):

Why might you choose one over the other? That's a tough one. A lot of it is personal preference. Some of it is technical (or feature) superiority.

One obvious drawback of any logging framework (particularly third-party ones) is the quality of support. What if you have an issue with log4net, NLog, Common.Logging, etc? Will you be able to get a fix from the developers of those frameworks? This might not be tremendously important as the source code is available for these frameworks. However, you might prefer NOT to "inherit" the source tree just to make a fix or add an enhancement. I will say that the frameworks are so extensible, that many enhancements could be added via normal extension points.

If you read the links that I posted above, I think that it is fair to say, based solely on the volume of favorable mentions, that log4net would be the clear "winner". It will be mentioned more frequently as the historical logging favorite and what many people would choose to use going forward.

NLog has its supporters, it just doesn't seem to have the penetration, or "top of mind" awareness that log4net does, even though they are very similar. NLog's capabilities are very similar to log4net and it has the extra advantage of having gone through a significant development cycle recently.

Enterprise Library is often touted as a good choice, but is almost equally as often touted as a terrible choice. Maybe some of its negative reputation is maybe not so good early versions? Maybe it is better now?

System.Diagnostics is often recommended as a reasonable choice with at least three strong benefits: no third party dependency, many Microsoft components are instrumented with System.Diagnostics, it is easily extendable (presumably to add some capabilities that are already present "for free" in frameworks like log4net and NLog?). If you use System.Diagnostics, I think the consensus would be (as would my recommendation) to use TraceSource objects rather than Trace.WriteLine/Debug.WriteLine.

Note also that System.Diagnostics and WCF work well together. WCF message traffic can be logged with System.Diagnostics and WCF will also propagate System.Diagnostics activity information (System.Diagnostics.CorrelationManager.ActivityId) across WCF service boundary calls.

I'm not so sure that log4net should continue to retain its most favored status. As has been noted elsewhere here on SO, log4net does not seem to be undergoing a lot of development recently (note that I think "log4net is dead" is an exaggeration) while NLog 2.0 is currently out in beta with the final release expected in Q1 2011 (Update: NLog 2.0 was released on Jul 17, 2011). Does this make NLog an obviously better choice than log4net? I don't know, but I think that, relatively speaking, NLog should receive at least equal consideration when choosing between the two and, probably, should be the presumed favorite for new development, at least until log4net development shows more signs of life.

log4net and NLog both offer very flexible configuration options. They allow you to have very fine granularity in the definition of your logging statements (by the "standard" pattern of defining a logger per type). They also allow for easy extension of the libraries by allow you to develop your own "logging destination" objects (log4net Appenders and NLog Targets) and "formatting" objects (log4net pattern converters and NLog LayoutRenderers).

Aside from a logging framework choice, some (many?) advocate for insulating your application code from a hard dependency on a particular logging framework by use of an abstraction layer. This can take the form of your own ILogger interface that you implement, perhaps on top of an existing framework. In the future, you could change frameworks by implementing your ILogger over a different framework. Or, you could use DI/IoC to inject "ILogger" into your code. Many DI/IoC frameworks provide a built-in ILogger abstraction that can be configured to use log4net, NLog, or Enterprise Library or you could write your own ILogger implementation and inject that). Who cares what the implementation is? Another way to insulate your code from having a hard dependency on a specific logging framework is through the use of an existing logging abstraction framework such as Common.Logging or SLF. The benefit is that, again, your application is not dependent on a specific logging framework. However, some would say that you have just traded one dependency (on a logging framework) for another (logging abstraction framework).

Two more notes about logging abstraction:

  1. A good logging abstraction should allow you to capture output from different logging frameworks in the same output file. Common.Logging calls this "bridging". Say that you have written an app using Common.Logging, backed by NLog. Now say that you are using a third party class library that is written using log4net directly. With a bridging system, you can capture the log4net output (via a custom appender) and reroute it through Common.Logging so that the third party class library's logging output can be viewed in the context of your app's logging output.

  2. Using a logging abstraction also allows you to "test drive" logging frameworks during development. You might start out thinking that log4net is that way to go, but you want to leave yourself open to trying NLog. Using a logging abstraction it is relatively easy to switch between the two. Ultimately, you can make the choice of which logging framework, but, in the meantime, you have been able to write mountains of code that is NOT dependent in any way on a specific logging framework.

Another reason that you might choose one framework over another is the environment in which you work. If you are already using part of the Enterprise Library, that might be enough to push you into using Enterprise Library logging.

What if you are developing in Silverlight? You might choose to use something like Clog - part of Calcium. You might also choose to use NLog 2.0, which is Silverlight AND WP7 compatible.

System.Diagnostics addons (Ukadc.Diagnostics, Essential.Diagnostics). These are not logging frameworks per se. Rather, they represent collections of useful objects and extension points that can be used with the existing System.Diagnostics framework. One of the best things, in my opinion, that each of these addons adds is the ability to format your logging output, similar to how you can format it with log4net and NLog. I have not used Essential.Diagnostics, but I have experimented with Ukadc.Diagnostics and think that it is really cool. It is even easy to write your own "formatting tokens".

I don't know if this completely answered your question (which is pretty broad anyway), but I think that there is plenty of food for thought here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...