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
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.
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.
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;
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;
If you want to specify a severity, use a value defined by the TraceSeverity enumeration.
TraceSeverity severity = TraceSeverity.High;
Use the SharePoint Service Locator to request an implementation of the ILogger interface.
ILogger logger = SharePointServiceLocator.GetCurrent().GetInstance<ILogger>();
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.