log4net vs. Nlog

Anyone have experience for both? How do they stack up against each other?

We are planning on using one of them for logging in an enterprise application.

References:

log4net

nlog

EDIT: We have no existing dependencies to either nlog or log4net.


Solution 1:

I was recently tasked to "prototype up some loggin'" for an upcoming project. I didn't have any logging framework experience. I researched, ran through tutorials, made toy apps, etc. on Log4Net, NLog, and Enterprise Library for a few days. Came back 3-4 weeks later and put them together into a cohesive demo. Hopefully some of this is useful to you.

My recommendation for our project is this:

  1. Use a logging facade (e.g. Common.Logging, SimpleLoggingFacade) to avoid direct dependencies.
  2. If we end up using Enterprise Library for other facilities, then use it for Logging, too.
  3. If we end up using something with a dependency on Log4Net, use Log4Net.
  4. If none of the above, use NLog. Which I'd prefer.

That's based on these findings (opinions!):

  • All 3 frameworks are capable and can do some sophisticated things. We want a quality solution, but frankly don't need ultra high performance or 60 types of event sinks.
  • All 3 have very similar basic concepts.
  • Each has its own cool tricks, like really advanced routing, or dynamic log filenames, file truncating, etc.
  • All 3 are pretty well documented in their own way.
  • For a complete newb like me, they were all a little awkward initially. No drastic differences here for the basics. I got over it.
  • When revisiting things a few weeks later, NLog was clearly the easiest to resume. I needed very little brush up on it. With Log4Net, I had to revisit a few online examples to get going. With EntLib, I gave up and did the tutorials all over again from scratch - I was totally lost.
  • I couldn't figure out how to get EntLib to do some things like log to the database. It might be easy, but it was beyond my time limit.
  • Log4Net and NLog have a small in-code footprint. EntLib is spammy, but I'd use a facade over it anyway.
  • I accidentally mis-configured EntLib and it told me at run time. Log4Net didn't. I didn't have an accidental mis-config with NLog.
  • EntLib comes with a nice looking app.config editor, which you 100% need. NLog has a config file schema so you get "intellisense". Log4Net comes with nada.

So obviously I like NLog so far. Not enough to use it in spite of having another solution available, though.

Solution 2:

A key consideration that hasn't been much discussed is support and updates.

Log4Net hasn't been updated since version 1.2.10 was published April 19, 2006.

In contrast, NLog has been actively supported since 2006 will soon release NLog 2.0 supporting many platforms that didn't exist when log4net was last updated such as:

  • NET Framework 2.0 SP1 and above, 3.5 & 4.0 (Client and Extended profiles)
  • Silverlight 2.0, 3.0, 4.0
  • .NET Compact Framework 2.0, 3.5
  • Mono 2.x profile

Solution 3:

Having had an experience with both frameworks recently, I thought I can share my views on each frameworks.

I was asked to evaluate the logging frameworks for an existing web application, I narrowed down my choices to NLog (v2.0) and log4net (v1.2.11) after going through various online forums. Here are my findings:

  1. Setting/starting up with NLog is dead easy. You go through the Getting started tutorial on their website and you are done. You get a fair idea, how thing might be with nlog. Config file is so intuitive that anyone can understand the config. For example: if you want to set the internal logging on, you set the flag in Nlog config file's header node, which is where you would expect it to be. In log4net, you set different flags in web.config's appSettings section.

  2. In log4net, internal logging doesnt output timestamp which is annoying. In Nlog, you get a nice log with timestamps. I found it very useful in my evaluations.

  3. Filters in log4net - You better check my this question - log4net filter - how to write AND filter to ignore log messages and if you find an answer/solution for this, please let me know. I understand, there is a workaround for this question, as you can write your own custom filter. But something which is not easily available in log4net.

  4. Performance - I logged around 3000 log messages to database using a stored procedure. I used simple for loop (int i=0; i<3000; i++... to log the same message 3000 times. For the writes: log4net AdoAppender took almost double the time than NLog.

  5. Log4net doesnt support asynchronous appender.

It was sufficient comparison for me to choose NLog as the logging framework. :)

Solution 4:

For anyone getting to this thread late, you may want to take a look back at the .Net Base Class Library (BCL). Many people missed the changes between .Net 1.1 and .Net 2.0 when the TraceSource class was introduced (circa 2005).

Using the TraceSource is analagous to other logging frameworks, with granular control of logging, configuration in app.config/web.config, and programmatic access - without the overhead of the enterprise application block.

  • .Net BCL Team Blog: Intro to Tracing - Part I (Look at Part II a,b,c as well)

There are also a number of comparisons floating around: "log4net vs TraceSource"

Solution 5:

For us, the key difference is in overall perf...

Have a look at Logger.IsDebugEnabled in NLog versus Log4Net, from our tests, NLog has less overhead and that's what we are after (low-latency stuff).

Cheers, Florian