WSS 3.0 had little to no support for developers as far as concerning the ULS logs (if you exclude reading them…). Things have significantly changed in SP2010; you can now write information via the Diagnostics Service. You can access the Diagnostics Service using the SPDiagnosticsService class. Here is a small code snippet:

SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local;

SPDiagnosticsCategory category = new SPDiagnosticsCategory("ULS Sample", TraceSeverity.Verbose, EventSeverity.Information);

diagnosticsService.WriteTrace(0, category, TraceSeverity.Verbose, "Hello, {0}", new[] { SPContext.Current.Web.CurrentUser.Name });

  The example above will create a new entry in the SharePoint Logs under the category “ULS Sample”, with verbose severity with the message “Hello, <Current User>”. You can also write to the Windows Application Event Log via the WriteEvent method of the SPDiagnosticsService class but that will require elevation.

  Writing to the Windows and SharePoint logs is now simple but still a mystery why the SharePoint insists to tab-separated format files instead of XML. Hopefully this will change in a future version.

Advertisements