Бөлісу құралы:


How to: Log an Event Message

The following procedure demonstrates how you can use the SharePoint Logger to write an event message to the Windows event log and the ULS trace log.

To log an event 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 System.Diagnostics;
    
  4. Define an event message, an area/category string, and an integer event ID as required.

    string msg = "Your 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 EventSeverity enumeration.

    EventSeverity severity = EventLogEntryType.Error;
    
  6. Use the SharePoint Service Locator to request an implementation of the ILogger interface.

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

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

For more information about how to use the LogToOperations method, see Creating Log Entries.