tags:

views:

5613

answers:

12
+42  Q: 

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.

+10  A: 

First look at the rest of your stack.

If you are using NHibernate, it utilizes Log4Net directly. Other frameworks might have other specific loggers they need.

Other than that: both work fine.

I've settled on Log4Net myself. It can be a pain to configure, and if it isn't configured correctly it is a pain to figure out what went wrong. But you can make it do almost anything you would want from a logger.

If you don't have a standing issue with Log4Net, here is an article I wrote on how to get started with it: http://elegantcode.com/2007/12/07/getting-started-with-log4net/

Chris Brandsma
thanks for the info Chris. We have no existing dependencies to either.
Greg Dean
A: 

You might also consider Microsoft Enterprise Library Logging Block. It comes with nice designer.

Rashack
What does a designer do for a logging framework?
Jafin
Enables you to configure it visually as opposed to hand crafting XML. Depends on your taste...
Rashack
A: 

I think the general consensus is that nlog is a bit easier to configure and use. Both are quite capable, though.

Travis
A: 

I have the same question, but I choice the NHibernate, so my first choice is log4net, just beginning.

I notice the NLog seem simplier than log4net, i wander what is the continue ?

+1  A: 

As I noticed, log4net locks their output files the whole time application is running, so you can't delete them. Otherwise they are similar.

So I prefer NLog.

Veton
Add <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> to your fileappender config to prevent this (mentioned on the log4net examples page here: http://logging.apache.org/log4net/release/config-examples.html)
Dag
+7  A: 

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

Florian Doyon
+51  A: 

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 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.

Matt
+1 on recommending building a Facade for true Seperation of Concerns (SoC), or so you don't polute your domain.
eduncan911
The facade only works if you can do it without breaking diagnostic values that are dependent on the call stack, etc. I've seen a lot of facades that break these essential diagnostic features that ended up doing more harm that good...
James Schek
Have you looked back at EntLib now that you have more experience with logging? I'm new to logging platforms and want to make sure I go with the most solid solution first if possible.
jlafay
This project was postponed, so I have no new knowledge, sorry. Our new architect is sponsoring EntLib to replace our persistence, so it will probably be our logger, too. In your case - I don't know your situ or what you need when you say "solid". All 3 are sufficient for basic corporate .NET apps. They more differentiate on ease of setup, ease of use, flat-out performance, options, and sellability. EntLib will make a fine solution if you don't mind the syntax. IMO, coming up with your logging strategy/policy will be the bigger deal.
Matt
Log4Net does not yet support the Client Profiles of .NET 3.5 SP1 and .NET 4, because it references System.Web. Just wanted to point that out, although it's actually closely related to 'with a dependency on Log4Net, use Log4Net.'
Simpzon
NLog is actively supported (http://nlog-project.org/download) while Log4Net hasn't been updated since version 1.2.10 was published published April 19, 2006 (https://issues.apache.org/jira/browse/LOG4NET/fixforversion/11128)
Jay Cincotta
+1  A: 

I echo the above and do prefer nLog. Entlib is needlessly bloated.

Re:Log4net One thing that ALWAYS gets me with log4net is forgetting to add the following to the global.asax to init the component:

log4net.Config.XmlConfigurator.Configure();
Francis Shanahan
+4  A: 

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.

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

thank you for this article
vittore
A: 

If you go here you can find a comprehensive matrix that includes both the NLog and Log4Net libs as well as Enterprise Lib and other products.

Somebody could argue that the matrix is done in a way to underline the features of the only commercial lib present in the matrix. I think it's true but it was useful anyway to drive my choice versus NLog.

Regards

Lorenzo
+9  A: 

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
Jay Cincotta
+1  A: 

Well .. I used Enterprise library for database logging tasks and now I switched to NLog due to performance bottleneck.

some comparison info :

http://pauliusraila.blogspot.com/2010/10/solving-database-logging-bottlenecks.html

Joe