Tuesday, January 4, 2011

Write Error Logging Entries To The SharePoint Unified Logging Service (ULS) Logs

All SharePoint exceptions are derived from the SPException class. We can write error logging entries to the SharePoint Unified Logging Service (ULS) logs by using code similar to the following sample:

using Microsoft.SharePoint.Administration;

    // Some code
catch (Exception ex)
    // Create diagnostics category
    SPDiagnosticsCategory oCat = new SPDiagnosticsCategory("A new category", TraceSeverity.Monitorable, EventSeverity.Error);
    // Write to ULS
    SPDiagnosticsService.Local.WriteEvent(1, oCat, EventSeverity.Error, "Error custom message", ex.StackTrace);

ULS is stored in the file system in “C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\LOGS”.

No comments: