How to: Log a Trace Message

The following procedure demonstrates how you can use the SharePoint Logger to write a trace message to the ULS trace log.

To log a trace message

  1. Add a reference to the SharePoint Guidance Library assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab, and then navigate to the ___location of the Microsoft.Practices.SharePoint.Common.dll assembly.

  2. Using the same procedure, add a reference to the Microsoft.Practices.ServiceLocation.dll assembly. This assembly contains the SharePoint Service Locator, which you will use to retrieve a logger instance.

  3. Add the following using statements to the top of your source code file.

    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.SharePoint.Common.ServiceLocation;
    using Microsoft.Practices.SharePoint.Common.Logging;
    using Microsoft.SharePoint.Administration;
    
  4. Define a trace message, an area/category string, and an integer event ID as required.

    string msg = "Your Trace Message";
    string areaCategory = @"Your Area/Your Category";
    int eventID = (int)YourEnumeration.YourEventID;
    
  5. If you want to specify a severity, use a value defined by the TraceSeverity enumeration.

    TraceSeverity severity = TraceSeverity.High;
    
  6. Use the SharePoint Service Locator to request an implementation of the ILogger interface.

    ILogger logger = 
      SharePointServiceLocator.GetCurrent().GetInstance<ILogger>();
    
  7. Call the ILogger.TraceToDeveloper method, passing in your message, area/category string, integer ID, and severity as parameters.

    logger.TraceToDeveloper(msg, eventID, severity, areaCategory);
    

For more information about how to use the TraceToDeveloper method, see Creating Trace Messages.