SharePoint 2010 Diagnostic logging

In SharePoint 2010 logging can be done both in ULS and Event Logs using SPDiagnosticsService class. The class provides two methods WriteTrace and WriteEvent to write traces and  events in ULS and Event Viewer respectively. SharePoint diagnostic logging can be configured using PowerShell or Central administration site. Open CA, go to Monitoring and click on Configure diagnostic logging in Reporting section.


As can be seen I have configured Diagnostic logging to track 
"Error" Event Level and "High" Trace Level. 
The full information about different options can be found at 
Configure Diagnostic logging.
Following utility class helps create logs in ULS and Event Viewer.
public class LoggerUtiliy
{
    private static readonly SPDiagnosticsService service;
    private static readonly SPDiagnosticsCategory category;
    static LoggerUtiliy()
    {
        service = SPDiagnosticsService.Local;
        category = service.Areas["SharePoint Foundation"].Categories["General"];
    }
    public static void LogToULS(TraceSeverity severity,string message)
    {
        service.WriteTrace(101, category, severity, message, null);
    }

    public static void LogToEventViewer(EventSeverity severity, string message)
    {
        service.WriteEvent(101, category, severity, message, null);
    }      
}
In the above code we are looking for the Out of the box SharePoint Foundation Area and inside that the General Category. The above utility can be used inside the main method as follows:

static void Main(string[] args)
{
    using (SPSite site = new SPSite("http://intranet.contoso.com"))
    {
        using (SPWeb web = site.OpenWeb(""))
        {
            SPList list = web.Lists.TryGetList("NonExistingList");
            if (null == list)
            {
                LoggerUtiliy.LogToULS(TraceSeverity.Monitorable, "NonExistingList List not found");
            }
        }
    }
}

Here, I have applied filter on ULS Viewer showing only EventID = 101. In order to log into Event Viewer, change the code in main method to following: 

LoggerUtiliy.LogToEventViewer(EventSeverity.ErrorCritical, "NonExistingList List not found");

We can also create our own Custom Areas and categories by inheriting from SPDiagnosticsServiceBase class.

public class LoggingService : SPDiagnosticsServiceBase
{
    private const string AREA = "Custom Diagonostic Area";
    private const string CATEGORY = "Custom Diagonostic Category";
    private static readonly LoggingService current;

    public static LoggingService Current
    {
        get
        {
            return LoggingService.current;
        }
    }

    static LoggingService()
    {
        LoggingService.current = new LoggingService();
    }

    private LoggingService()
        : base("Custom Logging Service",SPFarm.Local)
    {
    }

    protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
    {
        List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
        {
            new SPDiagnosticsArea(LoggingService.AREA, new List<SPDiagnosticsCategory>
            {
                new SPDiagnosticsCategory(LoggingService.CATEGORY,TraceSeverity.Monitorable,EventSeverity.Error)
            })
        };

        return areas;
    }

    public static void LogErrorToULS(TraceSeverity severity, string errorMessage)
    {
        SPDiagnosticsCategory category = LoggingService.Current.Areas[LoggingService.AREA].Categories[LoggingService.CATEGORY];
        LoggingService.Current.WriteTrace(0, category, severity, errorMessage);
    }

    public static void LogErrorToEventViewer(EventSeverity severity, string errorMessage)
    {
        SPDiagnosticsCategory category = LoggingService.Current.Areas[LoggingService.AREA].Categories[LoggingService.CATEGORY];
        LoggingService.Current.WriteEvent(0, category, severity, errorMessage);
    } 
}

Copy the following code in main function to call the methods of this new class which will create new Area and Category.
LoggingService.LogErrorToULS(TraceSeverity.Monitorable, "NonExistingList List cannot be found");