Why I Switched from log4net to NLog

2010, Nov 04    

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¦