Friday 7 November 2014

Logging to ULS in SharePoint

ULS stands for Unified Logging Service which captures and writes Exceptions/Logs in Log File(A Plain Text File with .log extension). SharePoint logs Each and every exceptions in ULS. We can also log our custom errors in ULS.

LoggingService.cs

using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MysiteExtension
{
    /// <summary>
    /// Logging Service used for ULS logging
    /// </summary>
    public class LoggingService : SPDiagnosticsServiceBase
    {
        // local variables
        private static string IntranetDiagnosticAreaName = "Mysite Extension";
        private static LoggingService _Current;

        private static LoggingService Current
        {
            get
            {
                if (_Current == null)
                {
                    _Current = new LoggingService();
                }
                return _Current;
            }
        }

        private LoggingService() : base("Mysite Extension", SPFarm.Local) { }

        /// <summary>
        /// Configure Diagnositc Areas for the Intranet Diagnostic Area
        /// </summary>
        /// <returns></returns>
        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
                                               {
                                                   new SPDiagnosticsArea(IntranetDiagnosticAreaName, new List<SPDiagnosticsCategory>
                                                   {
                                                       new SPDiagnosticsCategory(Constants.MysiteExt_WEBPARTS, TraceSeverity.Unexpected, EventSeverity.Error),
                                                        new SPDiagnosticsCategory(Constants.MysiteExt_LayoutPage, TraceSeverity.Unexpected, EventSeverity.Error),
                                                         new SPDiagnosticsCategory(Constants.MysiteExt_Feature, TraceSeverity.Unexpected, EventSeverity.Error),
                                                       //new SPDiagnosticsCategory(IntranetConstants.DiagnosticsCategoryTimer, TraceSeverity.Unexpected, EventSeverity.Error),
                                                       //new SPDiagnosticsCategory(IntranetConstants.DiagnosticsCategoryEvent, TraceSeverity.Unexpected, EventSeverity.Error),
                                                       //new SPDiagnosticsCategory(IntranetConstants.DiagnosticsCategoryFeature, TraceSeverity.Unexpected, EventSeverity.Error),
                                                       //new SPDiagnosticsCategory(IntranetConstants.DiagnosticsCategoryPages, TraceSeverity.Unexpected, EventSeverity.Error),
                                                       //new SPDiagnosticsCategory(IntranetConstants.DiagnosticsCategoryUserControls, TraceSeverity.Unexpected, EventSeverity.Error)
                                                   })
                                               };
            return areas;
        }

        /// <summary>
        /// Log Exception for the Area to ULS
        /// </summary>
        /// <param name="categoryName">Diagnostic Area to log exceptions</param>
        /// <param name="ex"></param>
        public static void LogError(string categoryName, Exception ex)
        {
            SPDiagnosticsCategory category = LoggingService.Current.Areas[IntranetDiagnosticAreaName].Categories[categoryName];
            LoggingService.Current.WriteTrace(0, category, TraceSeverity.Unexpected, "Error Message: " + ex.Message + " StackTrace: " + ex.StackTrace);
        }
    }
}

Constant.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MysiteExtension
{
    /// <summary>
    /// Constants for ULS logging
    /// </summary>
    internal class Constants
    {
        internal static string MysiteExt_WEBPARTS = "Webparts";
        internal static string MysiteExt_LayoutPage = "LayoutPage";
        internal static string MysiteExt_Feature = "Feature";
    }
}

Usage:


 catch (Exception ex)
            {
                // log exception to ULS
                LoggingService.LogError(Constants.HotMysiteExt_Feature, ex);
            }

No comments:

Post a Comment