Why I Switched from log4net to NLog

As the people who know me know, I have been a big proponent of log4net over the years. I tried very hard to stick with log4net despite years of inactivity of the project. Over the years, I found, reported and fixed issues, but the patches have never been applied. I have reached out to the remaining members of the team to get involved and help revive the project, but received few responses. I even applied patches and released an updated version myself.

As I started to use that updated version, I soon discovered that the current source has many bugs and actually fails many of its own test suites. I tried to get the test suites running and submitted more patches for those, but still the project remains dormant. Without the support of at least one member of the team, it is difficult to revive a project without forking it, which I don’t want to do.

I ended up talking with a member of the NHibernate team, I learned that they were also moving away from log4net and were looking at NLog, the new player in .NET logging. I read through the website, followed the forums and liked what I saw, so I decided to move over to NLog.

Switching over actually turned out to be fairly straight-forward once I managed a few regular expressions for search and replace. Roughly, I performed the following steps,

  1. I did a search and replace using Notepad++ in all of my project files to switch the references over.
  2. I then replaced all of my using statements.
  3. Next came the log instantiation. log4net uses ILog where NLog uses Logger. In log4net you also tend to use GetLogger( typeof( MyClass ) ) where in NLog you just use GetCurrentClassLogger(). This required a bit of RegEx magic.
  4. The hardest step was finding every instance of log.Warn( msg, exception) and converting them to log.WarnException( msg, Exception ). I did this nearly manually for each of the log levels, inspecting to see where I was passing in exceptions.
  5. Then for each of the log levels, I converted instances of log.WarnFormat( “Substitute this {0}”, val ) to log.Warn( “Substitute this {0}”, val ). This was another easy file replacement.
  6. Lastly, I removed my XmlConfigurator attributes and changed my log configurations. A quick recompile, a few minor fixes and I was up and running. Painless!

So far, I don’t regret my choice at all. I find the configuration of NLog to be much more intuitive and the Intellisense for the config file in Visual Studio sure helps. The documentation is still a bit sparse in places, but it is getting better. I do worry because it is currently a one man show, but it Jaroslaw Kowalski does seem fairly active and committed. Time will tell…

6 thoughts on “Why I Switched from log4net to NLog

  1. Pingback: log4net UdpAppender with IPv6 on Windows Vista and 7 » Alteridem Consulting

  2. Thanks for this! It was because of this post that I researched log4net’s history and found that it hasn’t been actively developed since version 1.2.10! Thank god I caught this before I got too far into development! Thanks again.

  3. I’m still going to continue use of NLog, but it does look like development of log4net has picked up again so if anyone reads this and has debate to use one over the other at this point they both support 4.5 so it really boils down to needs and complexity and maybe also future development support. NLog has not updated since Jul 18, 2011 whereas log4net has been last updated on Feb 16, 2012. It also seems that NLog’s main developer now works with Java and has very little time to work with it. http://nlog-project.org/2012/07/23/moving-forward.html It looks like I backed the wrong horse and I’ll have to switch in the future, but for now I’m good.

Comments are closed.