SharePoint - Logging to ULS in SharePoint 2010

 When developing custom components for SharePoint, be it simple or complex, it is a good practice to log all the activities made by the solution. It is of great help during debugging and maintaining the code. SharePoint 2010, via its object model, let's us access the diagnostic services and add custom messages to the log files.

Here is a quick example.

In this case we are trying to access a list named MyList, which does not exists in the site. So an exception will be thrown.

 using (SPSite site = new SPSite("http://site/"))  
 {    
   using (SPWeb web = site.OpenWeb())  
   {  
     try  
     {  
            SPList list = web.Lists["MyList"];            
     }  
     catch (Exception ex)  
     {   
           SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("My Category", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);  
     }  
   }  
 }  

And here is what we get in the ULS
 05/30/2014 07:47:04.01      w3wp.exe (0x0DAC)  0x21C4     Unknown  My Category 0000     Unexpected     List 'MyList' does not exist at site with URL 'http://site'.     c9afcc72-713c-4400-83cb-5b5ea0693a32   

Comments

Popular posts from this blog

The all new Movie Central

Movie Central 4.0 (New Release in works)

How to install nVidia Optimus driver