How to log to the SharePoint ULS Logs: Clean Debugging and Error Logging broken down into steps


You may also be interested in: UserVersity lessons from Mindsharp


 

Editor’s note: Contributor Philip Stathis is a SharePoint developer at Gig Werks. Follow him @philipstathis

This article is meant to introduce a simple error logging routine that can really simplify your debugging when needed.

I am assuming for this post that you know that SharePoint has ULS logs and that there is a nice tool called ULSViewer (http://archive.msdn.microsoft.com/ULSViewer ) that you can use to examine errors.

So let’s get to it, step by step process of spitting statements to ULS.

You need to create a new class anywhere in your project, and it can be called ULSLog2010.cs .

I pasted the code that you need to place in the class below, this is not a piece I authored myself but I can vouch for the results and ease of use.

Here’s what you need to do:

1. Create a new class called ULSLog2010.cs

2. Paste code below code in

3. Replace GWStandard.Logging with the namespace that your code is using to make it available where you need it

4. Replace SharePointCustomSolution with the name of your desired product.

5. Use the code to record debugging statements or errors like so:



6. Filter ULS log by Product = SharePointCustomSolution (or the custom name)

7. Cake