SharePoint 2010 Logging


I have practice of logging when handling exceptions and rest of the important things which I think will need when we want to find something important while fixing bugs in the future. Following is a one of the way you can log messages in to the ULS log from the code. You can use this method in a common place and call it from any place you want in SharePoint 2010 code.
   1:  private static void LogMessage(string message, string category, TraceSeverity traceSeverity, EventSeverity eventSeverity)
   2:  {
   3:      // retrieve app settings for logging
   4:      if (WebConfigurationManager.AppSettings != null && WebConfigurationManager.AppSettings[APP_SETTINGS_LOGGING_ENABLED] != null)
   5:      {
   6:          // get the accessdenied page path from the app settings
   7:          string loggingEnabled = WebConfigurationManager.AppSettings[APP_SETTINGS_LOGGING_ENABLED].ToLower();
   8:   
   9:          if (loggingEnabled.CompareTo("true") == 0)
  10:          {
  11:              SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
  12:              diagSvc.WriteTrace(0, new SPDiagnosticsCategory(category, traceSeverity, EventSeverity.None), traceSeverity, SP_LOG, message);
  13:          }
  14:      }
  15:  }

APP_SETTINGS_LOGGING_ENABLED is a web application setting parameter. We can make it ‘false’ if you want to disable the logging facility.

You can use the following link to get more information regarding the WriteTrace method.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spdiagnosticsservicebase.writetrace.aspx

Comments

Popular posts from this blog

How to get SharePoint dll version using JavaScript quickly

SharePoint App Only Registration Key Renewal script