Catching and logging error in Custom code(ULS logging)

using System;

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

 namespace CommonCode

{
    public class ULSLogging : SPDiagnosticsServiceBase

    {
        public static string DiagnosticAreaName = “CodeChallege LOG”;
        private static ULSLogging _Current;
        public static ULSLogging Current
        {
       

     get
            {
                if (_Current == null)
                {
                    _Current = new ULSLogging();
                }
                return _Current;
            }

        }
        private ULSLogging()
            : base(“CodeChallege Logging Service”, SPFarm.Local)
        {

        }
        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
        {
            new SPDiagnosticsArea(DiagnosticAreaName, new List<SPDiagnosticsCategory>
            {
                new SPDiagnosticsCategory(“CodeChallege”, TraceSeverity.Unexpected, EventSeverity.Error)

            })

        };
            return areas;
        }
        public static void LogError(stringcategoryName, string errorMessage)
        {
            SPDiagnosticsCategory category = ULSLogging.Current.Areas[DiagnosticAreaName].Categories[categoryName];
            ULSLogging.Current.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
        }
    }
}
in Catch block call above methode as below:-


ULSLogging.LogError(“CodeChallege”, ex.Message);

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s