Configuring NLog for WCF Services

How can you configure WCF services (on the server side) to log to NLog? This is exactly the dillema I found myself in. After scouring the internet for hours, I found little of use; there are apparently many things you need to do properly. So let’s go ahead and list them out.

  • Install NLog: Make sure you have NLog installed on the machine which you’re deploying to. (If you don’t, you should see your service crash, or errors in your Event Viewer.)
  • Configure NLog Properly: Make sure that NLog actually works! You can do this by debugging into the service on your local machine (or via remote debugging). Make sure you have loggers that log both to console and to a log file, as you can verify NLog is working with the former.
  • Copy NLog.Config to the .svc Directory: Some people mention that NLog.config should be placed where your .svc file is deployed. In addition to this, I find that you still need a copy in bin. For some reason, having only one log file (either one) didn’t work, but both together (identical files) worked.
  • Check Your App Pool: Another issue I ran into is that IIS had my site configured using the classic pool; I actually had to change it to DefaultAppPool.
  • Restart IIS: If you changed anything, restart IIS via the IIS console. It should work.

One more thing to mention: make sure you have some checks in place on the log size. Depending on your usage, your log files may quickly eat up gigs of space. You can gracefully handle this many ways, the easier being to archive logs every month and simply delete unnecessary ones. I use this configuration for rolling logs:






This limits my archives to six log files, rotated monthly; on the first of each month, NLog will generate a new file (exactly at midnight!) in /archives, with the filename log.n.txt (for n in {1, 2, …}).

And it should work! If it doesn’t, post a comment (especially if you found another piece of configuration or work that needs to be done before NLog cooperates).

About Ashiq Alibhai, PMP

Ashiq has been coding C# since 2005. A desktop, web, and RIA application developer, he's touched ASP.NET MVC, ActiveRecord, Silverlight, NUnit, and all kinds of exciting .NET technologies. He started C# City in order to accelerate his .NET learning.
This entry was posted in Libraries, Web and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *