SharePoint Logging: A Simple, Custom Logger for SharePoint Webparts and SharePoint Projects

Download Project Files Now

A while ago I was building my first webpart for SharePoint and I wanted an easy way to log debug information to log file. I know there are many ways to do this, but I wanted something simple and something that writes to a log text file. It’s how I code. Anyhow, as a .NET / C# programmer, I have become accustom to NLog (http://nlog-project.org/). It’s very simple and works wonderfully. I have successfully used NLog for both console applications and web applications, so it came to no surprise that I would turn to NLog for my SharePoint logging. Unfortunately it wasn’t so easy. This is because the NLog dll isn’t signed in a way the SharePoint prefers. I found blog posts on how to make NLog work within a SharePoint site, but you had to change too many parameters as well as decrease your security and this was not preferable for our projects.

So I thought it would be nice to create my own simple logging class that I could easily wrap into my SharePoint webpart projects, that way the logging code is compiled into the webpart itself. No new permissions needed, no security issues that I could think of. Easy. All I had to do was create the class.

I modeled my class interface off NLog because it was clean, elegant and I was used to it. It’s not a perfect solution by any means and it’s not complete I am sure. In face I haven’t included many features at all, but it works for me and I thought it may be a simple tool for others as well.

Installation:

I have included a simple Visual Studio 2008 solution with a simple c# console application and webpart. This is primarily to show you how to use the code.

The base Logger code is included in the pmLog.cs file. In here you will find two classes, a pLogManager class and pLog class. The pLog class includes all of the functions needed to handle the log file itself while the pLogManager class is what is needed to access the pLog instance.

To add logging to your webpart project, or in fact most any Visual Studio project, simply copy the pmLog.cs file to your project/solution and be sure that your project references System.Configuration. Once you have done this, it would be best if you changed the namespace of the pLog and pLogManager classes to the namespace of your webpart or application for ease of access.

Second, you will need to update your web.config or app.config file. If this is for a SharePoint webpart, then you will need to find your web.config file for your SharePoint site. This is normally located somewhere like C:InetpubwwwrootwssVirtualDirectories80 where 80 is the port number of the SharePoint application in question. In the config file, under the appsettings tag you will need to add two entries:

      <appSettings>
           <
add key=pmLogFileNamevalue=C:Templogs${shortdate}.txt/>           
           <
add key=pmLogLevelsvalue=Debug,Info,Error,Warn/>
      </
appSettings>

The pmLogFileName key specifies the location of where the log files should go. The pmLogLevels key specifies what log entries you want to actually record at this time. This allows you to include certain log events like Debug that you want to record while debugging, but when you go live, you may only want to record Error messages. Just remove the levels you do not want to include, i.e

<add key=pmLogLevelsvalue=Warn,Error/>

if you only want to record only Warn and Error messages. This way you can keep the debug record events in your code, but they won’t be recorded in the log file.

Please Note:

Two items you do not want to ignore. One, the directory specified in pmLogFileName has to already exist. In this example, c:templogs has to already exist. Second, the web server or application has to have write access to this directory. Another way to say that is that whatever account is executing the application has to have write access to the log directory. For a SharePoint 2010 web part, this is the account you specified as the application account when you created your SharePoint application. By default this is the Network Service account, but it may be a custom account you created or even possibly the internet guest account. Either way, this account needs to have write access to the log directory. I suggest that this be outside of your web application directory tree so that files are not being written to a directory that your visitors could access via a web browser.

Using the Logging Class

Once you have completed the installation process, it’s time to use it. Quite easy. First you will need to create the LogManager instance. The easy way to do this is to add the following line of code right after you declare your class:

private static pLog logger = pLogManager.GetCurrentClassLogger();

Now you can add log entries in any of your class functions.

logger.Debug(“This is a test debug message”);

or

logger.Debug(“This is a test error message”);

That’s it. The code takes care of the rest.

Example

You will find in the solution that I included in the download a simple console application file, TestProgram.cs as well as a simple Web Control, WebCustomControl.cs. Further you can see a sample app.config and web.config file. If you open this project and create the c:templogs directory, you should be able to compile and run this program and the console application will write two log entries to your log directory. I included the web control and web.config files just for your reference. Also this project is set to use .NET 2.0 although it should work fine with .NET 2.0+ projects and applications.

Final Thoughts

I know there are other ways to implement logging in SharePoint, and I also know that this is a very incomplete, simple logging function. There are probably quicker, more robust ways to log in SharePoint, but this is a good start that will hopefully help many other software engineers quickly log events while debugging and maintaining custom code for SharePoint and other .NET applications.

This code should work fine for SharePoint 2007, SharePoint 2010, .NET 1.1, .NET 2.0, .NET 3.0 and .NET 3.5 projects.

You are free to use this code however you would like, as often as you like. Modify it however you want. I ask that you leave the Created by comments at the top of the pmLog.cs class.

Have any questions or suggestions on how to make this more useful? Please contact me. Thanks and happy coding.

Download Project Files Now

Leave a Reply

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

Human Check *

Filed Under

Don't forget to check out Day 2 of our #12DaysofSharePoint series! We'll make it easy for you to search for.… https://t.co/nQFSmBioxA

Happy Friday! And welcome to Day 2 of our #12DaysofSharePoint series! Today's pro-tip is definitely worth 'discover… https://t.co/UjiMeY3aAu

Thank you to all who attended @EricOverfield's session 'Get Started with #TypeScript Now'. Can't get enough of Eric… https://t.co/LP2m9cmrJo

Don't forget to check out Day 1 of our #12DaysofSharePoint series! You'll definitely want to 'tag' it for later.… https://t.co/TwynT7Gtc9

"Get Started with #TypeScript Now" w/ @EricOverfield is starting in a few minutes here at #SPFestChicago. Come geek… https://t.co/6foyOGjTsY