How to implement logging in SharePoint 2010–part 2

Home | SharePoint Development | How to implement logging in SharePoint 2010–part 2

This post would focus on  how to create & manage custom log areas and custom log categories.

It is always recommended to create custom logging areas and categories using SharePoint Logger. The custom logging areas and categories makes life easier for system administrators. So that the system administrator can apply throttling rules to the applications in the same way that they configure logging and reporting from the other SharePoint applications.

  • The AddAreas and RemoveAreas methods of DiagnosticsAreaCollection can be used to add/remove areas of the diagnostic logging. The AddCategory and RemoveCategory methods of DiagnosticsCategoryCollection can be used to add/remove categories of the diagnostics logging.
  • The following snippet illustrates how to add DiganosticsArea and DiagnosticCategory to the SharePoint Logger.
       //Creates a new Diagnostic Area
        DiagnosticsArea oDiagnosticsArea = new DiagnosticsArea("Any Area1");

        //Add 1’st Diagnostics Category   
        oDiagnosticsArea.DiagnosticsCategories.Add(new DiagnosticsCategory(
            UI", EventSeverity.Error, and TraceSeverity.Medium));

        //Add 2’nd Diagnostics Category
        oDiagnosticsArea.DiagnosticsCategories.Add(new DiagnosticsCategory(
           "DataAccess", EventSeverity.Warning, and TraceSeverity.Medium));
  • It is always recommended to assign the application name to the Diagnostics Areas, which is easier for troubleshooting.

  • To save the Diagnostics area and category to the SharePoint environment, pass the instance of IConfigManager to the DiagnosticsAreaCollection.

  • Then, invoke the SaveConfiguration method of DiagnosticArea class to save all the custom areas and categories (of your application) to the SharePoint environment.

  • Do not use the default constructor of DiagnosticsAreaCollection and invoke the SaveConfiguration method together, this will cause an InvalidOperationException.

  • It is always recommended to use the overloaded constructor of DiagnosticsAreaCollection and invoke the SaveConfiguration method together

  • Do not add an area that already exists on the DiagnosticsAreaCollection, this will throw an InvalidOperationException.

  • Leverage the Remove method of DiagnosticsAreaCollection to remove the individual areas

IConfigManager oIConfigurationManager =
    SharePointServiceLocator.GetCurrent().GetInstance<IConfigManager>();
DiagnosticsAreaCollection oDiagnosticsAreaCollection = new
    DiagnosticsAreaCollection(oIConfigurationManager);

foreach (DiagnosticsArea oDiagnosticsArea in MyAreas)
{
  DiagnosticsArea oAreaToRemove = oDiagnosticsAreaCollection[oDiagnosticsArea.Name];

  if (oAreaToRemove != null)
  {  
    oDiagnosticsAreaCollection.Remove(oAreaToRemove);
  }
}
oDiagnosticsAreaCollection.SaveConfiguration();
  • Leverage the Remove method of DiagnosticsCategoryCollection to remove the individual categories

foreach (DiagnosticsArea area in oMyAreas)
{
  DiagnosticsArea oAreaToRemove = oConfiguredAreas[area.Name];

  if (oAreaToRemove != null)
  {
    foreach (DiagnosticsCategory c in area.DiagnosticsCategories)
    {
      var oExistingCategory = oAreaToRemove.DiagnosticsCategories[c.Name];
      if (oExistingCategory != null)
        {
          oAreaToRemove.DiagnosticsCategories.Remove(oExistingCategory);
        }
      }
      if (oAreaToRemove.DiagnosticsCategories.Count == 0)
      {
        oConfiguredAreas.Remove(oAreaToRemove);
      }
    }
  }
}

 Subscribe to my blog

One thought on “How to implement logging in SharePoint 2010–part 2

Leave a Reply