Introduction
Sometimes I prefer logging to a simple log file instead of using the Event Log, Tracing or the SharePoint Logs. The ability to turn on and off these logs without having to update a dll or web.config is a great advantage in SharePoint as well.
I was a fan of Log4net project but the project is no longer being actively supported. It hasn’t been for years. Due to this lack of maintenance it is now missing support for recent .NET Frameworks like the 4.0, the Compact framework, etc…
On top of this I regularly experienced problems with getting Log4net to work in a SharePoint environment. The most common behavior was that log files were created correctly but remained empty. No log lines were written to the files.
So time to change? Welcome NLog.
Basic principles
The basic principles with NLog are the same as with Log4Net
- You reference the dll in your project
- Declare a logger object in your class/function.
- Call the logging functions
- Configure the logging behavior by configuration files.
The config files
First positive experience. NLog is very clear where it expects the configuration files to be. This may seem trivial but in a complex website hosting scenario it can be a nightmare to determine the correct location of the Log4Net config files.
The following locations will be searched when executing your program:
- in the web.config
- web.nlog located in the same directory as web.config
- NLog.config in application’s directory
- NLog.dll.nlog in a directory where NLog.dll is located
For detailed information see the NLog documentation: http://nlog-project.org/wiki/Configuration_file
The project setup
Using NLog is very simple. It’s actually exactly the same as with log4net.
- Reference the dll in your project
- Add a “using NLog” statement to your class
- Create a logger object
- Call the correct functions of the log object
Note: logging exceptions is a bit different in NLog. If you want to log an exception you have to call the ‘LogException’ function. You can not pass the exception object as a parameter to the default logging functions.
The config file
One minor issue is that the default log file layout is not as clear as with log4net.
But after reading some documentation I got my log files in almost the same look and feel as in Log4Net.
Here is my configuration file.
1: <?xml version="1.0" encoding="utf-8" ?>
2: <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4: autoReload="true">
5: <targets>
6: <target name="logfile" xsi:type="File" fileName="C:\Logs\owstimer.txt"
7: layout="${level:padding=-6} ${longdate} ${logger:shortName=true} ${message}"/>
8: </targets>
9:
10: <rules>
11: <logger name="*" minlevel="Trace" writeTo="logfile" />
12: </rules>
13: </nlog>
Again for more detailed information see the documentation on the NLog project site : http://nlog-project.org/wiki/Configuration_file
Sharepoint specifics
To get it to work in SharePoint
- Copy the NLog dll in the GAC
- Copy the above configuration file in the same folder as your web.config file and name it ‘NLog.config’.
- Deploy your customizations (web parts, eventhandlers, master pages, ….)
- Do an iisreset (or Application Pool refresh)
And that’s it…
Note : For debugging my custom timer jobs I had to copy the same file in the \bin folder of my SharePoint hive.
Note : Don’t forget to start stop the SharePoint timer service so the service picks up the new version of your dll)
net stop “Windows SharePoint Services Timer”
net start “Windows SharePoint Services Timer”
Read Full Post »