Over the last few months we have been developing custom SharePoint timer jobs.  Since the jobs run under a Windows Service it makes it difficult to debug them.  The saving grace we found is called ULS.  It gives you a logging location that is consistent with the native timer jobs and an API which resembles most other logging frameworks.

In order write events you first need to include the Microsoft.SharePoint.Administration assembly.  Then it is simply a matter of getting an instance of a SPDiagnosticsService and calling the WriteTrace method.  There isn’t much more to it than that.

Once you have your code decorated with WriteEvent calls and deployed you can then use the ULS viewer to monitor the output.  This captures messages as they happen when connected to the ULS source.  Since so many actions write to this repository you will need to use the viewer’s filter feature to keep your sanity.  The first thing you will want to do is limit the Process to OWSTIMER.  After that filter for your for any identifiers you may have put in your messages.

Hopefully this makes the development of timer jobs easier for everyone.