Friday 10 December 2010

Write to the SharePoint ULS log files from your code

Writing to SharePoint's log files has been made very easy in SharePoint 2010.

All you have to du is call SPDiagnosticsService.Local.WriteTrace (located in Microsoft.SharePoint.Administration). First an example:

Microsoft.SharePoint.Administration.SPDiagnosticsService.Local.WriteTrace(
  0,
  new Microsoft.SharePoint.Administration.SPDiagnosticsCategory("ListTimerJob",
                            Microsoft.SharePoint.Administration.TraceSeverity.Medium,
                            Microsoft.SharePoint.Administration.EventSeverity.Information
                            ),
  Microsoft.SharePoint.Administration.TraceSeverity.Medium,
  "The list {0} found",
  new object[] { "ListTimerJob" }
);


Ordered from the most important/critical to the least the TraceSeverity levels can be set to: Unexpected, Monitorable, High, Medium, Verbose, None.
In the same order the EventSeverity levels is: Critical, Error, Warning, Information, Verbose, None.

A description of the levels can be found here: http://technet.microsoft.com/en-us/library/ee748656.aspx

My own log messages shows up in the SharePoint log file now.



I passed "ListTimerJob" as the first parameter to the SPDiagnosticsCategory above - this shows up in the Category coloumn, which makes it easy to make I filter un only my messages.


See also (links to msdn)
Using the Trace Logging API
SPDiagnosticsServiceBase Class

Thursday 9 December 2010

Creating custom Timer Jobs in SharePoint 2010

Lately I had an assignment where I needed to make my first timer job in the SharePoint.
I had a bit of trouble finding a good and strait forward and up to date tutorial, but found one in the end:

Creating Custom Timer Job in SharePoint 2010

Its a simple example of creating a timer job in SharePoint 2010 that adds items to a list each time it runs. Short, step by step and with screen shots. Just the way I like it!

If you later want a more in depth article this looks promissing: http://msdn.microsoft.com/en-us/library/cc406686(office.12).aspx


Restart the SharePoint Timer service
When working with timer jobs you need to restart the "SharePoint Timer" service everytime you have deployed new changes to your code.


I had a frustrating day at work trying to find this information anywhere on Google, but without any luck. Then I asked a colleage (which I probably should have done from the beginning!), and my day was saved!  :-)