Sunday, February 17, 2013

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(string categoryName, 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);
 

No comments:

Post a Comment

SharePoint document metadata not updating

I faced a weird issue today, Metadata for document which has lookup column was not updating even after saving the item. There was no erro...