Logging to the SharePoint ULS logs programmatically

Logging to the SharePoint ULS logs programmatically

When writing SharePoint applications error messages should be handled appropriately, as well as displaying a message to the user it may be useful to log this message to the SharePoint 12 hive error logs. The SharePoint error logs sit in the following location of the server – 12 HiveLOGS.

To write a message to the log directly from custom code you can use the Microsoft.Office.Server.Diagnostics.PortalLog class, simply reference the Microsoft.Office.Server.dll and use the following method:

[code lang="csharp"]PortalLog.LogString(yourerrormessage)[/code]

The LogString method accepts a string so just pass in your exception message and your good to go. Additionally the ‘DebugWriteString()’ method works the same way as the above but allows you to specify the level of the item that you are logging (Critical, Error, Information or Verbose).

Hope this helps someone! :-)

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">