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();

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

  • 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);
      }
    }
  }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 Subscribe to my blog

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

  1. Pingback: How to implement logging in SharePoint 2010–part 2 - My experiments with SharePoint, Azure and .NET using Visual Studio

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.