User Tools

Site Tools


notes:sharepoint2010:diagnostics

Exceptions and Diagnostics in SharePoint 2010

Enable advanced debugging in VS environment (the output window will contain all information that VS is receiving from SharePoint via the stack trace):

  • Go to [HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0\SharePointTools]
  • Change the DWORD value for EnableDiagnostics from 0 to 1
  • If the DWORD value does not exist, create it as a new DWORD value

An example of a class (TraceLogManager) that writes messages to the SharePoint Unified Logging System (ULS):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Diagnostics;
 
public enum DiagnosticAreaName
{
    None,
    AuthenticationProvider,
    MessagingSystem,
    MainWebsite,
    EventReceiver
}
 
public class TraceLogManager : SPDiagnosticsServiceBase
{
    private const string DiagnosticCategoryInfo = "Information";
    private const string DiagnosticCategoryError = "Error";
    private const string DiagnosticServiceName = "WBS Logging Service";
 
    private static uint messageId = 1;
 
    private static string GetDiagnosticAreaName(DiagnosticAreaName dan)
    {
        switch (dan)
        {
            case DiagnosticAreaName.AuthenticationProvider:
                return "WbsAuthenticationProvider";
            case DiagnosticAreaName.MessagingSystem:
                return "WbsMessagingSystem";
            case DiagnosticAreaName.EventReceiver:
                return "WbsEventReceiver";
            case DiagnosticAreaName.MainWebsite:
                return "WbsMainWebsite";
            default:
                return "WbsDefault";
        }
    }
 
    private static TraceLogWriter instance;
 
    public static TraceLogWriter Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new TraceLogWriter();
            }
            return instance;
        }
    }
 
    private TraceLogWriter()
        : base(DiagnosticServiceName, SPFarm.Local) { }
 
    protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
    {
        List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
        {
            new SPDiagnosticsArea(GetDiagnosticAreaName(DiagnosticAreaName.None), 
                new List<SPDiagnosticsCategory>
            {
                new SPDiagnosticsCategory(DiagnosticCategoryInfo, 
                    TraceSeverity.Verbose, EventSeverity.Information),
                new SPDiagnosticsCategory(DiagnosticCategoryError, 
                    TraceSeverity.High, EventSeverity.Error)
            }),
 
            new SPDiagnosticsArea(GetDiagnosticAreaName(DiagnosticAreaName.AuthenticationProvider), 
               new List<SPDiagnosticsCategory>
            {
                new SPDiagnosticsCategory(DiagnosticCategoryInfo, 
                    TraceSeverity.Verbose, EventSeverity.Information),
                new SPDiagnosticsCategory(DiagnosticCategoryError, 
                    TraceSeverity.High, EventSeverity.Error)
            }),
 
            new SPDiagnosticsArea(GetDiagnosticAreaName(DiagnosticAreaName.MessagingSystem), 
                new List<SPDiagnosticsCategory>
            {
                new SPDiagnosticsCategory(DiagnosticCategoryInfo, 
                    TraceSeverity.Verbose, EventSeverity.Information),
                new SPDiagnosticsCategory(DiagnosticCategoryError, 
                    TraceSeverity.High, EventSeverity.Error)
            }),
 
            new SPDiagnosticsArea(GetDiagnosticAreaName(DiagnosticAreaName.EventReceiver), 
                new List<SPDiagnosticsCategory>
            {
                new SPDiagnosticsCategory(DiagnosticCategoryInfo, 
                    TraceSeverity.Verbose, EventSeverity.Information),
                new SPDiagnosticsCategory(DiagnosticCategoryError, 
                    TraceSeverity.High, EventSeverity.Error)
            }),
 
            new SPDiagnosticsArea(GetDiagnosticAreaName(DiagnosticAreaName.MainWebsite), 
                new List<SPDiagnosticsCategory>
            {
                new SPDiagnosticsCategory(DiagnosticCategoryInfo, 
                    TraceSeverity.Verbose, EventSeverity.Information),
                new SPDiagnosticsCategory(DiagnosticCategoryError, 
                    TraceSeverity.High, EventSeverity.Error)
            })
        };
        return areas;
    }
 
    public static void WriteInfo(DiagnosticAreaName area, string message)
    {
        SPDiagnosticsCategory category = 
            TraceLogWriter.Instance.Areas[GetDiagnosticAreaName(area)].Categories[DiagnosticCategoryInfo];
        TraceLogWriter.Instance.WriteTrace(messageId, category, TraceSeverity.Verbose, message);
        messageId++;
    }
 
    public static void WriteError(DiagnosticAreaName area, string message)
    {
        SPDiagnosticsCategory category = 
            TraceLogWriter.Instance.Areas[GetDiagnosticAreaName(area)].Categories[DiagnosticCategoryError];
        TraceLogWriter.Instance.WriteTrace(messageId, category, TraceSeverity.High, message);
        messageId++;
    }
}

Catch an exception when the user does not have access to a website:

// Get SPWeb object representing the root site.
SPWeb web = SPContext.Current.Site.RootWeb;
 
// Get the collection of all subsites including those the user may not have permissions to see.
SPWebCollection subsites = web.Webs();
 
bool catchAccessDenied = SPContext.Current.Web.Site.CatchAccessDeniedException;
 
try
{
    SPContext.Current.Web.Site.CatchAccessDeniedException = false;
 
    // Loop through the subsites collection.
    foreach (SPWeb subsite in subsites)
    {
        try
        {
            // ... use subsite.Url and subsite.Title
        }
        finally
        {
            if (subsite != null)
                subsite.Dispose();
        }
    }
}
catch (UnauthorizedAccessException)
{
    // ... access denied
}
finally
{
    SPContext.Current.Web.Site.CatchAccessDeniedException = catchAccessDenied;
}
notes/sharepoint2010/diagnostics.txt · Last modified: 2015/06/24 by admin